Skip to content

Instantly share code, notes, and snippets.

@achinta
Last active April 1, 2019 04:15
Show Gist options
  • Save achinta/a91877c0821364bfcc8a06f16fbe411d to your computer and use it in GitHub Desktop.
Save achinta/a91877c0821364bfcc8a06f16fbe411d to your computer and use it in GitHub Desktop.
Pandas apply multiple aggregations to multiple columns and rename columns appropriately
"""
Groupby, apply aggregations and rename columns for a dataframe. When we apply multiple aggregations to a df,
two level column names are created. Instead, this method returns a dataframe with format 'colname_agg'
example:
df = pd.DataFrame({'A': [1, 1, 1, 2, 2],
'B': range(5),
'C': range(5)})
groupByCols = ['A']
agg = {'B': ['sum','mean'], 'C': 'min'}
agg_df(groupByCols,agg) returns column names [B_sum,B_mean,C_min]
"""
def agg_df(df, groupByCols, aggs):
dfg = df.groupby(groupByCols).agg(aggs)
dfg.columns = ["_".join(x) for x in dfg.columns.ravel()]
return dfg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment