wordcloud用于python中的csv文件 [英] wordcloud for a csv file in python
本文介绍了wordcloud用于python中的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有2列的CSV文件(数据框) 第1列包含一个我爱香蕉的句子
i have a csv file with 2 columns (dataframe) column 1 contains a sentence i love banana
第2列包含我具有5 classes
每堂课我都需要一个wordcloud 其实每一个班级对应的所有智慧都可以做到吗? 它尝试使用此代码,但id不起作用
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()
数据集示例
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
.
Here is an example for one class: 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 --
结果:
一些解释:
为单个class
生成的文本:
In [81]: df.loc[df.classe == 'positive', 'text'].str.cat(sep='\n')
Out[81]: 'i love banana\nlove apple\nlove, love, love'
这篇关于wordcloud用于python中的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文