add string to duplicate strings within two columns of dataframe

C. Lindemann :

I would like to create a sunburst chart. Therefore, I need a dataframe with unique strings in every column (for every chart level). My aim is to ad an additional string to all strings in col2 that appear anywhere in col1. My original dataframe looks something like this:

   col1     col2    value
0   pig      dog        3
1   cat  chicken        2    
2  fish      pig        4
3   dog     mule        7

What I would like to achieve is something like this:

   col1     col2    value
0   pig   dog_ag        3
1   cat  chicken        2
2  fish   pig_ag        4
3   dog     mule        7

Any help is really appreciated.

jezrael :

Use Series.mask with Series.isin:

df['col2'] = df['col2'].mask(df['col2'].isin(df['col1']), df['col2'] + '_ag')
print (df)
   col1     col2  value
0   pig   dog_ag      3
1   cat  chicken      2
2  fish   pig_ag      4
3   dog     mule      7

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291581&siteId=1