[英]Yelp Web Scraping
在 Jupyter 筆記本上運行我的代碼時,我遇到了這個錯誤。 我安裝了matplotlib
和pandas
。 有誰知道它可能是什么? 代碼:
def average_words(x):
words = x.split()
return sum(len(word) for words in word) / len(words)
df['average_word_length'] = df['review'].apply(lambda x: average_words(x))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-102-bbc583af9f13> in <module>
----> 1 df['average_word_length'] = df['review'].apply(lambda x: average_words(x))
/opt/anaconda3/envs/py36/lib/python3.6/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
3846 else:
3847 values = self.astype(object).values
-> 3848 mapped = lib.map_infer(values, f, convert=convert_dtype)
3849
3850 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()
<ipython-input-102-bbc583af9f13> in <lambda>(x)
----> 1 df['average_word_length'] = df['review'].apply(lambda x: average_words(x))
<ipython-input-96-4f4dfe065b11> in average_words(x)
1 def average_words(x):
2 words = x.split()
----> 3 return sum(len(word) for words in word) / len(words)
NameError: name 'word' is not defined
需要明確的是,我正在嘗試從 Yelp 的網站上抓取 Web
問題:
NameError:未定義名稱“單詞”。 您正在嘗試訪問 word,但 word 顯然沒有定義。
可能的解決方案:
我認為正確的返回語句是這樣的:
return sum(len(word) for word in words) / len(words)
(我將word和words切換for word in words
,因為我假設 words 是數組而不是 word)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.