Dynamic column naming during groupby agg

Question:

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.

Asked By: thefrollickingnerd

||

Answers:

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)
Answered By: Ynjxsjmh
    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))
Answered By: G.G
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.