Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordCloud from data frame with frequency python

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

like image 519
Edward Avatar asked Jul 19 '16 18:07

Edward


People also ask

How do you find the frequency of a word in Python?

Use set() method to remove a duplicate and to give a set of unique words. Iterate over the set and use count function (i.e. string. count(newstring[iteration])) to find the frequency of word at each iteration.

What is Stopwords in Wordcloud?

From the wordcloud documentation: stopwords : set of strings or None. The words that will be eliminated. If None, the build-in STOPWORDS list will be used.


1 Answers

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

like image 187
Ricardo M S Avatar answered Oct 06 '22 11:10

Ricardo M S