daiyue
daiyue

Reputation: 7448

Apply multiple operations on same columns after groupby

I have the following df,

id    year_month    amount
10    201901        10
10    201901        20
10    201901        30
20    201902        40
20    201902        20

I want to groupby id and year-month and then get the group size and sum of amount,

df.groupby(['id', 'year_month'], as_index=False)['amount'].sum()

df.groupby(['id', 'year_month'], as_index=False).size().reset_index(name='count')

I am wondering how to do it at the same time in one line;

id    year_month    amount    count
10    201901        60        3
20    201902        60        2

Upvotes: 1

Views: 178

Answers (1)

Mohit Motwani
Mohit Motwani

Reputation: 4792

Use agg:

df.groupby(['id', 'year_month']).agg({'amount': ['count', 'sum']})


                    amount
                   count    sum
id  year_month      
10  201901          3       60
20  201902          2       60

If you want to remove the multi-index, use MultiIndex.droplevel:

s = df.groupby(['id', 'year_month']).agg({'amount': ['count', 'sum']}).rename(columns ={'sum': 'amount'})
s.columns = s.columns.droplevel(level=0)
s.reset_index()

    id  year_month  count   amount
0   10  201901        3      60
1   20  201902        2      60

Upvotes: 5

Related Questions