Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill a dataframe column with empty arrays

I need to fill a pandas dataframe column with empty numpy arrays. I mean that any row has to be an empty array. Something like

df['ColumnName'] = np.empty(0,dtype=float)

but this don't work because it tries to use every value of the array and assign one value per row.

I tried then

for k in range(len(df)):
     df['ColumnName'].iloc[k] = np.empty(0,dtype=float)

but still no luck. Any advice ?

like image 256
Uge Avatar asked Nov 30 '25 06:11

Uge


1 Answers

You can repeat the np.empty into number of rows and then assign them to the column. Since it aint a scalar it cant be directly assigned like df['x'] = some_scalar.

df = pd.DataFrame({'a':[0,1,2]})

df['c'] = [np.empty(0,dtype=float)]*len(df)

Output :

  a   c
0  0  []
1  1  []
2  2  []
like image 143
Bharath Avatar answered Dec 01 '25 18:12

Bharath