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

问题描述 投票:0回答:2

我有以下数据,我正在尝试为每个“人”(即1、2、3)创建词云。我使用下面的代码来创建词云,但我不确定如何为每个组创建词云。 注意:我是 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'])
python python-3.x word-cloud
2个回答
0
投票

这应该可行,但添加了一个我不确定的额外输出:

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

0
投票

对类别运行 for 循环:

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

stopwords = set(STOPWORDS)

for val in df.Person.unique():
    wordcloud = WordCloud(background_color ='white',stopwords = stopwords, max_words=100)
    wordcloud.generate(df. Response[(df.Response.notnull()) & (df.Person == val)].to_string())
    plt.imshow(wordcloud)
    plt.title(val)
    plt.axis("off")
    plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.