Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an array to pandas dataframe

Tags:

python

pandas

I have a dataframe, and I want to create a new column and add arrays to this each row of this new column. I know to do this I have to change the datatype of the column to 'object' I tried the following but it doesn;t work,

import pandas
import numpy as np

df = pandas.DataFrame({'a':[1,2,3,4]})
df['b'] = np.nan
df['b'] = df['b'].astype(object)
df.loc[0,'b'] = [[1,2,4,5]]

The error is

ValueError: Must have equal len keys and value when setting with an ndarray

However, it works if I convert the datatype of the whole dataframe into 'object':

df = pandas.DataFrame({'a':[1,2,3,4]})
df['b'] = np.nan
df = df.astype(object)
df.loc[0,'b'] = [[1,2,4,5]] 

So my question is: why do I have to change the datatype of whole DataFrame?

like image 473
lizardfireman Avatar asked Aug 21 '26 02:08

lizardfireman


1 Answers

try this:

In [12]: df.at[0,'b'] = [1,2,4,5]

In [13]: df
Out[13]:
   a             b
0  1  [1, 2, 4, 5]
1  2           NaN
2  3           NaN
3  4           NaN

PS be aware that as soon as you put non scalar value in any cells - the corresponding column's dtype will be changed to object in order to be able to contain non-scalar values:

In [14]: df.dtypes
Out[14]:
a     int64
b    object
dtype: object

PPS generally it's a bad idea to store non-scalar values in cells, because the vast majority of Pandas/Numpy methods will not work properly with such data.

like image 139
MaxU - stop WAR against UA Avatar answered Aug 22 '26 15:08

MaxU - stop WAR against UA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!