I have a dataframe with 3 columns, product_id, user_city, purchase (0 or 1) to see if the customer purchased or not the item. I want to calculate the percentage of purchase by city For example, if my dataframe looks like this:
product_id user_city purchase
1 NYC 0
2 NYC 1
3 LA 1
4 LA 1
5 LA 1
6 LA 0
here I would get:
perc_purchase
NYC 0.5
LA 0.75
I have used groupedby with size() but this only gives me a count of 0 and 1 by city, no percentage. Please, help!!