【发布时间】:2022-01-25 16:50:47
【问题描述】:
我有一个空数据框,我一次向它附加一个数据。像这样:
# Initialize an empty dataframe to store the tweet id and sentiment
tweets = pd.DataFrame(columns=['tweet_id', 'sentiment'])
tweet_id 是一个整数,sentiment 是一个可以有 3 个值的字符串,即。 “正面”、“负面”、“中性”。现在我有一个附加到数据框的循环:
for i in range(len(whatever_I_want)):
tweet = get_new_tweet()
tweets = tweets.append({'tweet_id': tweet['id'], 'sentiment': tweet['sentiment']}, ignore_index=True)
# Update the plot with the new dataframe
tweets.groupby('sentiment').count()['tweet_id'].plot.bar()
plt.show()
这是有效的,但它正在创建多个图,我需要关闭一个才能查看另一个。每当我运行循环时,我都希望更新相同的情节。我该怎么做?我搜索了它,我得到了使用 numpy 附加或显示折线图的解决方案。
如何使用 pandas groupby() 实现这一目标?
【问题讨论】:
标签: python pandas matplotlib