【问题标题】:how to remove common words from a column in pandas? [duplicate]如何从熊猫的列中删除常用词? [复制]
【发布时间】:2019-03-06 19:12:37
【问题描述】:

Value counts of words

如何删除“to”、“and”、“from”、“this”等常用词。我只对保留“AI”、“数据”、“学习”、“机器”、“人工”之类的词感兴趣。

【问题讨论】:

标签: python pandas


【解决方案1】:

我认为您要删除的是“to”、“the”等停用词。nltk 有一个预定义的停用词列表:

from nltk.corpus import stopwords
stop_words = stopwords.words('english')
stop_words

['i',
 'me',
 'my',
 'myself',
 'we',
 'our',
 'ours',
 'ourselves',
 'you',...

您可以使用 np.where 将停用词替换为 np.nan

title_analysis['new_col'] = np.where(title_analysis['words'].str.contains(stopwords), np.nan, title_analysis['words'])

然后做 value_counts()

title_analysis['new_col'].value_counts()

如果您有自己想要忽略的单词集,只需将 stop_words 替换为您的单词列表即可。

【讨论】:

    猜你喜欢
    • 2019-11-13
    • 1970-01-01
    • 2021-02-17
    • 2018-12-28
    • 2019-10-15
    • 2020-09-15
    • 2018-12-20
    • 2019-11-05
    • 2015-08-12
    相关资源
    最近更新 更多