尝试更新Pandas数据框时获取“ RuntimeError:生成器引发StopIteration”

时间:2019-02-04 21:52:15

标签: python python-3.x pandas

我有一个Pandas数据框,并希望使用以下代码来计算bigrams:

from nltk import bigrams
df['tweet_bigrams'] = df['tweet_tokenized'].apply(lambda x: list(bigrams(x)))

在Jupyter中运行良好。但是,当我尝试在Linux终端上运行它时,我仍然收到以下错误:

Traceback (most recent call last):
   File "/usr/licensed/anaconda3/5.3.1/lib/python3.7/site-packages/nltk/util.py", line 468, in ngrams
history.append(next(sequence))
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
   File "url_tweet_feature_extraction.py", line 143, in <module>
    df['tweet_bigrams'] = df['tweet_tokenized'].apply(lambda x: list(bigrams(x)))
  File "/usr/licensed/anaconda3/5.3.1/lib/python3.7/site-packages/pandas/core/series.py", line 3194, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/_libs/src/inference.pyx", line 1472, in pandas._libs.lib.map_infer
  File "url_tweet_feature_extraction.py", line 143, in <lambda>
df['tweet_bigrams'] = df['tweet_tokenized'].apply(lambda x: list(bigrams(x)))
  File "/usr/licensed/anaconda3/5.3.1/lib/python3.7/site-packages/nltk/util.py", line 491, in bigrams
for item in ngrams(sequence, 2, **kwargs):
RuntimeError: generator raised StopIteration

关于如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:2)

更新您的NLTK。您需要版本3.4(或更高版本,以供将来的读者使用)。旧版本依赖var textArray = []; const wordOne = "Hello"; const wordTwo = "World"; textArray = [(wordOne !== "foo" && wordOne), (wordTwo !== "foo" && wordTwo)]; console.log(textArray.join(", "));处理,该处理在Python 3.7中有所更改。