我正在尝试从从 .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”的行。
我使用的代码是基于先前在此处回答的问题。
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' object has no attribute 'split'
我完全理解我在这里有错,因为我不理解被调用的函数。我正在尝试在一边做课程的同时加深对 pandas 和 nltk 的理解,但这并没有真正去任何地方:)
清除 DataFrame 中的停用词后,目标是将其写入新的 CSV。但那是为了以后的阶段。
编辑:标题更改以进行澄清