Every owner believes they know which customers matter most. The answer is usually the handful they deal with personally, the one who calls all the time, or the biggest company on the list. The transaction records usually disagree. The customers who actually carry the business are often just twenty percent of the total, and the ones you spend the most time on are not always on that list.
RFM groups customers with three numbers: recency of the last purchase, frequency of purchases, and monetary value. Every business already has these three numbers. They are just scattered across Excel files and invoices. The walkthrough below starts from one sales sheet, scores it in Python, sorts customers into segments, and exports a colour-coded Excel file.
What the three numbers mean
- R (Recency): days between the customer's last order and the snapshot date. Smaller is fresher.
- F (Frequency): number of distinct orders in the period. Count order IDs, not rows, or one large order split across ten lines looks like ten purchases.
- M (Monetary): total spend in the period, with refunds deducted.
They only make sense together. A customer with high M might be a single large job from a year ago. A customer with high F might be buying a few hundred dollars at a time. The combination tells the story.
Step one: one sheet, four columns
Export from your POS, accounting system or invoice records. All you need is four columns: customer ID, order ID, date, amount. Eighteen months of history is enough. Shorter and you cannot see the buying cycle; longer and closed accounts still look active.
Two things need fixing before you run anything. Customer names must be consistent, since "Chan Kee" and "Chan Kee Foods" are the same account and must map to one ID. And amounts must have refunds deducted rather than being gross sales.
Step two: compute R, F and M in pandas
There is one easy mistake here: do not use pd.Timestamp.today() as the snapshot. If the data was last refreshed a month ago, using today adds thirty days to every customer's recency, and the segment boundaries drift. The same customer is active this month and mysteriously churned the next, with no behaviour change at all.
Step three: score each dimension, five levels is enough
The three numbers have different units, so convert them into comparable scores. Split customers into quintiles and award one to five points for each. For recency, lower is better, so the scale is reversed.
Why the rank(method="first") step? Many customers have identical order counts, for example everyone who bought exactly once. Passing that straight into qcut hits duplicate bin edges and raises Bin edges must be unique. Ranking first makes every value unique and the call always succeeds.
Step four: assign the segments
With the scores in place, a short set of rules sorts customers into groups. Five is enough to act on:
Each group needs a different action. Core customers need retention, growing customers need a bigger basket, new customers need a second purchase, dormant loyal customers need one message rather than a phone campaign, and high-value at-risk customers need a human call. Sending one promotion to all five is saying the same sentence to five different people.
Step five: export a colour-coded Excel file
The output has to reach the sales team, and not everyone opens Python. A workbook with colour blocks lets a colleague see who to call in one glance.
A real example
A food wholesaler with 3,200 customers, eighteen months of history and roughly 41,000 order lines ran this once. The results:
- 214 core customers, 6.7 percent of the base, contributing 43 percent of revenue.
- Adding the growing segment, 19 percent of customers account for 61 percent of revenue. The other 81 percent share the remaining 39 percent.
- 96 high-value at-risk customers who spent HK$2.4 million over the previous twelve months but have not ordered in five months.
Those 96 were the most valuable finding. They had simply stopped ordering, and nobody noticed, because customers who go quiet do not complain or enquire. Sales called each one. Forty-three responded, nineteen placed a new order, and about HK$380,000 came back, roughly sixteen percent of that group's historic revenue.
On the other side, 480 dormant loyal customers had each bought five times or more but nothing for six months. They did not justify phone calls, but they did justify a single WhatsApp message at almost no cost.
Four traps worth knowing
First, using today as the snapshot date. If the data has not been refreshed, every customer ages at once and the segmentation becomes meaningless. Always anchor to the last date in the data.
Second, leaving refunds and freight in the monetary figure. A customer with a high return rate can post large sales and still be net negative. If your system cannot supply refunds, use net sales rather than shipped value.
Third, treating new customers as low value. A first-time buyer always scores F of one and usually a small M, so a straight ranking on total score pushes them to the bottom. They are the only group that grows, which is why they need their own segment instead of being folded into regular.
Fourth, unreconciled customer IDs. One customer registered under two names and three phone numbers becomes three people in the analysis, each looking small, and the same promotion gets paid for three times. Half a day spent cleaning the customer master beats any change of model.
Summary
Python Tutorial: RFM needs no machine learning and no CRM licence. A four-column sales sheet, twenty lines of pandas and ten lines of openpyxl turns "which customers matter" from a feeling into a number. The segment that usually pays for the exercise is not the core group but the high-value customers who went quiet: you already have the relationship and the history, and all that is missing is one conversation. Run it quarterly. The report is not the point; the point is whether anyone acts on each group afterwards.