【问题标题】:Filtering pandas dataframe by exact words match通过精确的单词匹配过滤熊猫数据框
【发布时间】:2019-02-17 02:20:45
【问题描述】:

我想应用一个可以在文本中找到确切单词的功能。我有一个单词列表,我想在给定文本中找到确切的单词。提供的代码匹配单词的字符。我需要一个可以使用 r 匹配确切单词的解决方案。示例代码:

slno      comments
 1         Dear Customer, Thank you very much. We will get back soon with 
           analysis & rootcause by today. Regards, Amit
 2         Dear Customer, I will check and update you. Thanks, Rahul
 3         Hi Rick / Wei wang Will check the logs and update with the findings 
           Regards Satya
  4          Dear Customer, Thanks for opening the case. We will check and get 
            back to you soon. Thanks Harshit

匹配词应该是Thank, get back, Regard, check, update, logs 哪个记录匹配记录应该放置的任何单词,即使与一个应该删除的单词也不匹配。

谢谢你

【问题讨论】:

  • 您提供的不是示例代码,它是数据帧上 print() 的输出。请提供示例代码。

标签: python pandas


【解决方案1】:

在 Python 中,我会拆分 cmets 并将拆分后的评论的每个单词与您想要匹配的单词列表进行比较。

可以这样做:

word in comment.split(' ')

【讨论】:

    【解决方案2】:

    问题不够清楚。

    如果我理解正确的话:

    def lookup(tempDf, tempWord ):
        for i in range(NumberOfRows):
           if tempWord in tempDf.iloc[i]:
              tempDf.drop(tempDf.drop(tempDf.index[i]), inplace=True)
        return tempDf
    
    wordsList = ['Thank', 'get back', 'Regard', 'check', 'update', 'logs']
    
    for word in wordsList:
        df = lookup(df, word)
    

    【讨论】:

      猜你喜欢
      • 2018-03-09
      • 2022-01-04
      • 1970-01-01
      • 2019-05-21
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      相关资源
      最近更新 更多