Home > Software engineering >  Pandas rename columns in function using chained operations
Pandas rename columns in function using chained operations

Time:07-22

Given the folling dataframe:

df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',
...                               'Parrot', 'Parrot'],
...                    'Max Speed': [380., 370., 24., 26.]})
>>> df

    Animal  Max Speed
0   Falcon  380.0
1   Falcon  370.0
2   Parrot  24.0
3   Parrot  26.0

and this function which is used to groupby:

def summary_df_grpby(df_):

    return df_.groupby(["Animal"]).agg({"Max Speed": ["max", "min"]})

if I want to change the column names I can use:

cols = ["animal_max_speed","animal_min_speed"]

grpd = summary_df_grpby(df)

grpd.columns = cols

I would like to capture this in my function though and continue to chain operations.

something like:

def summary_df_grpby(df_):

    return df_.groupby(["Animal"]).agg({"Max Speed": ["max", "min"]}).rename(columns=cols)

this however returns TypeError: 'list' object is not callable

CodePudding user response:

Use pandas' set_axis function to do the renaming in a chained operation :

def summary_df_grpby(df_):

    return df_.groupby(["Animal"]).agg({"Max Speed": ["max", "min"]}).set_axis(cols, axis = 1)

summary_df_grpby(df)

        animal_max_speed  animal_min_speed
Animal
Falcon             380.0             370.0
Parrot              26.0              24.0

CodePudding user response:

Just offering a different solution using the pd.NamedAgg syntax:

def summary_df_grpby(df_):

    return df_.groupby(["Animal"]).agg(animal_max_speed=('Max Speed', 'max'),
                                       animal_min_speed=('Max Speed', 'min'))
summary_df_grpby(df)

Output:

       animal_max_speed  animal_min_speed
Animal                                    
Falcon             380.0             370.0
Parrot              26.0              24.0
  • Related