【发布时间】:2020-11-16 01:29:22
【问题描述】:
我有一个推文数据框,我正在尝试清理我的“推文”列 - 删除停用词并使用词形还原。
下面是我的代码:
stop_words = set(stopwords.words('english'))
lemmatizer= WordNetLemmatizer()
sentence = df['tweet'].apply(nltk.sent_tokenize)
0 [ 'country year happy']
1 [ 'wish happy year']
2 [ 'live year together']
for i in range(len(sentence)):
words=nltk.word_tokenize(str(sentence[i]))
words=[lemmatizer.lemmatize(word) for word in words if word not in
set(stopwords.words('english'))]
sentence[i]=' '.join(words)
上面的代码给了我以下错误:(我包含了所有的回溯)
KeyError Traceback (most recent call last)
<ipython-input-384-f4bb836363e1> in <module>
1 for i in range(len(sentence)):
----> 2 words=nltk.word_tokenize(str(sentence[i]))
3 words=[lemmatizer.lemmatize(word) for word in words if word not in
set(stopwords.words('english'))]
4 sentence[i]=' '.join(words)
~\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
869 key = com.apply_if_callable(key, self)
870 try:
--> 871 result = self.index.get_value(self, key)
872
873 if not is_scalar(result):
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self,
series, key)
4403 k = self._convert_scalar_indexer(k, kind="getitem")
4404 try:
-> 4405 return self._engine.get_value(s, k,
tz=getattr(series.dtype, "tz", None))
4406 except KeyError as e1:
4407 if len(self) > 0 and (self.holds_integer() or
self.is_boolean()):
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in
pandas._libs.hashtable.Int64HashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in
pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 34
我该如何解决这个错误?
另外,我怎样才能在我的数据框中获得结果 - 添加另一列与结果?
【问题讨论】:
标签: python nlp nltk tokenize stop-words