Skip to content
Advertisement

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)?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement