【问题标题】:How to make run a code with a wordcloud error如何使用 wordcloud 错误运行代码
【发布时间】:2019-09-10 20:35:43
【问题描述】:

我正在尝试从 Python 中的数据框创建 wordcloud,但是当我尝试运行代码时,它给了我一条错误消息,如下所示:NameError: name 'text' is not defined。 数据框由从当地报纸上抓取的数据组成,我想做的是用最常提到的单词制作一个 wordcloud。

数据框是这样的:

enter image description here

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


【解决方案1】:

您的代码的“文本”未定义。 试试这个方法。

text = ''

for i in range(len(df)):
    text += df.iloc[i,2]

.generate(df.text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2021-10-12
    • 2021-10-18
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多