Home > database >  python pandas DataFrame - assign a list to multiple cells
python pandas DataFrame - assign a list to multiple cells

Time:12-30

I have a DataFrame like

name col1 col2
 a    aa   123
 a    bb   123
 b    aa   234

and a list

[1, 2, 3]

I want to replace the col2 of every row with col1 = 'aa' with the list like

name col1     col2
 a    aa   [1, 2, 3]
 a    bb       123
 b    aa   [1, 2, 3]

I tried something like

df.loc[df[col1] == 'aa', col2] = [1, 2, 3]

but it gives me the error:

ValueError: could not broadcast input array from shape (xx,) into shape (yy,)

How should I get around this?

CodePudding user response:

Make it simple. np.where should do. Code below

df['col2']=np.where(df['col1']=='aa', str(lst), df['col2'])

CodePudding user response:

A list comprehension with an if/else should work

df['col2'] = [x['col2'] if x['col1'] != 'aa' else [1,2,3] for ind,x in df.iterrows()]

CodePudding user response:

It will be safe to do with for loop

df.col2 = df.col2.astype(object)
for x in df.index:
    if df.at[x,'col1'] == 'aa':
       df.at[x,'col2'] = [1,2,3]
       
df
  name col1       col2
0    a   aa  [1, 2, 3]
1    a   bb        123
2    b   aa  [1, 2, 3]

CodePudding user response:

You can also use:

data = {'aa':[1,2,3]}
df['col2'] = np.where(df['col1'] == 'aa', df['col1'].map(data), df['col2'])

You should use this with care, as doing this will change list to both locations:

df['col2'].loc[0].append(5)
print(df)
#OUTPUT
  name col1          col2
0    a   aa  [1, 2, 3, 5]
1    a   bb           123
2    b   aa  [1, 2, 3, 5]

But this is fine:

df = df.loc[1:]
print(df)
#OUTPUT
  name col1       col2
1    a   bb        123
2    b   aa  [1, 2, 3]

CodePudding user response:

import pandas as pd
df = pd.DataFrame({"name":["a","a","b"],"col1":["aa","bb","aa"],"col2":[123,123,234]})
l = [1,2,3]
df["col2"] = df.apply(lambda x: l if x.col1 == "aa" else x.col2, axis =1)
df
  • Related