【问题标题】:Append custom stopwords to default stopwords list from nltk.corpus and remove stopwords from a series in a dataframe using lambda将自定义停用词附加到 nltk.corpus 中的默认停用词列表,并使用 lambda 从数据帧中的系列中删除停用词
【发布时间】:2023-03-29 00:24:01
【问题描述】:

我有包含 41,000 行 Flickr 标签的多列数据框。我只想从一列中删除所有英文停用词,而其他列保持不变。

这是我从 nltk.corpus 中提取停用词列表的代码:

from nltk.corpus import stopwords
stopWordsListEng = stopwords.words("english")

但我想添加我能想到的其他停用词:

according accordingly across act actually

我还没有弄清楚如何将其添加到现有的停用词列表中。

以及如何应用 lambda 仅删除一列中的停用词。因为我希望我的代码尽可能简单。

这是我的专栏的样子:

column1                        column2                                                 column3
some words from this column    i don't know actually what across to me accordingly     25,000

我希望我的专栏在删除所有停用词后看起来像这样(或多或少):

column1                        column2                column3
some words from this column    don't know what to me  25,000

【问题讨论】:

  • 您好,欢迎来到 StackOverflow!你使用的是什么版本的 Python 和 NLTK?我建议将 stopWordsListEng 转换为 set 并将单词添加到集合中。
  • 嗨@hongsy,我使用python 3.7.3,但我不知道如何检查我使用的NLTK版本

标签: python lambda tags flickr stop-words


【解决方案1】:

您可以使用列表extend向现有停用词添加其他停用词

_new_stopwords_to_add = ['according', 'accordingly', 'across', 'act', 'actually']
stopWordsListEng.extend(_new_stopwords_to_add)

仅使用 pandas.DataFrame.apply 从一个 pandas 列中删除停用词

df['column2'] = df['column2'].apply(lambda x: ' '.join([item for item in x.split() if item not in stopWordsListEng]))

【讨论】:

  • 我该怎么做@Shijith?
  • 知道了@Shijith
猜你喜欢
  • 2021-02-02
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 2021-02-17
  • 2015-05-30
  • 2021-10-24
  • 1970-01-01
相关资源
最近更新 更多