If I create a dataframe with a single column
a = pd.DataFrame({'x': [np.array([1,2,3,4]), np.array([1,2,3])]})
then reassign the value of the first row
a.loc[0, 'x'] = a.loc[0, 'x']
a.loc[0, 'x']
is unchanged. All is well!
However, if I add a second column
a = pd.DataFrame({'x': [np.array([1,2,3,4]), np.array([1,2,3])], 'y':[1,2]})
then a.loc[0, 'x'] = a.loc[0, 'x']
throws the error:
ValueError: Must have equal len keys and value when setting with an iterable
Can someone explain what I'm doing wrong here? I've found a solution here: i.e. use set_value
instead of loc, but I'd like to know why loc
doesn't work.
Also, is this an appropriate usage of the pandas DataFrame? I have a bunch of vectors x
that I would like to associate with some other variables and an index, and a DataFrame seemed to be the best way to store them and run operations on them (df.apply
works very well to perform operations in bulk on these arrays!).