【问题标题】:Removing nltk stopwords from csv DataFrame rows从 csv DataFrame 行中删除 nltk 停用词
【发布时间】:2018-06-20 22:01:51
【问题描述】:

我正在尝试从从 .csv 读取的以下 DataFrame 中删除停用词。它基本上是一长串二元组及其在洗发水标签数据集中出现的频率。

目标是在“word1”或“word2”列中出现停用词时删除整行。

                        word1                      word2  frequency
0                       nicht                         in       3069
1                        wenn                        sie       2729
2                         von                    kindern       2108
3                         die                      hände       2094
4                        darf                      nicht       2091
5                       hände                        von       2091
6                      citric                       acid       2088
7                     kindern                   gelangen       2082
8                         sie                      einen       2053
9                         mit                        den       2023
10                       eine                   reaktion       1976

然而,到目前为止,当它与来自 nltk 的德语停用词匹配时,我什至没有设法删除仅基于列 'word1' 的行。

我使用的代码基于之前回答的问题here

import pandas as pd
from nltk.corpus import stopwords

stop = stopwords.words('german')

df = pd.read_table("myfile.csv", sep=";")
df.columns = ["word1","word2","frequency"]

df["word1"] = df["word1"].apply(lambda x: ' '.join([word for word in x.split() if word not in (stop)]))

print(df) 

我得到的错误是: AttributeError: 'list' 对象没有属性 'split'

我完全明白,我不理解被调用的函数是错误的。我正在尝试在一边做课程的同时加深对 pandas 和 nltk 的理解,但这并没有真正去任何地方:)

一旦清除 DataFrame 中的停用词,目标就是将其写入新的 CSV。但那是以后的阶段了。

编辑:为了澄清而更改标题

【问题讨论】:

  • 我无法重现您的错误。您能否在问题中包含“myfile.csv”的前几行。
  • 问题解决了。但是,如果您有兴趣;这是前 100 行的虚拟 CSV 的链接:here

标签: python pandas csv nltk corpus


【解决方案1】:

您可以为此目的使用列表推导。在这里,创建了一个新列 temp。如果 word1word2 中的任何一个在 stop 中,则 temp 的值为 False .删除那些 temp 值为 False 的行。最后,删除 temp 列并写入新的 csv 文件。希望这会有所帮助。

import pandas as pd
from nltk.corpus import stopwords

stop = stopwords.words('english')
df = pd.read_csv("myfile.csv", sep=";")


df["temp"] = [True  if row.word1 not in stop and row.word2 not in stop else False for index, row in df.iterrows()]
df = df[df.temp == True]
df.drop('temp', axis=1, inplace=True)

df.to_csv("myfile_out.csv", sep=';') 

【讨论】:

    【解决方案2】:

    apply-函数不会删除任何行。它只是将一个函数映射到 Series df["word1"] 的每个元素上。此外,您在列 "word1" 中的条目似乎属于 list 类型,而不是 string 类型。

    但是,如果 df 是一个 pandas DataFrame,其中包含一个列 "word1" 和字符串,那么就这样做

    df = df[~df["word1"].isin(stop)]
    

    ...然后您从 df 中删除所有条目,其中“word1”处于停止状态。这里的 ~ 是否定运算符,所以它的意思是 notsome_series.isin(some_iterable) 方法返回一个与 some_series 具有相同索引的系列,其中每个条目都是一个布尔值,表示 some_series 中的相应条目是否em> 包含在 some_iterable 中。

    通常,您可以从 DataFrame 中选择切片

    df[Series of booleans]
    

    其中“系列”表示熊猫系列。由于 pandas Series 与比较运算符一起使用,您可以执行以下操作

    df[df["frequency"] > 2060]
    

    它返回一个 DataFrame 只包含频率值高于 2060 的行。

    编辑:我不确定是否反对来自您,但如果此处提供的代码不起作用,您应该显示几行 .csv 文件,因为仅从您的代码我们就可以不知道你的 DataFrame 到底长什么样。

    干杯, 赛拉斯

    【讨论】:

    • 否决票不是来自我。我现在正在尝试:)
    猜你喜欢
    • 2015-01-20
    • 2013-05-12
    • 1970-01-01
    • 2019-10-01
    • 2013-10-08
    • 1970-01-01
    • 2014-04-29
    • 2019-01-21
    • 2018-12-06
    相关资源
    最近更新 更多