0

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!!

ansev
  • 30,322
  • 5
  • 17
  • 31
PandasKoala
  • 75
  • 1
  • 10

1 Answers1

0

You should do

df.groupby('user_city')['purchase'].mean().sort_values().to_frame('purchase')

This will give you

           purchase
user_city          
NYC            0.50
LA             0.75
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65