All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

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.

RFM describes each customer by three numbers: how recently they bought, how often, and how much. Scored 1 to 5 against other customers, they turn into segments you can act on.

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):

Pythonorders.csv

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

Pythonorders.csv

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

Pythonorders.csv

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:

One row per customer. groupby('customer').agg(last=('date', 'max'), frequency=('order_id', 'nunique'), monetary=('revenue', 'sum'))
Recency. (snapshot - rfm['last']).dt.days
Scores.
customerrecencyfreqmoneyRFM
C139820555
C2404260333
C3200135111
Segments. np.select: R ≥ 4 and F ≥ 4 → Champions; R ≤ 2 and F ≥ 3 → At risk.
Act. Thank Champions; win back At risk; do not spend on one-off bargain hunters.

4. Your turn

Your turnorders.csv
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

  1. Make an RFM “cell” like “545” with string joining.
  2. Why fix the snapshot date instead of using today’s date?
  3. How would you check that Champions really buy more afterwards?
Answers
  1. rfm['cell'] = rfm['R'].astype(str) + rfm['F'].astype(str) + rfm['M'].astype(str).
  2. So the analysis gives the same answer every time and matches the period the data covers.
  3. 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

1What does R in RFM measure?
2Why rank frequency before qcut?
3A customer with R = 1 and F = 5 is best described as...

Sources

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.