Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas rename columns in function using chained operations

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading