【问题标题】:Pandas: search list of keywords in the text column and tag itPandas:在文本列中搜索关键字列表并对其进行标记
【发布时间】:2020-01-01 18:13:56
【问题描述】:

我将单词袋作为列表格式的元素。我正在尝试搜索这些单词中的每一个是否仅在它“开始于”列表中的元素时才出现在熊猫数据框中。我试过用“startswith”和“contains”来比较。

代码:

import pandas as pd
# list of words to search for
searchwords = ['harry','harry potter','secret garden']

# Data
l1 = [1, 2, 3,4,5]
l2 = ['Harry Potter is a great book',
      'Harry Potter is very famous',
      'I enjoyed reading Harry Potter series',
      'LOTR is also a great book along',
      'Have you read Secret Garden as well?'
]
df = pd.DataFrame({'id':l1,'text':l2})
df['text'] = df['text'].str.lower()

# Preview df:
    id  text
0   1   harry potter is a great book
1   2   harry potter is very famous
2   3   i enjoyed reading harry potter series
3   4   lotr is also a great book along
4   5   have you read secret garden as well?

尝试#1:

When I run this command it picks it up and gives me the results through out the text column. Not what I am looking for. I just used to check if I am doing things right for an example reasons for my understanding.
df[df['text'].str.contains('|'.join(searchwords))]

尝试 #2: 当我运行此命令时,它什么也不返回。这是为什么?我做错了什么?当我将 'harry' 搜索为单个时,它可以工作,但当我传入元素列表时却不行。

df[df['text'].str.startswith('harry')] # works with single string.
df[df['text'].str.startswith('|'.join(searchwords))] # returns nothing! 

【问题讨论】:

    标签: python python-3.x pandas


    【解决方案1】:

    使用startswithtuple

    例如:

    searchwords = ['harry','harry potter','secret garden']
    
    # Data
    l1 = [1, 2, 3,4,5]
    l2 = ['Harry Potter is a great book',
          'Harry Potter is very famous',
          'I enjoyed reading Harry Potter series',
          'LOTR is also a great book along',
          'Have you read Secret Garden as well?'
    ]
    df = pd.DataFrame({'id':l1,'text':l2})
    df['text'] = df['text'].str.lower()
    
    print(df[df['text'].str.startswith(tuple(searchwords))] )
    

    输出:

       id                          text
    0   1  harry potter is a great book
    1   2   harry potter is very famous
    

    【讨论】:

    • 有趣的+1,你能解释一下startswith是如何解释元组的吗?
    • 这很有趣。这仅适用于开始和结束。永远不会猜到会使用元组!
    • 谢谢,很好,但是为什么只有元组,我的意思是为什么不直接列出列表,是因为它是这样写的还是元组有一些我不知道的特殊属性,或者我在这里遗漏了什么..@Rakesh
    【解决方案2】:

    由于startswith 接受str 并且没有正则表达式,所以使用str.findall

    df[df['text'].str.findall('^(?:'+'|'.join(searchwords) + ')').apply(len) > 0]
    

    输出

       id                          text
    0   1  harry potter is a great book
    1   2   harry potter is very famous
    

    【讨论】:

    • 感谢^(?: 的及时解决方案,'startswith' 是吗?好奇怎么办?不知道'startwith'不能使用正则表达式
    【解决方案3】:

    您可以在 startswith 函数中传递一个元组来检查多个单词 看到这个str.startswith with a list of strings to test for

    在你的情况下,你可以这样做

    df['text'].str.startswith(tuple(searchwords))
    
    Out:
    0     True
    1     True
    2    False
    3    False
    4    False
    Name: text, dtype: bool
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2011-07-10
      • 2019-07-26
      相关资源
      最近更新 更多