【问题标题】:Pandas groupby count one column against the other columnPandas groupby 将一列与另一列进行计数
【发布时间】:2021-01-05 12:12:33
【问题描述】:

我有这个包含 1100 万行的数据框:

我想使用'user_id' 列计算有多少用户发布了相同数量的推文并绘制直方图(y 轴:用户数,x 轴:推文数)。

我试过这个:

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

这行不通。有人可以帮忙吗?谢谢。

【问题讨论】:

  • 您能否提供您期望的典型输出作为表格和图表。

标签: python pandas dataframe pandas-groupby


【解决方案1】:

看看下面的内容是否适合你。根据需要使用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()

【讨论】:

    猜你喜欢
    • 2018-03-01
    • 1970-01-01
    • 2019-08-03
    • 2021-10-30
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    相关资源
    最近更新 更多