如何从熊猫中的列中删除常用词?

时间:2019-03-06 11:04:29

标签: python pandas

Value counts of words

如何删除“ to”,“ and”和“ from”,“ this”等常用词。我只想保留“ AI”,“数据”,“学习”,“机器”,“人工”等字眼。

1 个答案:

答案 0 :(得分:2)

我认为您要删除的是停用词,例如“ 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替换为单词列表即可。

相关问题