【问题标题】:Remove stopwords-like words in a column [duplicate]删除列中类似停用词的单词[重复]
【发布时间】:2021-08-18 13:59:11
【问题描述】:

我有一个包含对象列和超过 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

【问题讨论】:

  • x是什么类型?
  • 对象但更改为字符串。原始哥伦比亚对象类型。
  • 你只能加入列表和迭代。您需要先转换为列表
  • 我可以通过以下方式删除停用词:stop_words = set(stopwords.words('english')) for word in new_words: if word not in stop_wordss: print(word) 如何放回 df 中的新列? @mousetail

标签: python pandas nltk gensim stop-words


【解决方案1】:

使用 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

【讨论】:

  • 运行:df['words'].to_list() stop_words = stopwords.words('english') df['words'] = df_freq['words'].apply(lambda x: [word for word in x.split() if word not in stop_words]) 给出AttributeError: 'int' object has no attribute 'split' 错误。
猜你喜欢
  • 2018-09-28
  • 1970-01-01
  • 2016-04-23
  • 2019-04-08
  • 2021-02-02
  • 2019-01-03
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多