Sub topic 4
Building a cohort retention table
This chapter assumes you know what a cohort is and how you define an active event and a retention rule (earlier chapters in this tutorial). It also assumes basic pandas. Here you turn raw events into a table that the next chapters read for shape and trend.
What the table holds
A cohort retention table has one row per cohort and one column per age. A cohort is the group of users whose first event fell in the same month. Age is the number of months since that first month. Each cell is the share of the cohort that was active at that age.
Age 0 is the cohort's first month. Every user in the cohort is active then by definition, so age 0 is always 100%. It is also the denominator for every other cell in the row.
The recipe
Four steps take you from events to table.
Worked example
The script below uses ten events from five users.
import numpy as np
import pandas as pd
events = pd.DataFrame({
"user_id": [1, 1, 1, 2, 2, 3, 3, 4, 5, 5],
"ts": pd.to_datetime([
"2026-01-05", "2026-02-10", "2026-03-03",
"2026-01-20", "2026-02-15",
"2026-02-02", "2026-03-12",
"2026-02-18",
"2026-03-05", "2026-03-20",
]),
})
events["month"] = events["ts"].dt.to_period("M")
events["cohort"] = events.groupby("user_id")["month"].transform("min")
events["age"] = (
(events["month"].dt.year - events["cohort"].dt.year) * 12
+ (events["month"].dt.month - events["cohort"].dt.month)
)
active = (
events.groupby(["cohort", "age"])["user_id"]
.nunique()
.unstack(fill_value=0)
)
retention = active.div(active[0], axis=0)
print(retention.round(2))The output is:
age 0 1 2
cohort
2026-01 1.0 1.0 0.5
2026-02 1.0 0.5 0.0
2026-03 1.0 0.0 0.0Each step does one job:
to_period("M")turns a timestamp into a calendar month.groupby("user_id")["month"].transform("min")finds each user's first month and copies it onto every one of that user's events.- The
ageformula subtracts months across year boundaries correctly, because it multiplies the year difference by 12. nunique()counts each user once per cell, even if they had several events in a month. Usingcount()here would count events and inflate retention.active.div(active[0], axis=0)divides each row by its own age 0 value.
Reading the first result
The January cohort has two users. Both returned in February, and one was still active in March, so its row reads 100% then 50%. The February cohort retains 50% at age 1.
The zeros in the lower right are a trap. The data ends in March 2026. The February cohort has not yet had an age 2, and the March cohort has not had an age 1. Those cells were never observed, but fill_value=0 shows them as 0% retention.
Common errors
| Symptom | Likely cause |
|---|---|
| Retention above 100% | Counting events instead of distinct users, or dividing by the wrong column |
| Age 0 below 100% | Cohort defined by a different event than the one counted as active |
| Ages off by one year boundary | Subtracting month numbers without the year term |
| Last diagonal looks like a collapse | Incomplete periods filled with zeros |
Check two things on any new table. Age 0 should be exactly 1.0 in every row. The cohort sizes should add up to your total distinct users.
Exercise
In the example, user 5 has two events in March 2026, on the 5th and the 20th. Why does the March cohort's age 0 count still equal 1, and what would go wrong if you replaced nunique() with count()?
Show answer
nunique() counts distinct user IDs, so user 5 counts once in the cell. With count(), user 5 would count twice, giving a March age 0 of 2. The ratio would still be 1.0 for that cell, but the denominators would be inflated for every cohort with repeat events. Cells at later ages could then exceed 100%, for example a January age 1 of 3 events divided by a size of 2 users.