wordcloud for a csv file in python

2019-08-06 03:13发布

i have a csv file with 2 columns (dataframe) column 1 contains a sentence i love banana

and column 2 contains a classe i have 5 classes

i need a wordcloud for each class in fact each all the senetences corresponding to each classe it possible to do it ? it try this code but id does not working

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()

example of datasets

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

1条回答
老娘就宠你
2楼-- · 2019-08-06 04:10

Here is an example for one class: positive.

Assuming we have the following 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

Solution:

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 --

Result:

enter image description here

Some explanations:

generated text for a single class:

In [81]: df.loc[df.classe == 'positive', 'text'].str.cat(sep='\n')
Out[81]: 'i love banana\nlove apple\nlove, love, love'
查看更多
登录 后发表回答