熊猫系列中的 wordlcoud

问题描述

我想从 Pandas 数据框创建一个 wordcloud,但只能从一列“完成这句话:我买刀是为了……(收集、狩猎、娱乐、安全……等)。

enter image description here

出于这个原因,我创建了一个单独的 Pandas 系列“moreuses”,它只存储来自该列(第 4 号)的值:

moreuses = dataset.iloc[:,4]
moreuses

输出

0                                      EDC and Outdoors 
1                                    Fishing and hunting
2             Hunting,safety,fun,and for everyday use
3          Purely for use,not for display or collecting
4                                                   fun!
                             ...                        
138                  Everyday carry,cutting misc things
139                                              fun/edc
140                         Utility. Fun,and Collection
141    playing with it like a toy. I'm not really nee...
142    Work,play,defense and somthing beautiful to ...
Name: Finish the sentence: I buy knifes for...  (collection,hunting,...,etc. ),Length: 143,dtype: object

现在我想用这些词创建一个 wordcloud,但出现错误

from wordcloud import WordCloud

# Read the whole text.
text = moreuses

# Generate a word cloud image
wordcloud = WordCloud().generate(text)

# display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt


# take relative word frequencies into account,lower max_font_size
wordcloud = WordCloud(background_color="white",max_words=len(s),max_font_size=40,relative_scaling=.5).generate(text)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()

错误:类型错误:预期的字符串或类似字节的对象

现在我已经尝试从这个stackoverflow帖子中实现解决方案: Generate word cloud from single-column Pandas dataframe

但是我没有从建议的代码中得到任何输出

wordcloud2 = WordCloud().generate(' '.join(moreuses)

我相信是因为在这个例子中只有一个词?但是,我似乎无法弄清楚问题所在……非常感谢您的帮助。

解决方法

首先把你的专栏变成一个列表

words = list(moreuses.values())

然后加入值

string_of_words = " ".join(words)

然后生成你的词云

wordcloud = WordCloud().generate(string_of_words)

注意:未经测试,因为没有给出样本数据。