按一列对另一列进行计数

2024-09-30 20:19:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个1100万行的数据框:

Dataframe

我想用'user_id'列和柱状图(y轴:用户数,x轴:tweets数)统计有多少用户发布了相同数量的tweets

我试过这个:

user_tweet_df.groupby('tweet_count').count()

这行不通。有人能帮忙吗?多谢各位


Tags: 数据用户iddf数量counttweetstweet
1条回答
网友
1楼 · 发布于 2024-09-30 20:19:22

看看下面是否适合你。根据需要使用pandas docs on visualization自定义图形

import matplotlib.pyplot as plt
import pandas as pd
from tabulate import tabulate

tweets_df = pd.DataFrame({'user_id':[312,412,521,577,614,753,965,989],
                    'user_name':['Mary','Bob','Hans','Nicole','Chris','Matt','Carol','Khan'],
                    'tweet_count':[207,35,35,1,2,1,1,15]})
print(tabulate(tweets_df, headers='keys'), '\n')

grouped_df = tweets_df.groupby('tweet_count').count()[['user_id']]
print(tabulate(grouped_df, headers='keys'), '\n')

grouped_df.plot(kind='bar')
plt.show()

enter image description here

相关问题 更多 >