Finding the proportion of duplicates to non-duplicates in a column

Viewed 26

Imagine I have a dataset that is like so:

         cust_id       prod_title            total_sales    trans_timestamp
0        1001015       All Veggie Yummies    72.99           2021-03-30 10:06:05.108653 
1        1001019       Ball and String       18.95           2021-03-30 10:07:01.746945 
2        1022098       Cat Cave              28.45           2021-03-30 10:10:41.387170    
3        1022157       Chewie Dental         24.95           2021-03-30 10:20:11.571311 
4        1022189       Chomp-a Plush         60.99           2021-03-30 10:33:11.289467 
5        1002664       Feline Fix Mix        65.99           2021-03-30 10:37:55.446798      
6        1002666       Fetch Blaster         9.95            2021-03-30 10:39:36.488829 
7        1002175       Foozy Mouse           45.99           2021-03-30 10:43:21.490817
8        1002666       Kitty Climber         35.99           2021-03-30 10:51:31.510563      
9        1022189       Purr Mix              32.99           2021-03-30 11:01:50.082440 
10       1011924       Fetch Blaster         19.90           2021-03-30 11:11:15.944726 
11       1022236       Purr Mix              98.97           2021-03-30 11:15:39.390134     
12       1022189       Cat Cave              56.90           2021-03-30 11:21:10.167505 
13       1002137       Purrfect Puree        54.95           2021-03-30 11:27:51.133318 
14       1002159       Foozy Mouse           91.98           2021-03-30 11:29:00.292890 
15       1002175       Reddy Beddy           21.95           2021-03-30 11:30:22.928818      
16       1002108       Cat Cave              85.83           2021-03-30 11:34:35.776578 
17       1002186       Scratchy Post         48.95           2021-03-30 10:39:28.604007 
18       1002175       Snack-em Fish         15.99           2021-03-30 10:39:40.349882      
19       1002261       Snoozer Essentails    99.95           2021-03-30 10:51:31.510563 
20       1002666       Scratchy Post         48.95           2021-03-30 11:01:28.024109 
21       1002159       Purrfect Puree        219.80          2021-03-30 11:11:15.944726     
22       1002680       Chewie Dental         49.90           2021-03-30 11:15:39.390134 
23       1002678       Reddy Beddy           65.85           2021-03-30 11:21:10.167505 
24       1013769       The New Bone          71.96           2021-03-30 11:27:17.147159      
25       1013859       Reddy Beddy           109.75          2021-03-30 11:28:48.669564 

What is the proportion of returning customers? Store as variable prop_returning.

I'm guessing it's wanting to find the proportion based on the cust_id column, I'm not sure how to get rid of the non-duplicates and then get a value from the percentage that is returning vs non returning. I've set up a separate dataframe called duplicate and used the count_values command to get the length of duplicates, and than compare that to the length of the normal cust_id length like this:

duplicate = df_cleaned[df_cleaned.duplicated('cust_id')]
duplicate['cust_id'].value_counts()

df_cleaned['cust_id'].value_counts()

Even then though I just see the length defined on the bottom, not being an accessible value or variable to throw into a function to get the proportion.

1 Answers

This delivers the proportion of unique customers that are returning customers:

df['cust_id'].value_counts().gt(1).mean()

Result

0.21052631578947367
Related