Module 8: Analyses employers ask for · Lesson 8.4 · about 20 min
RFM customer segmentation
Score every customer on recency, frequency and money spent with groupby and qcut, turn the scores into named segments such as Champions and At risk with np.select, and say what each segment should get.
Module 8 · about 20 minutes · uses Lessons 4.2 and 4.5. You’ll be able to segment customers by behaviour, the classic CRM analysis.
In everyday terms: a café regular: the barista knows who came this morning (recency), who comes every day (frequency) and who orders for the whole office (money).
1. One row per customer
Move from the order line grain to the customer grain (Lesson 6.5):
The snapshot date is “today” for the analysis. Fix it, so the numbers don’t change each day you rerun.
2. Scores from 1 to 5
qcut makes 5 groups of equal size (quintiles). Recency is reversed: fewer days since the last order is better, so it gets 5.
3. Named segments
np.select checks the rules in order and takes the first that is true, like CASE WHEN in SQL. It works on whole columns at once (Lesson 8.8), so it is much faster than apply with an if.
What each segment gets:
| Segment | Meaning | Action |
|---|---|---|
| Champions | Recent and frequent | Thank, early access, ask for reviews |
| Loyal | Fairly recent and frequent | Loyalty rewards, cross-sell |
| New or returning | Recent, not yet frequent | Welcome series, second-purchase offer |
| At risk | Used to buy often, not lately | Win-back offer, ask why |
| Lapsed | Neither | Low-cost reminders only |
- Recency
- Days from the last order to a snapshot date. Lower is better.
- Frequency
- Number of distinct orders.
- Monetary
- Total spend.
- qcut(x, 5, labels=[1..5])
- Quintile scores: each score holds a fifth of customers.
- Physical meaning
- Scores are relative to this shop: a 5 means "top fifth here", not a fixed amount. Recency is reversed so 5 is best, and frequency is ranked first because many customers share the same order count.
“RFM predicts who will buy.” It describes past behaviour. It is a strong first segmentation, but test any action (a voucher to At risk customers) before claiming it works.
Watch it happen, one step at a time:
groupby('customer').agg(last=('date', 'max'), frequency=('order_id', 'nunique'), monetary=('revenue', 'sum'))(snapshot - rfm['last']).dt.days| customer | recency | freq | money | R | F | M |
|---|---|---|---|---|---|---|
| C1 | 3 | 9 | 820 | 5 | 5 | 5 |
| C2 | 40 | 4 | 260 | 3 | 3 | 3 |
| C3 | 200 | 1 | 35 | 1 | 1 | 1 |
np.select: R ≥ 4 and F ≥ 4 → Champions; R ≤ 2 and F ≥ 3 → At risk.4. Your turn
How it works
Principle: past behaviour predicts future behaviour. Someone who bought recently and often is the most likely to buy again; that idea is decades older than machine learning and still beats many fancy models as a first cut.
Quintiles: qcut(x, 5) cuts at the 20th, 40th, 60th and 80th percentiles, so each score holds a fifth of customers. Scores are relative: a “5” means “top fifth of this shop”, not a fixed amount.
Beyond RFM: the same table is the starting point for customer lifetime value models and churn models (ML foundations course), where recency and frequency are usually the strongest features.
5. Practice
- Make an RFM “cell” like “545” with string joining.
- Why fix the snapshot date instead of using today’s date?
- How would you check that Champions really buy more afterwards?
Answers
rfm['cell'] = rfm['R'].astype(str) + rfm['F'].astype(str) + rfm['M'].astype(str).- So the analysis gives the same answer every time and matches the period the data covers.
- Score customers on data up to a cut-off date, then compare their spending in the months after it (a holdout period).
New words: RFM, recency, frequency, monetary, snapshot date, quintile, np.select.
- One row per customer: last date, order count, total spend.
- qcut into 5 groups; reverse recency so 5 is best.
- Rank frequency (method=‘first’) before qcut to break ties.
- Name segments with np.select and say what to do for each.
Check yourself
Sources
- Hughes, A. M. (1994), Strategic Database Marketing, Probus (RFM)
- pandas documentation, User Guide: pandas.qcut https://pandas.pydata.org/docs/reference/api/pandas.qcut.html
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.