Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pandas DataFrame - assign a list to multiple cells

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?

like image 448
Grumpy Civet Avatar asked Dec 30 '21 03:12

Grumpy Civet


People also ask

Can a Pandas DataFrame cell contain a list?

You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame. loc() methods.

How do you split a list inside a DataFrame cell into columns in pandas?

To split a pandas column of lists into multiple columns, create a new dataframe by applying the tolist() function to the column. The following is the syntax. You can also pass the names of new columns resulting from the split as a list.

How do I split a list into multiple columns in pandas?

To split a pandas column of lists into multiple columns, create a new dataframe by applying the tolist () function to the column. The following is the syntax. You can also pass the names of new columns resulting from the split as a list.

How to create multiple columns within the same assign in pandas?

Alternatively, the same behavior can be achieved by directly referencing an existing Series or sequence: You can create multiple columns within the same assign where one of the columns depends on another one defined within the same assign: © Copyright 2008-2021, the pandas development team.

How do you assign column names to a Dataframe in Python?

The column names are keywords. If the values are callable, they are computed on the DataFrame and assigned to the new columns. The callable must not change input DataFrame (though pandas doesn’t check it). If the values are not callable, (e.g. a Series, scalar, or array), they are simply assigned.

How to insert list into cell of pandas Dataframe?

Complete Example to Insert List into Cell of Pandas DataFrame By using df.at (), df.iat (), df.loc [] method you can insert a list of values into a pandas DataFrame cell. I have covered this here by using these functions with a sample DataFrame.


2 Answers

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

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

Alternatively use pd.Series with list locked in double brackects

df['col2']=np.where(df['col1']=='aa', pd.Series([lst]), df['col2'])
like image 83
wwnde Avatar answered Oct 01 '22 14:10

wwnde


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
like image 26
Jagadeeshkumar Viswanathan Avatar answered Oct 01 '22 13:10

Jagadeeshkumar Viswanathan