Home > database >  How to update a list column in pandas dataframe with a condition?
How to update a list column in pandas dataframe with a condition?

Time:11-17

Assume that you have a pandas dataframe with a list column.

How to append the list column based on a condition so that at the end we can get unique elements of list in that column?

import pandas as pd

df = pd.DataFrame(columns=['col1', 'col2'])
print(df)

col1 = 'A'
df = df.append({'col1': col1, 'col2': ['a1','a2', 'a3']}, ignore_index=True)
print(df)

new_list = ['a4', 'a5','a1']
df.loc[df['col1'] == col1,'col2'][0].extend(new_list)
print(df)

Get

  col1          col2
0    A  [a1, a2, a3]
  col1                      col2
0    A  [a1, a2, a3, a4, a5]

instead of

   col1          col2 
 0    A  [a1, a2, a3]   
   col1           col2 
 0    A  [a1, a2, a3, a4, a5, a1]

CodePudding user response:

Try leverage setsenter code here

df['col2'] = df['col2'].apply(lambda x:[*{*x}.union({*new_list})])

Output

col1                  col2
0    A  [a1, a5, a2, a3, a4]
  • Related