python - 删除列中类似停用词的词
问题描述
我有一个包含对象列和超过 100,000 行的数据框,如下所示:
df['words']
0 the
1 to
2 of
3 a
4 with
5 as
6 job
7 mobil
8 market
9 think
10....
没有停用词的所需输出:
df['words']
0 way
1 http
2 internet
3 car
4 do
5 want
6 work
7 uber
8....
有没有办法使用 gensim、spacy 或 nltk 在单列中遍历常用的停用词?
我试过了:
from gensim.parsing.preprocessing import remove_stopwords
stopwords.words('english')
df['words'] = df['words'].apply(lambda x: gensim.parsing.preprocessing.remove_stopwords(" ".join(x)))
但这会导致:
TypeError: can only join an iterable
解决方案
使用 nltk 去除停用词。导入包
import pandas as pd
from nltk.corpus import stopwords
创建停用词列表
stop_words = stopwords.words('english')
stop_words[:10]
然后,
df['newword'] = list(map(lambda line: list(filter(lambda word: word not in stop_words, line)), df.words))
df
推荐阅读
- javascript - 如何为导出的 javascript 对象编写类型文件
- html - div元素问题
- macos - 在clang和gcc之间选择g++的编译器
- tensorflow - Google Colab Tensorflow 模型未使用 epoch 中的所有样本
- kotlin - Kotlin 静态初始化中的 ExceptionInInitializerError
- python - Python split() 复制
- c# - 实例化数组中的特定对象时如何触发事件?
- python - 在python中附加文本文件
- java - 如何使用 jdbc 从表行中获取数据?
- docusignapi - DocuSign,发送 2 个文档,signhere documentid 被忽略