【问题标题】:Remove repeating words in column, based on another column根据另一列删除列中的重复单词
【发布时间】:2021-11-18 17:30:29
【问题描述】:

我有如下熊猫数据框:

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

我怎样才能实现它?

我已经环顾四周,但找不到适合我的解决方案。

谢谢!

【问题讨论】:

    标签: python pandas duplicates


    【解决方案1】:

    尝试将applyaxis=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
    >>> 
    

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 1970-01-01
      • 2014-07-23
      • 2021-09-12
      • 2020-06-22
      • 1970-01-01
      • 2017-12-17
      • 2016-07-11
      • 2018-05-27
      相关资源
      最近更新 更多