How do I group a dataframe by multiple columns simultaneously

Question:

I have a dataframe that looks something like this:

Individual Category Amount Extras
A              1      250    30
A              1      300    10
A              1      500    8
A              2      350    12
B              1      200    9
B              2      300    20
B              2      450    15

I want to get a dataframe that looks like this:

Individual Category Count Amount Extras
A             1       3    1050    48
A             2       1     350    12
B             1       1     200     9
B             2       2     750    35

I know that you can use groupby with Pandas, but is it possible to group using count and sum simultaneously?

Asked By: Vuotelin

||

Answers:

You could try as follows:

output_df = df.groupby(['Individual','Category']).agg(
    Count=('Individual', 'count'),
    Amount=('Amount','sum'),
    Extras=('Extras','sum')).reset_index(drop=False)

print(output_df)

  Individual  Category  Count  Amount  Extras
0          A         1      3    1050      48
1          A         2      1     350      12
2          B         1      1     200       9
3          B         2      2     750      35

So, we are using df.groupby, and then apply named aggregation, allowing us to "[name] output columns when applying multiple aggregation functions to specific columns".

Answered By: ouroboros1
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.