python - Python中的多个名称类型错误wordcloud
问题描述
我很混乱。我正在尝试为我的数据集制作一个 wordcloud,但我不断收到我似乎无法弄清楚的错误......数据集不是文本,它是一个 csv 文件。
我在此代码之上有一个完整的代码,用于清理数据并添加情绪分析列等。但这是我代码的 wordcloud 部分:
#create wordcloud to see what kind of words apear in the reviews
from wordcloud import WordCloud
import matplotlib.pyplot as plt
def show_wordcloud(data, title = None):
wordcloud = WordCloud(
background_color='white',
max_words=200,
max_font_size=40,
scale=3,
random_state=42
).generate(str(data))
fig = plt.figure(1, figsize = (20,20))
plt.axis('off')
plt.title('Review words')
if title:
fig.suptitle(title, fontsize = 20)
fig.subplots_adjust(top = 2.3)
plt.imshow(wordcloud)
plt.show()
#print wordcloud
show_wordcloud(reviews_df['review'])
我的 Python 终端的错误是:
>>> from wordcloud import WordCloud
import matplotlib.pyplot as plt
def show_wordcloud(data, title = None):
wordcloud = WordCloud(
background_color='white',
max_words=200,
max_font_size=40,
scale=3,
random_state=42
).generate(str(data))
fig = plt.figure(1, figsize = (20,20))
plt.axis('off')
plt.title('Review words')
if title:
fig.suptitle(title, fontsize = 20)
fig.subplots_adjust(top = 2.3)
plt.imshow(wordcloud)
plt.show()
#print wordcloud
show_wordcloud(reviews_df['review'])
>>> import matplotlib.pyplot as plt
>>> def show_wordcloud(data, title = None):
... wordcloud = WordCloud(
... background_color='white',
... max_words=200,
... max_font_size=40,
... scale=3,
... random_state=42
... ).generate(str(data))
...
>>> fig = plt.figure(1, figsize = (20,20))
>>> plt.axis('off')
(0.0, 1.0, 0.0, 1.0)
>>> plt.title('Review words')
Text(0.5, 1.0, 'Review words')
>>> if title:
... fig.suptitle(title, fontsize = 20)
... fig.subplots_adjust(top = 2.3)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
**NameError: name 'title' is not defined**
>>> plt.imshow(wordcloud)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
**NameError: name 'wordcloud' is not defined**
>>> plt.show()
>>> #print wordcloud
>>>
>>> show_wordcloud(reviews_df['review'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
**NameError: name 'reviews_df' is not defined**
>>>
我究竟做错了什么??有人可以帮忙吗?
解决方案
推荐阅读
- python - 读取文件名并插入数据库(使用 python 和 MSSQL)
- php - 如何在页面加载时检查空表单变量
- python - Python ImportError:确认安装后没有名为“请求”的模块
- python - Pandas:对于 df 中的每一行,计算该值在另一列中存在的次数
- python - 一旦函数返回,什么会保留对函数嵌套类的引用?
- node.js - 电子和 MLab
- .net-core - 将 Microsoft.AspNetCore.All 添加到私有 NuGet
- csvkit - 如何诊断csvsql“未找到列”错误
- r - 如何使用 dplyr 加入 2 个表并保留 RHS 表中的所有列?
- python - 我怎样才能让 QLabel 与它显示的 QPixmap 的大小完全一致?