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

Can we use iterables in pandas groupby agg function?

I have a pandas groupby function.
I have another input in the form of dict which has {column:aggfunc} structure as shown below:

d = {'production': 'sum',
     'Demand': 'first'}

I want to use this dict to apply aggregate function as follows:

df.groupby(['Month']).agg(production=pd.NamedAgg('production', aggfunc='sum'),
                          demand=pd.NamedAgg('Demand', aggfunc='first'))

Is there some way I can achieve this using the input dict d (may be by using dict comprehensions)?

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

>Solution :

If dictionary contains columns name and aggregate function pass it to GroupBy.agg, columns names are not changed:

df = pd.DataFrame({'Month': ['jan', 'jan', 'feb'],
                   'production':[1,5,9],
                   'Demand': list('abc')})

d = {'production': 'sum', 'Demand': 'first'}

df = df.groupby(['Month']).agg(d)
print (df)
       production Demand
Month                   
feb             9      c
jan             6      a

If need also set new columns names by named aggregation in dictionary use:

d = {'production123': ('production', 'sum'), 'demand':('Demand',  'first')}


df = df.groupby(['Month']).agg(**d)
print (df)
       production123 demand
Month                      
feb                9      c
jan                6      a
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