How to get the sub-columns from a pandas DataFrame after a .groupby() and .agg()?

26 Views Asked by At

I'm attempting to perform a groupby and aggregate operation on a Pandas DataFrame. Specifically, I want to compute the mean and count for each class group. However, I'm encountering issues accessing the generated columns.

Here's an example of the transformation I'm aiming for:

import pandas as pd

df = pd.DataFrame({
    'Class': ['A', 'B', 'A', 'B', 'C'],
    'Val': [25, 30, 35, 40, 15],
})

grouped = df.groupby(by='Class').agg({'Val': ['mean', 'count']})

The result I obtain is as follows:

    Val
         mean  count
Class
A        30.0      2
B        35.0      2
C        15.0      1

However, I want to get rid of the "Val" sub-column to achieve this data structure:

Class   mean  count
A       30.0      2
B       35.0      2
C       15.0      1
1

There are 1 best solutions below

0
mozway On BEST ANSWER

You should slice before agg:

grouped = df.groupby(by='Class', as_index=False)['Val'].agg(['mean', 'count'])

Output:

  Class  mean  count
0     A  30.0      2
1     B  35.0      2
2     C  15.0      1