How to Apply different aggregations for different columns

data_mining
aggregation
python

#1

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"]) 
tempstatecity["Customer_ID","Buy_Times","Customer_Value"].agg({"CountOfCustomer":"count","AvgCountOfPurchases":"avg","AvgOfPurchaseValue":"avg"})

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"])
tempstatecity[["Customer_ID","Buy_Times","Customer_Value"]].agg(["count","sum"])

Output will be like:
image

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


#2

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()}))

#3

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.


#4

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

I don’t get what you mean by

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


#5

.agg fn is depreciated in python 3