Home > Software engineering >  Pandas - get median of the daily sum
Pandas - get median of the daily sum

Time:09-15

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.

CodePudding user response:

use .agg:

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

CodePudding user response:

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()
  • Related