I want to place a numpy array in a cell of a pandas dataframe.
For specific reasons, before assigning the array to the cell, I add another column in the same dataframe, whose values are set to NaN.
Can someone help me understand what adding the column with the nans does to my data frame, why breaks the code, and how I can fix it?
Inserting an array into a column works:
import pandas as pd
import numpy as np

#%% this works as expected
df = pd.DataFrame([0, 1, 2, 3, 4], columns=['a'])
df['a'] = df['a'].astype(object)
df.loc[4, 'a'] = np.array([5, 6, 7, 8])
df

But after inserting the column with nans, the same code breaks and I get the following error:
ValueError: Must have equal len keys and value when setting with an iterable
#%% after adding a second column, x, filled with nan, the code breaks
df = pd.DataFrame([0, 1, 2, 3, 4], columns=['a'])
df['x'] = np.nan
df['a'] = df['a'].astype(object)
df.loc[4, 'a'] = np.array([5, 6, 7, 8])
df

Finally, I want to add the array to the new column, but I get the same error.
#%% this is what I want to do, breaks, too
df = pd.DataFrame([0, 1, 2, 3, 4], columns=['a'])
df['x'] = np.nan
df['x'] = df['x'].astype(object)
df.loc[4, 'x'] = np.array([5, 6, 7, 8])
df



Adding numpy array to Pandas dataframe cell results in ValueError
If you only need to set a single cell, use at:
df.at[4, 'a'] = np.array([5, 6, 7, 8])


