Segments and cohorts
Last updated
An average over all customers can hide the most important thing about them: some are loyal, some came once, some are drifting away. In this lesson you'll split the suki card holders into segments by how recently and how often they buy and how much they spend, follow each month's new members as a cohort to see how many keep buying, and find a shrinking group that the overall average hid.
By the end of this lesson you can
- Segment customers by recency, frequency and amount spent
- Build a cohort retention table and read it by rows and columns
- Check an overall average group by group
Segments: recency, frequency, amount
The owners launched the suki card in January 2024, and 580 people joined in the first six months. Two files record them: cards.csv (one row per card: store, barangay, the month it joined) and card_months.csv (one row per card per month it bought anything, with the visits and pesos).
A segment is a group of customers who behave alike. A classic way to segment, used by shops for decades, is RFM:
- Recency: how long since they last bought.
- Frequency: how often they buy.
- Monetary: how much they spend.
Run the cell: at the end of December 2024, each card holder's months since their last purchase, the months they bought in, and their total pesos, grouped into three segments with simple rules.
- Loyal (bought in December, in at least 6 months): 164 members, ₱3,821 each on average.
- Occasional (still buying, less often): 43.
- Lapsed (nothing for 3 months or more): 373, with only 3 months of buying on average.
The rules are choices (9.3): write them down. The useful question isn't "what's our average customer", but "how many loyal ones do we have, and how do we keep them".
Cohorts
Segments describe customers at one moment. To see how customers change over time, group them by when they started. A cohort is everyone who joined in the same month; following each cohort month by month shows how many keep buying. Run the cells.
months_sinceis how many months after joining each purchase was: 0 for the joining month.pivot_table(..., aggfunc="nunique")counts the different cards in each cohort and month since joining.counts.div(counts[0], axis=0)divides every row by its own month-0 count: the retention as a percentage of the cohort's starting size.sns.heatmap(..., annot=True)shades each cell and writes its number in.
Reading a retention table
A cohort table is read in three directions. Try them:
| joined | |||||||
|---|---|---|---|---|---|---|---|
| 100 | 80 | 73 | 59 | 51 | 44 | 42 | |
| 100 | 84 | 78 | 69 | 58 | 49 | 44 | |
| 100 | 74 | 54 | 41 | 30 | 25 | 21 | |
| 100 | 87 | 74 | 67 | 57 | 48 | 44 | |
| 100 | 82 | 72 | 62 | 54 | 46 | 40 | |
| 100 | 84 | 67 | 60 | 48 | 43 | 35 |
months since joining →
Choose a row to follow one cohort as it ages, or a column to compare cohorts at the same age.
- Along a row: one cohort aging. Of January's new members, 80% bought again the next month, 59% three months on, 42% six months on.
- Down a column: cohorts compared at the same age. Three months after joining, most cohorts are at 59% to 69%. March is at 41%, far below the rest.
- Diagonally: the same calendar month. Cells on a diagonal all fall in the same month of 2024, so a store-wide event (a typhoon, a promotion) shows up as a diagonal stripe.
What happened in March? The owners remember: a sign-up gift (a free kilo of sugar) ran that month. It brought in people who wanted the sugar, not the store. A promotion's success should be judged by who stays, not by who signs up.
Wrong way: the average that hid Rosario
The owners' summary: "59% of new card holders are still buying after three months: good, and the same everywhere." Run the cell.
All stores together: 59.1%. By store: Lipa 63.7%, Batangas 61.7%, Tanauan 57.4%, and Rosario 49.5%. Rosario keeps only half its new members three months in, and it's also the store whose sales shrank in real terms (9.5). The overall figure was correct, and it hid the one store where something needs doing.
The check: after any overall rate, break it down by the groups that matter (store, cohort, segment, barangay) before writing "the same everywhere". A group that differs is often the finding. And, as the next lesson shows, small groups vary a lot by chance, so check their sizes before treating a difference as real.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- pandas: pivot_table: counting unique members by cohort and month
- seaborn: heatmap: a shaded table, with the numbers written in
- Diez, Çetinkaya-Rundel and Barr, OpenIntro Statistics, 4th ed., section 1.3: observational data, and why groups must be compared like with like