获取"RuntimeError:生成器引发StopIteration".在尝试更新Pandas数据框时 [英] Get "RuntimeError: generator raised StopIteration" while trying to update a Pandas dataframe

查看:421
本文介绍了获取"RuntimeError:生成器引发StopIteration".在尝试更新Pandas数据框时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Pandas dataframe and want to compute bigrams with the following code:

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

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

It was working fine in Jupyter. However, when I tried to run it on Linux terminal, I keep receiving the following error:

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

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

Any idea on how to resolve this?

推荐答案

更新您的NLTK.您需要版本3.4(或更高版本,以供将来的读者使用).旧版本依赖于StopIteration处理,该处理在Python 3.7中已更改.

Update your NLTK. You need version 3.4 (or higher, for future readers). Old versions relied on StopIteration handling that changed in Python 3.7.

这篇关于获取"RuntimeError:生成器引发StopIteration".在尝试更新Pandas数据框时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆