来自频率为 python 的数据框的 WordCloud

WordCloud from data frame with frequency python

我有一个数据框如下

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)

我尝试用系数作为频率而不是计数来制作词云 充足

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

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

我怎样才能改进这段代码并制作这样的词云

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

谢谢

对我来说,创建字典很有效,就像这样:

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()

其中 bag 是一个 pandas DataFrame,包含列 wordscounts

首先我们得到元组列表

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

然后

wordcloud = WordCloud().generate_from_frequencies(dict(tuples))

就这些了