【问题标题】:Filtering out rows with strings in a dataframe, which do not contain certain words, using Python使用Python过滤掉数据框中不包含某些单词的字符串行
【发布时间】:2021-02-27 02:16:28
【问题描述】:

我有一个带有“文本”列的数据框。

我想过滤除 text 列中的行以外的所有其他内容,包含某些字符串。 我的单词列表很长。例如,犯罪、税收等。

这适用于一个词:

data_cleaned = data_cleaned.loc[data_cleaned['text'].str.contains('population')].reset_index(drop = True)

如何添加多个单词,不仅有人口,还有犯罪等。

我看到答案like this,但它对我不起作用。

更新。

我的完整单词列表如下所示

key_words = ['population'
                          'migrarion'
                          'crime',
                          'safety',
                          'taxation',
                          'taxes',
                          'weather', 
                          'climate',
                          'opportunities',
                          'employment',
                          'unemployment',
                          'cultural life',
                          'services',
                          'jobs',
                          'economic growth',
                          'economic decline',
                          'pollution',
                          'environment',
                          'health',
                          'insurance',
                          'education',
                          'natural disaster',
                          'retirement']

【问题讨论】:

    标签: python string text nlp


    【解决方案1】:

    假设lst 是以下字符串列表,则可以:

    def selector(s):
        for w in lst:
            if w in s:
                return True
        return False
    
    data_cleaned = data_cleaned.loc[data_cleaned['text'].apply(selector)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 2012-02-02
      • 2021-12-18
      • 2021-12-10
      相关资源
      最近更新 更多