如何在Python中创建不同组的词云?

时间:2019-06-12 02:39:32

标签: python python-3.x word-cloud

我有以下数据,我正在尝试为每个“人”(即1,2,3)创建wordcloud。我正在使用下面的代码创建wordcloud,但是我不确定如何为每个组创建词云。 注意:我是python新手,请根据需要提供解释和参考。

数据集:

import pandas as pd
data = {'Person':['1', '1','1','2','2','2','2','3','3'],'Response':['I like to eat','You have nice day','My name is ','I like to eat','You have nice day','My name is','This is it','I like to eat','You have nice day'],
       }
df = pd.DataFrame(data)

Wordcloud的代码

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
stopwords = set(STOPWORDS)

def show_wordcloud(data, title = None):
    wordcloud = WordCloud(
        background_color='white',
        stopwords=stopwords,
        max_words=200,
        max_font_size=40, 
        scale=3,
        random_state=1 # chosen at random by flipping a coin; it was heads
    ).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.show()

show_wordcloud(data['Response'])

1 个答案:

答案 0 :(得分:0)

这应该可以,但是会增加一个我不确定的输出:

df.groupby('Person').apply(
    lambda x: show_wordcloud(x.Response.tolist(), title=f"Person {x.name}")
)