python中csv文件的wordcloud

python中csv文件的wordcloud,python,csv,pandas,classification,word-cloud,Python,Csv,Pandas,Classification,Word Cloud,我有一个包含两列的csv文件(数据帧) 第一栏有一句话“我爱香蕉” 第2列包含一个类,我有5个类 每堂课我都需要一个wordcloud 事实上,每一个类别对应的所有意义都有可能做到吗? 它尝试此代码,但id不起作用 import matplotlib.pyplot as plt cloud = WordCloud(background_color="white", max_words=20, stopwords=stopwords) tuples = tuple([tuple(x) for x

我有一个包含两列的csv文件(数据帧) 第一栏有一句话“我爱香蕉”

第2列包含一个类,我有
5个类

每堂课我都需要一个wordcloud 事实上,每一个类别对应的所有意义都有可能做到吗? 它尝试此代码,但id不起作用

import matplotlib.pyplot as plt
cloud = WordCloud(background_color="white", max_words=20, stopwords=stopwords)
tuples = tuple([tuple(x) for x in df.Phrase.value_counts().reset_index().values])
a = cloud.generate_from_frequencies(tuples)

plt.imshow(a)
plt.axis("off")
plt.title("a")
plt.show()
数据集示例

text                           classe
i love banana                 positive 
i hate banana                 negetive
maybe i love maybe no         neutral
bit yes bit no                not_sure
wooooooooooow                 like_it

下面是一个类的示例:
positive

假设我们有以下DF:

In [79]: df
Out[79]:
                    text    classe
0          i love banana  positive
1             love apple  positive
2       love, love, love  positive
3          i hate banana  negative
4               it sucks  negative
5  maybe i love maybe no   neutral
6         bit yes bit no  not_sure
7          wooooooooooow   like_it
解决方案:

In [80]: %paste
from wordcloud import WordCloud
from nltk.corpus import stopwords

cloud = WordCloud(background_color="white", max_words=20, stopwords=stopwords.words('english'))

positive_cloud = cloud.generate(df.loc[df.classe == 'positive', 'text'].str.cat(sep='\n'))
plt.figure()
plt.imshow(positive_cloud)
plt.axis("off")
plt.show()
## -- End pasted text --
结果:

一些解释:

为单个
类生成的文本

In [81]: df.loc[df.classe == 'positive', 'text'].str.cat(sep='\n')
Out[81]: 'i love banana\nlove apple\nlove, love, love'

@MaxU是的,我修改了description@MaxU是的,我修改了描述,我注意到了。我目前正在学习
wordcloud
是如何工作的…;)如果我理解正确的话,你想要5张图片(云)——每堂课一张,对吗?@MaxU这正是我需要的