简体   繁体   English

Pandas - 获取每日总和的中位数

[英]Pandas - get median of the daily sum

I have a such line of code:我有这样一行代码:

df_append.groupby(['user_id', 'date'])['money'].sum()

It gives me such results:它给了我这样的结果:

user_id      date
1           20210701          7169.21
            20210702          7988.33
            20210703          7326.52
            20210704          6281.38
            20210705          5561.10
                               ...   
1031536     20220626          5162.35
            20220627          4522.90
            20220628          5028.58
            20220629          5694.28
            20220630          6487.43

But now I want a median value for all those dates.但现在我想要所有这些日期的中值。 I am not really sure how to do it.我不确定该怎么做。 I tried.median() after.sum() but it gives me one value instead of one median value for a specific user.我在.sum() 之后尝试了.median(),但它给了我一个值,而不是一个特定用户的中值。

use .agg :使用.agg

df_append.groupby(['user_id', 'date'])['money'].agg(['sum', 'median']).droplevel(0, axis=1)

This was the result that I wanted to achieve - figured it out:这是我想要达到的结果 - 弄清楚了:

transactions_df['sum median daily'] = (df_append.groupby(['user_id', 'date'])['money'].sum().to_frame(name = 'sum').reset_index()).groupby('user_id')['sum'].median()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM