简体   繁体   中英

Grouping rows and adding average of group values to Dataframe

I have a dataframe where I need to create a grouping of ages and then have the averages amount of Tip amount for each group.

My Data looks the following

Tip amount Age
3 30
30 35
4 60
1 12
7 25
3 45
15 31
5 8

I have tried to use pd.cut() with bins to create the grouping, but I can't seem to get the Tip amount average (maybe using mean() ) to be in the DataFrame as well.

import pandas as pd

bins= [0,15,30,45,60,85]
labels = ['0-14','15-29','30-44','45-59','60+']

df['Tip amount']=df['Tip amount'].astype(int)
#df = df.groupby('Age')[['Tip amount']].mean()
df = df.groupby(pd.cut(df['Age'], bins=bins, labels=labels, right=False)).size()

This gives the following output:

Age
0-14 2
15-29 1
30-44 3
45-59 1
60+ 1

But I would like to have the average Tip amount for the groups as well.

Age Tip amount
0-14 2 avg
15-29 1 avg
30-44 3 avg
45-59 1 avg
60+ 1 avg

Try:

df.groupby(pd.cut(df['Age'], bins=bins, labels=labels, right=False)).agg({'Age': ['size'], 'Tip amount': ['mean']})

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