Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas Dataframe add element to a list in a cell

I am trying something like this:
List append in pandas cell

But the problem is the post is old and everything is deprecated and should not be used anymore.

d = {'col1': ['TEST', 'TEST'], 'col2': [[1, 2], [1, 2]], 'col3': [35, 89]}
df = pd.DataFrame(data=d)
col1 col2 col3
TEST [1, 2, 3] 35
TEST [1, 2, 3] 89

My Dataframe looks like this, were there is the col2 is the one I am interested in. I need to add [0,0] to the lists in col2 for every row in the DataFrame. My real DataFrame is of dynamic shape so I cant just set every cell on its own.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

End result should look like this:

col1 col2 col3
TEST [1, 2, 3, 0, 0] 35
TEST [1, 2, 3, 0, 0] 89

I fooled around with df.apply and df.assign but I can’t seem to get it to work.
I tried:

df['col2'] += [0, 0]

df = df.col2.apply(lambda x: x.append([0,0]))
     Which returns a Series that looks nothing like i need it

df = df.assign(new_column = lambda x: x + list([0, 0))

>Solution :

Not sure if this is the best way to go but, option 2 works with a little modification

import pandas as pd

d = {'col1': ['TEST', 'TEST'], 'col2': [[1, 2], [1, 2]], 'col3': [35, 89]}
df = pd.DataFrame(data=d)
df["col2"] = df["col2"].apply(lambda x: x + [0,0])
print(df)

Firstly, if you want to add all members of an iterable to a list use .extend instead of .append. This doesn’t work because the method works inplace and doesn’t return anything so "col2" values become None, so use list summation instead. Finally, you want to assign your modified column to the original DataFrame, not override it (this is the reason for the Series return)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading