【问题标题】:Unable to remove english stopwords from a dataframe无法从数据框中删除英语停用词
【发布时间】:2017-11-28 20:00:10
【问题描述】:

我一直在尝试对电影评论数据集执行情感分析,但我陷入了无法从数据中删除英语停用词的地步。我做错了什么?

from nltk.corpus import stopwords
stop = stopwords.words("English")
list_ = []
for file_ in dataset:
    dataset['Content'] = dataset['Content'].apply(lambda x: [item for item in x.split(',') if item not in stop])
    list_.append(dataset)
dataset = pd.concat(list_, ignore_index=True)

【问题讨论】:

  • 你得到的error是什么?
  • @open-source 没有错误 - 我执行此代码时没有任何反应。
  • 您的内容是'i, am, the, computer, machine.' 格式的吗?您可以发布一条您希望从中删除停用词的行吗?
  • 可能这就是你需要的github.com/alvations/earthy/blob/master/… =)

标签: python pandas nltk sentiment-analysis stop-words


【解决方案1】:

通过您的评论,我认为您不需要遍历dataset。 (可能dataset 只包含名为Content 的单列)

你可以这样做:

 dataset["Content"] = dataset["Content"].str.split(",").apply(lambda x: [item for item in x if item not in stop])

【讨论】:

  • 我收到了TypeError: string indices must be integers
【解决方案2】:

您正在循环数据集,但每次都附加整个帧而不使用文件_尝试:

from nltk.corpus import stopwords
stop = stopwords.words("English")
dataset['Cleaned'] = dataset['Content'].apply(lambda x: ','.join([item for item in x.split(',') if item not in stop]))

如果您想将其展平为单个列表,则返回一个包含单词列表的系列:

flat_list = [item for sublist in list(dataset['Cleaned'].values) for item in sublist]

Making a flat list out of list of lists in Python顶帽子

【讨论】:

  • 我也收到了 TypeError: string indices must be integers 的代码。 dataset 是类型 DataFrame 顺便说一句。
  • 啊好的,不清楚,您想要的结果形式是什么?一个单词列表,还是每行一个列表?
  • 我更新了我的答案,为您提供了两种选择。我假设 dataset['Content'] 元素包含逗号分隔的单词列表,如果没有,请给出示例数据集
  • 为了澄清您在两个示例中都遇到了这些错误,因为迭代数据框实际上是迭代列而不是行。为此,您可以使用 iterrows,但在这种情况下,您可以只使用如图所示的 apply,因为 iterrows 返回元组。如果你真的想做代码之类的事情,你也可以遍历数据集的索引。
  • 是的,数据集是一个逗号分隔的数据框,由电影评论组成。已从每一行中删除标点符号。预期输出:3 行有约 50 个单词,这些行中有 2、5、7 个停用词。输出应该是逗号分隔的 48、45 和 43 个字的数据帧。
【解决方案3】:

试试earthy

>>> from earthy.wordlist import punctuations, stopwords
>>> from earthy.preprocessing import remove_stopwords
>>> result = dataset['Content'].apply(remove_stopwords)

https://github.com/alvations/earthy/blob/master/FAQ.md#what-else-can-earthy-do

【讨论】:

  • 我想我必须添加“无耻插件”;P
【解决方案4】:

我认为到目前为止,代码应该可以处理信息。我所做的假设是数据有额外的空间,而用逗号分隔。以下是运行的测试:(希望对您有所帮助!

import pandas as pd
from nltk.corpus import stopwords
import nltk

stop = nltk.corpus.stopwords.words('english')

dataset = pd.DataFrame([{'Content':'i, am, the, computer, machine'}])
dataset = dataset.append({'Content':'i, play, game'}, ignore_index=True)
print(dataset)
list_ = []
for file_ in dataset:
    dataset['Content'] = dataset['Content'].apply(lambda x: [item.strip() for item in x.split(',') if item.strip() not in stop])
    list_.append(dataset)
dataset = pd.concat(list_, ignore_index=True)

print(dataset)

使用停用词输入:

                          Content
0   i, am, the, computer, machine
1                   i, play, game

输出:

                Content
 0  [computer, machine]
 1         [play, game]

【讨论】:

    猜你喜欢
    • 2019-12-07
    • 2018-05-23
    • 2019-12-13
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    相关资源
    最近更新 更多