Group several columns then aggregate a set of columns in Pandas (It crashes badly compared to R's data.table) Group several columns then aggregate a set of columns in Pandas (It crashes badly compared to R's data.table) pandas pandas

Group several columns then aggregate a set of columns in Pandas (It crashes badly compared to R's data.table)


I think what you're looking for is agg instead of apply. You can pass a dict mapping columns to the functions you want to apply, so I think this would work for you:

ddata = ddata.groupby(group_column_list).agg({    'ANDroid_Margin'      : sum,    'Margins'             : sum,    'ANDroid_App_Qty'     : sum,    'Apple_Margin'        : sum,    'P_Lic'               : sum,    'Cust_ANDroid_Margins': 'mean',    'Cust_Mobile_Margins' : 'mean',    'Cust_ANDroid_App_Qty': 'mean'})