Home > Enterprise >  Dynamic column naming during groupby agg
Dynamic column naming during groupby agg

Time:10-20

I want to perform a general version of

df.groupby([columns]).agg(new_name1=('col1':'min'),
                          new_name2=('col1':'max'),
                          new_name3=('col2':'mean'))

I would like to be able to specify the new column names dynamically so that this function is general. ie -

d = {
     'new_name1':('col1':'min'), 
     'new_name2':('col1':'max'),
     'new_name3':('col2':'mean')
    }

And then use this dictionary in the agg, doing something like

groupby([columns]).agg(d)

Currently all I can do is

groupby([columns]).agg({'col1':['min', 'max'] 'col2':['mean']}).reset_index()

But that returns a dataframe where the aggregations down a level and I can't rename them.

CodePudding user response:

You can use ** to unpack dictionary and there is no syntax like ('col1':'min')

d = {
     'new_name1':('col1','min'), 
     'new_name2':('col1','max'),
     'new_name3':('col2','mean')
}


groupby([columns]).agg(**d)

CodePudding user response:

    str1="df1.groupby('School').agg({})"

str2="new_name1=('Height','min'),new_name2=('Height','max'),new_name3=('Weight','mean')"#build as your need
eval(str1.format(str2))
  • Related