WordCloud from data frame with frequency python

痴心易碎 提交于 2019-12-06 05:48:28

问题


i have a dataframe as bellow

Int64Index: 14830 entries, 25791 to 10668
Data columns (total 2 columns):
word    14830 non-null object
coef    14830 non-null float64
dtypes: float64(1), object(1)

i try to make word cloud with coef as a frequency instead count for ample

text = df['word']
WordCloud.generate_from_text(text)
TypeError: generate_from_text() missing 1 required positional argument: 'text'

or

text = np.array(df['word'])
WordCloud.generate_from_text(text)
TypeError: generate_from_text() missing 1 required positional argument: 'text'

How can i improve this code & made word cloud like this

from wordcloud import WordCloud
wordcloud = WordCloud( ranks_only= frequency).generate(text)
plt.imshow(wordcloud)
plt.axis('off')
plt.show()

thanks


回答1:


For me it worked creating a dictionary, like this:

d = {}
for a, x in bag.values:
    d[a] = x

import matplotlib.pyplot as plt
from wordcloud import WordCloud

wordcloud = WordCloud()
wordcloud.generate_from_frequencies(frequencies=d)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()

where bag is a pandas DataFrame with columns words and counts




回答2:


first we get list of tuples

tuples = [tuple(x) for x in df.values]

then

wordcloud = WordCloud().generate_from_frequencies(tuples)

that's all



来源:https://stackoverflow.com/questions/38465478/wordcloud-from-data-frame-with-frequency-python

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