python打开文件要wordcloud吗_wordcloud用于python中的csv文件

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

解决方案

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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值