How to Apply different aggregations for different columns



I want to different type of aggregations to by columns after grouping based on Certain Columns. i was able to achieve that using the

tempstatecity = customerdata.groupby(["Last_state","Last_city"]) 

but the use of .agg function depreciated recently, how to do different aggregations for different columns?

Recently i have found a way of aggregation but it won’t help me in applying different aggregations for different columns.

tempstatecity = customerdata.groupby(["Last_state","Last_city"])

Output will be like:

But i want to apply different aggregations like count for customer_id and sum for Buy_Times etc…,Any help in achieving this would be very helpful


Well there’s always lambda

temp = customerdata.groupby(["Last_state","Last_city"]) 
temp.apply(lambda x: pd.Series({'CustCount': x['Customer_ID'].count(), 'AvgPurchs':  x['Buy_Times'].sum()}))


Thanks! Super useful.

But i can’t reorder the columns, for example i want the ‘Count_Of_Customers’ Column to be my first column but it is automatically arranging in some order.

temp = customerdata.groupby(["Last_state","Last_city"]) 
temp = temp.apply(lambda x: pd.Series({'Count_Of_Customers':x['Customer_ID'].count(),'Average_Number_Purchases':x['Buy_Times'].mean(),'Average_Purchase_Value':x['Customer_Value'].mean()})).reset_index()

Even though i have written the ‘Count_Of_Customers’ Column in first it is not appearing as i specified in the table.


tempstatecity = customerdata.groupby(["Last_state","Last_city"], as_index=False).agg({"Customer_ID":"count","Buy_Times":"avg","Customer_Value":"avg"})

I don’t get what you mean by

Because i’m still able to use the above code and getting the desired output


.agg fn is depreciated in python 3