首页 > 解决方案 > 如何为 Pandas Dataframe 中的每一列绘制 Wordlcoud?

问题描述

我有一个带有 N 行的熊猫数据框。

每行 N 有很多单词。

我正在尝试为每一列绘制 wordloud。

东风:

    prediction                    text
0           1  [menu, food, food and chicken...
1           2  [transfer, money, transfer, transfer, trans...
2           0  [cold,cold,cold,hot,love...

代码

import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS

def show_wordcloud(data, title):

        wordcloud = WordCloud(
        background_color='white',
        max_words=200,
        max_font_size=40,
        scale=3,
    ).generate(str(data))


    fig = plt.figure(1, figsize=(12, 12))
    plt.axis('off')

    if title:
        fig.suptitle(title, fontsize=20)
        fig.subplots_adjust(top=2.3)


    plt.imshow(wordcloud)
    plt.savefig('cluster{}.png'.format(title))
    plt.show()


for row in df.iterrows():
    show_wordcloud(row['text'], row["prediction"])

但我得到的是除 lat 之外的所有列的空白图像。

我该如何解决?

谢谢

标签: pythonpandasmatplotlibimshowword-cloud

解决方案


推荐阅读