3

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 ?

Uge
  • 63
  • 2
  • 5
  • 1
    Now, what do you mean by `empty`? Have you read the docs on `np.empty`? – Divakar Oct 27 '17 at 13:12
  • I mean that every row of the dataframe has to contain an array object, unitialisated and of zero length. – Uge Oct 27 '17 at 13:14
  • 1
    Do simple search before asking https://stackoverflow.com/questions/31466769/add-column-of-empty-lists-to-dataframe – BENY Oct 27 '17 at 14:55
  • What's the intended use of this column? That kind of array is pretty useless. – hpaulj Oct 27 '17 at 16:51
  • 1
    Possible duplicate of [Add column of empty lists to DataFrame](https://stackoverflow.com/q/31466769/1278112) – Shihe Zhang Oct 28 '17 at 02:53

2 Answers2

2

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  []
Bharath M Shetty
  • 30,075
  • 6
  • 57
  • 108
1

You can also use a simple comprehension

df = pd.DataFrame({'a':[0,1,2]})
df['c'] = [[] for i in range(len(df))]

Output

   a   c
0  0  []
1  1  []
2  2  []
vkt
  • 439
  • 5
  • 6