add string to duplicate strings within two columns of dataframe

Viewed 53

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.

2 Answers

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

Using list comprehension

df["col2"] = [i+"_ag" if i in df["col1"].values else i for i in df["col2"]]

Or you can use np.where

df["col2"] = np.where(df["col2"].isin(df["col1"]), df["col2"]+"_ag", df["col2"])
Related