Home > Net >  Pandas groupby used with agg doesn't return key columns
Pandas groupby used with agg doesn't return key columns

Time:12-14

In a project I'm working into I'm forced to use Pandas version 1.1.5. I'm trying to do a group by operation in order to aggregate a variable using multiple functions:

import pandas as pd
import numpy as np

df = pd.DataFrame( { 
        "Name" : ["Alice", "Bob", "James", "Mallory", "Bob" , "Lucas", "Alice", "Bob", "James", "Mallory", "Bob" , "Lucas"] , 
        "Apples" : [22, 31, 35, 41, 27, 32, 64, 12, 59, 45, 65, 31] } )

apple_df = df.groupby('Name', as_index = False).agg(
    apple_avg = ('Apples', np.mean),
    apple_median = ('Apples', np.median),
    apple_count = ('Apples', np.count_nonzero)
)
apple_df

I'm expecting the Name column with the other aggregation variables as result like this:

enter image description here

But I'm getting the following:

enter image description here

Any known bug and workaround for this issue?

P.S. All works fine with Pandas 1.3.0, but I can't use it in this project.

CodePudding user response:

You could try to remove the as_index parameter and make a .reset_index() instead:

apple_df = df.groupby('Name').agg(
    apple_avg = ('Apples', np.mean),
    apple_median = ('Apples', np.median),
    apple_count = ('Apples', np.count_nonzero)
).reset_index()
  • Related