Calculate retention rate from scratch
You have seen how to create retention and average quantity metrics table for the monthly acquisition cohorts. Now it's you time to build the retention metrics by yourself.
The online
dataset has been loaded to you with monthly cohorts and cohort index assigned from this lesson. Feel free to print it in the Console.
Also, we have created a loaded a groupby
object as grouping
DataFrame with this command:
grouping = online.groupby(['CohortMonth', 'CohortIndex'])
This exercise is part of the course
Customer Segmentation in Python
Exercise instructions
- Select the customer ID column, count the number of unique values, store it as
cohort_data
, and reset its index. - Create a pivot with monthly cohort in the index, cohort index in the columns and the customer ID in the values.
- Select the first column and store it to
cohort_sizes
. - Divide the cohort count by cohort sizes along the rows.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Count the number of unique values per customer ID
cohort_data = grouping[____].apply(pd.Series.____).reset_index()
# Create a pivot
cohort_counts = cohort_data.____(index=____, columns=____, values=____)
# Select the first column and store it to cohort_sizes
cohort_sizes = cohort_counts.iloc[:,____]
# Divide the cohort count by cohort sizes along the rows
retention = cohort_counts.____(____, axis=____)