2

I want to, at the same time, create a new column in a pandas dataframe and set its first value to a list.

I want to transform this dataframe

df = pd.DataFrame.from_dict({'a':[1,2],'b':[3,4]})

   a  b  
0  1  3
1  2  4

into this one

   a  b   c
0  1  3 [2,3]
1  2  4 NaN

I tried :

df.loc[0, 'c'] = [2,3]

df.loc[0, 'c'] = np.array([2,3])

df.loc[0, 'c'] = [[2,3]]

df.at[0,'c'] = [2,3]

df.at[0,'d'] = [[2,3]]

It does not work.

How should I proceed?

2 Answers2

2

If the first element of a series is a list, then the series must be of type object (not the most efficient for numerical computations). This should work, however.

df = df.assign(c=None)
df.loc[0, 'c'] = [2, 3]
>>> df
   a  b       c
0  1  3  [2, 3]
1  2  4    None

If you really need the remaining values of column c to be NaNs instead of None, use this:

df.loc[1:, 'c'] = np.nan
Alexander
  • 105,104
  • 32
  • 201
  • 196
1

The problem seems to have something to do with the type of the c column. If you convert it to type 'object', you can use iat, loc or set_value to set a cell as a list.

df2 = (
    df.assign(c=np.nan)
    .assign(c=lambda x: x.c.astype(object))
)

df2.set_value(0,'c',[2,3])
Out[86]: 
   a  b       c
0  1  3  [2, 3]
1  2  4     NaN
Allen Qin
  • 19,507
  • 8
  • 51
  • 67
  • The easiest way would be a combination of @Alexander and this answer (as the Nan were not needed) : df = df.assign(c=None); df.at[0,'c'] = [2,3] – Tiphaine Champetier Dec 01 '21 at 17:38