I was trying to add a list of numpy arrays as elements to the pandas DataFrame:
using:
df.loc[df['B']==4,'A'] = [np.array([5, 6, 7, 8]),np.array([2,3])]
Whether or not this is allowed seems to depend on how I initialise df:
Testing two different initialisations of df
Can someone explain to me what's going on?
Here's the code as text for everyone to try:
The code that's not working
df = pd.DataFrame(columns=['A','B'])
a = [1,2,0,4,5]
b = [3,4,4,7,3]
df['A'] = a
df['B'] = b
df.loc[df['B']==4,'A'] = [np.array([5, 6, 7, 8]),np.array([2,3])]
df
The code that's working
df = pd.DataFrame(columns=['A','B'])
a = [1,2,0,4,5]
b = [3,4,4,7,3]
for i in range(len(a)):
df.loc[i,'A'] = a[i]
df.loc[i,'B'] = b[i]
df.loc[df['B']==4,'A'] = [np.array([5, 6, 7, 8]),np.array([2,3])]
df