agg
with a dict
of functions
Create a dict
of functions and pass it to agg
. You’ll also need as_index=False
to prevent the group columns from becoming the index in your output.
f = {'NET_AMT': 'sum', 'QTY_SOLD': 'sum', 'UPC_DSC': 'first'}
df.groupby(['month', 'UPC_ID'], as_index=False).agg(f)
month UPC_ID UPC_DSC NET_AMT QTY_SOLD
0 2017.02 111 desc1 10 2
1 2017.02 222 desc2 15 3
2 2017.02 333 desc3 4 1
3 2017.03 111 desc1 25 5
Blanket sum
Just call sum
without any column names. This handles the numeric columns. For UPC_DSC
, you’ll need to handle it separately.
g = df.groupby(['month', 'UPC_ID'])
i = g.sum()
j = g[['UPC_DSC']].first()
pd.concat([i, j], 1).reset_index()
month UPC_ID QTY_SOLD NET_AMT UPC_DSC
0 2017.02 111 2 10 desc1
1 2017.02 222 3 15 desc2
2 2017.02 333 1 4 desc3
3 2017.03 111 5 25 desc1