【问题标题】:Remove text lines only from first 2 lines if matches to other column's text如果与其他列的文本匹配,则仅从前 2 行中删除文本行
【发布时间】:2021-04-24 11:52:37
【问题描述】:

我有一个 Pandas 数据框 (Python 3.x),如下所示:

title type content
Mango Paragraph Mango is called the king of fruits. It is a stone fruit produced from numerous species of tropical trees.....
Dates Paragraph [Dates] /n [Paragraph] /n Dates have been a staple food of the Middle East and the Indus Valley for thousands of years......
Mango Essay "Mango" "Essay" Mango is called the king of fruits. It is a stone fruit produced from numerous species of tropical trees.....
Apple Article Apple /n Article /n An apple is an edible fruit produced by an apple tree.....
...... ...... ...................................................................................

在某些行中,“title”和“type”列的文本也出现在“content”列中。如果“内容”列中存在“标题”和“类型”,我需要删除整行。 我尝试了很多次,但未能删除包含其他列相同文本的整行。 请帮忙!!

【问题讨论】:

    标签: python-3.x pandas text nlp


    【解决方案1】:

    numpy.char.find怎么样:

    import numpy as np
    
    values = df.values.astype(str)
    
    mask = (np.char.find(values[:,[2,2]], values[:,[0,1]]) == -1).any(1) 
    df[mask]    
    

    输出

       title       type                                            content
    0  Mango  Paragraph  Mango is called the king of fruits. It is a st...
    

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      相关资源
      最近更新 更多