简体   繁体   中英

PANDAS groupby 2 columns then count and mean

I have a data frame of users and each time they entered a website, it looks like this:

(if there are x row with same week and date it means the user entered the site x time that date).

ID week date
1 2 20/07/21
2 3 23/07/21
2 3 23/07/21
2 3 26/07/21
2 4 30/07/21
2 4 30/07/21
2 4 30/07/21
2 4 31/07/21

so far I've managed to do this:

ID week date days number
1 2 20/07/21 1
2 3 23/07/21 2
2 3 26/07/21 1
2 4 30/07/21 3
2 4 31/07/21 1

using this code:

df.groupby(['ID','week','date']).agg({'date':['count']})

but I need to calculate the mean times each user used the site by week, so each user has a row for each week. Therefor the output I need looks like this:

ID week mean days number
1 2 1
2 3 1.5
2 4 2

Any ideas how to continue?

Thanks!!

Use:

(df.groupby(['ID', 'week', 'date'], as_index=False)['date']
 .agg('count')
 .groupby(['ID', 'week'], as_index=False)
 .agg(**{'mean days number': ('date', 'mean')})
)

Output:

   ID  week  mean days number
0   1     2               1.0
1   2     3               1.5
2   2     4               2.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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