简体   繁体   中英

Pandas groupby used with agg doesn't return key columns

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:

在此处输入图像描述

But I'm getting the following:

在此处输入图像描述

Any known bug and workaround for this issue?

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

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()

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