wordcloud for a csv file in python

被刻印的时光 ゝ 提交于 2019-12-13 17:25:15

问题


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:


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:

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'


来源:https://stackoverflow.com/questions/42193013/wordcloud-for-a-csv-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!