Remove repeating words in column, based on another column

Viewed 62

I have got pandas DataFrame as below:

First Column Second Column
Dog Dog is good
Big Cat Big cat is here
Fat rat Fat rat is there
Pink tree Pink tree means love

I want to remove repeating word in second column based on first column. My desired output is:

First Column Second Column
Dog is good
Big Cat is here
Fat rat is there
Pink tree means love

How can i achieve it?

I have looked around here, but could not find solution which would suite me.

Thanks!

1 Answers

Try using row-wise apply with axis=1:

df['Second Column'] = df.apply(lambda x: x['Second Column'].lower().replace(x['First Column'].lower(), ''), axis=1)

>>> df
  First Column Second Column
0          Dog       is good
1      Big Cat       is here
2      Fat rat      is there
3    Pink tree    means love
>>> 
Related