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.
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))