I know that in pandas, I can do something like this, where I apply multiple aggregations to the same column:
import pandas as pd
df = pd.DataFrame({'id':[1,1,2,2], 'x1':[0,1,0,1], 'x2':[1,0,1,0],'x3':[0,1,0,1], 'x4':[1,0,1,0]})
df.groupby('id').agg({'x1':['sum', 'max'], 'x2':['sum','max']})
Is there a syntax shortcut to do a similar thing, except this time apply the same aggregation to multiple columns? However, I am also looking to perform more than one type of aggregation.
Valid Syntax Example
df.groupby('id').agg({'x1':sum, 'x2':sum, 'x3':mean, 'x4':mean})
Desired Outcome Example
df.groupby('id').agg({['x1', 'x2']:sum, ['x3', 'x4']:mean})
I know this isn't a valid key-value pair, but hopefully illustrates what I'm aiming for. As to why I want to do this, my current aggregation statement is getting long and I am looking for ways to shorten it.