Cohort Analysis Using Python for Beginners: A Hands-On Tutorial
As a data scientist or analyst, you‘re often tasked with understanding user behavior and measuring the performance of products or features over time. One of the most powerful techniques for this is cohort analysis. In this tutorial, we‘ll walk through a hands-on example of conducting cohort analysis using Python, with a focus on analyzing user retention rates for a digital product.
What is Cohort Analysis?
Cohort analysis is a method of analyzing user behavior by segmenting users into groups, or cohorts, based on a common characteristic or experience within a defined time period. The most common type of cohort is an acquisition cohort, where users are grouped by the time period in which they first interacted with the product, such as signing up or making a first purchase.
By comparing the behavior of different cohorts over time, we can gain insights into how product changes or marketing efforts impact user engagement and retention. Cohort analysis helps cuts through the noise of overall growth metrics and isolates the effects of product changes on specific groups of users.
Some key benefits and use cases of cohort analysis include:
- Understanding user retention and churn rates
- Measuring the impact of product changes or feature releases
- Identifying high-value user segments
- Optimizing marketing channels and campaigns
- Forecasting future growth and revenue
Tutorial: Cohort Analysis in Python
Now let‘s dive into a practical example of performing cohort analysis in Python. We‘ll be using a sample dataset of user activity in a hypothetical digital product. Our goal will be to calculate monthly user retention rates and visualize them in an insightful way.
Step 1: Load and Prepare the Data
First, we‘ll load our user activity data using the pandas library and take a look at the structure:
import pandas as pd
df = pd.read_csv(‘user_activity.csv‘)
print(df.head())
user_id event_date
0 1 2022-01-15
1 2 2022-01-23
2 1 2022-02-03
3 3 2022-02-14
4 2 2022-03-09
Our data contains user IDs and event dates indicating when a user interacted with our product. To perform cohort analysis, we‘ll need to assign each user to an acquisition cohort based on their first event date.
But first, let‘s check for missing values and handle them appropriately:
print(df.isnull().sum())
df = df.dropna()
In this case, we‘ll simply drop any rows with missing values, but in practice you may want to use more sophisticated imputation methods.
Next, we‘ll extract the cohort month from each user‘s first event date. We can do this using pandas‘ to_datetime() function and the dt accessor:
df[‘cohort_month‘] = df.groupby(‘user_id‘)[‘event_date‘].transform(‘min‘)
df[‘cohort_month‘] = pd.to_datetime(df[‘cohort_month‘]).dt.to_period(‘M‘)
print(df.head())
user_id event_date cohort_month
0 1 2022-01-15 2022-01
1 2 2022-01-23 2022-01
2 1 2022-02-03 2022-01
3 3 2022-02-14 2022-02
4 2 2022-03-09 2022-01
We now have a cohort_month column indicating the first month each user was active.
Step 2: Assign Users to Cohorts and Calculate Retention
With our data prepared, we can assign each user to an acquisition cohort and calculate retention rates.
First, let‘s count the number of active users in each cohort and time period:
cohort_counts = pd.crosstab(df[‘cohort_month‘], pd.to_datetime(df[‘event_date‘]).dt.to_period(‘M‘))
print(cohort_counts)
event_date 2022-01 2022-02 2022-03
cohort_month
2022-01 67 34 25
2022-02 0 83 38
2022-03 0 0 62
This gives us a table of user counts for each cohort and month. The values on the diagonal represent the total number of users acquired in each cohort month.
To calculate retention, we simply divide each row by the first value in that row (which represents the total users acquired in that cohort month):
cohort_size = cohort_counts.iloc[:,0]
retention = cohort_counts.divide(cohort_size, axis=0)
print(retention)
event_date 2022-01 2022-02 2022-03
cohort_month
2022-01 1.0 0.507 0.373
2022-02 NaN 1.000 0.458
2022-03 NaN NaN 1.000
The resulting table shows monthly retention rates for each cohort, with 1.0 representing 100% of the cohort returning in the first month, and lower values in subsequent months indicating the percentage of the original cohort that returned.
Step 3: Visualize Cohort Retention
To better understand the retention patterns, let‘s visualize our results as a heatmap using seaborn:
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(10,8))
sns.heatmap(data=retention, annot=True, fmt=‘.0%‘, cmap=‘RdYlGn‘)
plt.title(‘Monthly User Retention by Acquisition Cohort‘)
plt.show()

In this heatmap, each row represents an acquisition cohort, each column represents a retention month, and the cell values show the retention rate as a percentage. Darker green colors indicate higher retention.
Interpreting Cohort Analysis Results
So what insights can we glean from this cohort analysis? A few key things jump out:
-
Retention rates decline over time for each cohort. This is a common pattern, as some users inevitably churn after trying out a product.
-
The January 2022 cohort had higher retention than later cohorts in the 2nd month (50.7% vs 45.8% for February). This suggests the January cohort found more initial value in the product.
-
By the 3rd month, 37.3% of the January cohort was still active, compared to an average of around 40% for comparable SaaS products. This indicates our retention is slightly below benchmarks.
Based on these insights, we might dig deeper into what made the January cohort stickier – were there specific marketing campaigns or product features that resonated? We may also want to survey churned users to understand why they didn‘t find ongoing value, and prioritize product changes to improve early retention.
Further Applications of Cohort Analysis
Beyond analyzing retention, cohort analysis is a versatile technique that can be applied to a variety of business questions, such as:
-
Revenue cohorts: Analyzing monthly revenue generated by each acquisition cohort can help measure the long-term value of marketing efforts. Revenue cohorts are also useful for revenue forecasting.
-
A/B testing: Comparing metrics for cohorts exposed to different product experiences or marketing messages can measure the impact of experiments and inform decisions.
-
Churn prediction: Modeling the likelihood of churn based on a user‘s cohort and other behavioral factors can help proactively intervene with at-risk users.
The possibilities are endless – cohort analysis is a general framework for understanding how groups of users evolve over time in relation to your product or service. As long as you have timestamps and a user identifier, you can define cohorts and track relevant metrics.
Conclusion
In this tutorial, we walked through the key steps of conducting cohort analysis in Python:
- Preparing and cleaning user activity data
- Extracting cohort time periods and assigning users to cohorts
- Calculating retention rates by cohort and time period
- Visualizing retention in a heatmap to identify patterns
- Interpreting results and deriving actionable insights
By segmenting users into cohorts and tracking their behavior over time, we can gain a deeper understanding of product performance and user engagement. Cohort analysis is an essential tool for data-driven decision making, allowing us to cut through the noise of topline metrics and focus on the factors that drive retention and growth.
To learn more, check out these additional resources:
- Cohort Analysis in Python Using Pandas
- The Ultimate Guide to Cohort Analysis
- Cohort Analysis That Helps You Look Ahead
Happy analyzing!