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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source