根据另一列删除列中的重复单词
Remove repeating words in column, based on another column
我有 pandas DataFrame 如下:
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
我想根据第一列删除第二列中的重复单词。我想要的输出是:
First Column
Second Column
Dog
is good
Big Cat
is here
Fat rat
is there
Pink tree
means love
如何实现?
我在这里四处寻找,但找不到适合我的解决方案。
谢谢!
尝试按行使用 apply
和 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
>>>
我有 pandas DataFrame 如下:
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 |
我想根据第一列删除第二列中的重复单词。我想要的输出是:
First Column | Second Column |
---|---|
Dog | is good |
Big Cat | is here |
Fat rat | is there |
Pink tree | means love |
如何实现?
我在这里四处寻找,但找不到适合我的解决方案。
谢谢!
尝试按行使用 apply
和 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
>>>