【发布时间】:2019-09-10 20:35:43
【问题描述】:
我正在尝试从 Python 中的数据框创建 wordcloud,但是当我尝试运行代码时,它给了我一条错误消息,如下所示:NameError: name 'text' is not defined。 数据框由从当地报纸上抓取的数据组成,我想做的是用最常提到的单词制作一个 wordcloud。
数据框是这样的:
import os
os.chdir("H:\RP3055G001\Estructuracion\Python\Gestion")
df = pd.read_csv("export_dataframe.csv")
from nltk.corpus import stopwords
nltk_sw = stopwords.words('spanish')
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
%matplotlib inline
wordcloud_2 = WordCloud(stopwords=nltk_sw,background_color='white',width=1400,height=1200).generate(text)
plt.figure(figsize=(10,50))
plt.imshow(wordcloud_2)
plt.axis('off')
plt.show()
【问题讨论】:
-
该错误意味着您正在尝试使用
.generate(text),但您尚未将变量text定义为任何含义 -
根据我上面上传的照片,你有什么解决方案来解决这个错误?
-
解决方案是创建变量
text,其中包含您要在wordcloud 中显示的单词——即。text = "Hello World of Word Cloud
标签: python python-3.x word-cloud wordcloud2