保留正文与补采附件。源页面当前未重新核验;具体限制见交付清单。

Revisit consent button We value your privacy We use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking "Accept All", you consent to our use of cookies. Customise Reject All Accept All Customise Consent Preferences We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below. The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... Show more NecessaryAlways Active Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Functional Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

Analytics Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Performance Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Advertisement Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

Uncategorised Other uncategorised cookies are those that are being analysed and have not been classified into a category as yet. No cookies to display. Reject All Save My Preferences Accept All Toggle SearchToggle Mobile Navigation Publish AI, ML & data-science insights to a global community of data professionals. Sign inSubmit an Article LatestEditor's PicksNewsletterShipAI


Write For TDS LinkedInXToggle Search Data Science

EDA in Public (Part 3): RFM Analysis for Customer Segmentation in Pandas

How to build, score, and interpret RFM segments step by step Ibrahim Salami January 1, 202612 min read Exploratory Data Analysis with Pandas Image by Author. Created with Gemini Welcome back! If you’ve been following along, we’ve come a long way. In Part 1, we did the “dirty work” of cleaning and prepping. In Part 2, we zoomed out to a high-altitude view of NovaShop’s world — spotting the big storms (high-revenue countries) and the seasonal patterns (the massive Q4 rush). But here’s the thing: a business doesn’t actually sell to “months” or “countries.” It sells to human beings. If you treat every customer exactly the same, you’re making two very expensive mistakes:

The Solution? Behavioural Segmentation. Instead of guessing, we’re going to use the data to let the customers tell us who they are. We do this using the gold standard of retail analytics: RFM Analysis.

By the end of this part, we’ll move beyond “Top 10 Products” and actually assign a specific, actionable Label to every single customer in NovaShop’s database. Learn this step by step with the interactive AI and Data Scientist roadmap.

Data Preparation: The “Missing ID” Pivot

Before we can start scoring, we have to address a decision we made back in Part 1. If you remember our Initial Inspection , we noticed that about 25% of our rows were missing a CustomerID. At the time, we made a strategic business decision to keep those rows. We needed them to calculate the accurate total revenue and see which products were popular overall. For RFM analysis, the rules change. You cannot track behavior without a consistent identity. We can’t know how “frequent” a customer is if we don’t know who they are! So, our first step in Part 3 is to isolate our “Trackable Universe” by filtering for rows where a CustomerID exists.

Engineering the RFM Metrics

Now that we have a dataset where every row is linked to a specific person, we need to aggregate all their individual transactions into three summary numbers: Recency, Frequency, and Monetary.

Defining the Snapshot Date

Before calculating RFM, we need a reference point in time , commonly called the snapshot date. Here, we take the most recent transaction date in the dataset and add one day. This snapshot date represents the moment at which we’re evaluating customer behaviour. python

snapshot_date = df['InvoiceDate'].max() + dt.timedelta(days=1)

We added one day, so customers who bought on the most recent date still have a Recency value of 1 day, not 0. This keeps the metric intuitive and avoids edge-case problems.

Aggregating Transactions at the Customer Level

python

rfm = df.groupby(‘CustomerID’).agg({
‘InvoiceDate’: lambda x: (snapshot_date — x.max()).days,
‘InvoiceNo’: ‘nunique’,
‘Revenue’: ‘sum’
})

Each row in our dataset represents a single transaction. To calculate RFM, we need to collapse these transactions into one row per customer. We do this by grouping the data by CustomerID and applying different aggregation functions:

Renaming Columns for Clarity

python

rfm.rename(columns={
'InvoiceDate': 'Recency',
'InvoiceNo': 'Frequency',
'Revenue': 'Monetary'
}, inplace=True)py

The aggregation step keeps the original column names, which can be confusing. Renaming them makes the dataframe immediately readable and aligns it with standard RFM terminology. Now each column clearly answers a business question:

Inspecting the Result

python

print(rfm.head())

The final rfm dataframe contains one row per customer , with three intuitive metrics summarizing their behavior. Output: Let’s walk through this the way we would with NovaShop in a real conversation.

“When was the last time this customer bought from us?” That’s exactly what Recency answers. Take Customer 12347 :

They’re fresh. They remember the brand. They’re still engaged. Now compare that to Customer 12346 :

Even though this customer spent a lot in the past, they’re currently silent. From NovaShop’s perspective: Recency tells us who’s still listening and who might need a nudge (or a wake-up call).

“Is this a one-time buyer or someone who keeps coming back?” That’s where Frequency comes in. Look again at Customer 12347 :

Now look at several others:

From a business perspective, frequency separates casual shoppers from loyal customers.

“Who actually brings in the money?” That’s the Monetary column.
And this is where things get interesting. Customer 12346 :

This tells a very specific story: A single, very large order… a long time ago… and nothing since. Now compare that to Customer 12347 :

Important insight for NovaShop: A “high-value” customer in the past isn’t necessarily a valuable customer today.

Why This View Changes the Conversation

If NovaShop only looked at total revenue, they might focus all their attention on customers like 12346. But RFM shows us that:

This output helps NovaShop stop guessing and start prioritizing:

Right now, these are still raw numbers. In the next step, we’ll rank and score these customers, so NovaShop doesn’t have to interpret rows manually. Instead, they’ll see clear segments like:

That’s where this becomes a real decision-making tool — not just a dataframe.

Turning RFM Numbers Into Meaningful Customer Segments

At this stage, NovaShop has a table full of numbers. Useful — but not exactly decision-friendly. A marketing team can’t realistically scan hundreds or thousands of rows asking:

Our goal is to rank customers relative to one another and turn raw values into scores .

Step 1: Ranking Customers by Each RFM Metric

Instead of treating Recency, Frequency, and Monetary as absolute values, we look at where each customer stands compared to everyone else.

In practice, we do this by splitting each metric into quantiles (usually 4 or 5 buckets). However, there’s a small real-world wrinkle. This is something I came across while working on this project In transactional datasets, it’s common to see:

To keep things robust and readable, we’ll wrap the scoring logic in a small helper function. python

def rfm_score(series, ascending=True, n_bins=5):
# Rank the values to ensure uniqueness
ranked = series.rank(method=’first’, ascending=ascending)

# Use pd.qcut on the ranks to assign bins
return pd.qcut(
ranked,
q=n_bins,
labels=range(1, n_bins+1)
).astype(int)

To explain what’s going on here:

In short, this function provides a robust and reusable way to score RFM metrics without running into duplicate bin edge errors — and without overcomplicating the logic.

Step 2: Applying the Scores

Now we can score each metric cleanly and consistently: python

# Assign R, F, M scores
rfm['R_Score'] = rfm_score(rfm['Recency'], ascending=False) # Recent purchases = high score
rfm['F_Score'] = rfm_score(rfm['Frequency']) # More frequent = high score
rfm['M_Score'] = rfm_score(rfm['Monetary']) # Higher spend = high score

The only special case here is Recency :

What This Means for NovaShop

Instead of seeing this: markup

Recency = 326Frequency = 1Monetary = 77,183.60

NovaShop now sees something like: markup

R = 1, F = 1, M = 5

That’s instantly more interpretable:

Step 3: Creating a Combined RFM Score

Now we combine these three scores into a single RFM code: python

rfm['RFM_Score'] = (
rfm['R_Score'].astype(str) +
rfm['F_Score'].astype(str) +
rfm['M_Score'].astype(str)
)

This produces values like:

Each customer now carries a compact behavioral fingerprint. And we’re not done yet.

Translating RFM Scores Into Customer Segments

Raw scores are nice, but let’s be honest: no marketing manager wants to look at 555, 154, or 311 all day. NovaShop needs labels that make sense at a glance. That’s where RFM segments come in.

Step 1: Defining Segments

Using RFM scores, we can classify customers into meaningful categories. Here’s a common approach:

This transforms abstract numbers into a narrative that marketing and management can act on.

Step 2: Mapping Scores to Segments

Here’s an example using simple conditional logic: python

def rfm_segment(row):
if row['R_Score'] >= 4 and row['F_Score'] >= 4 and row['M_Score'] >= 4:
return 'Champions'
elif row['F_Score'] >= 4:
return 'Loyal Customers'
elif row['M_Score'] >= 4:
return 'Big Spenders'
elif row['R_Score'] <= 2:
return 'At-Risk'
else:
return 'Others'
rfm['Segment'] = rfm.apply(rfm_segment, axis=1)

Now each customer has a human-readable label , making it immediately actionable. Let’s review our results using rfm.head()

Step 3: Turning Segments into Strategy

With labeled segments, NovaShop can:

This is the moment where data becomes strategy.

What NovaShop Should Do Next (Key Takeaways & Recommendations)

At the start of this analysis, NovaShop had a familiar problem:
A lot of transactional data, but limited clarity on customer behaviour. By applying the RFM framework, we’ve turned raw purchase history into a clear, structured view of who NovaShop’s customers are — and how they behave. Now let’s talk about what to actually do with it.

1. Protect and Reward Your Best Customers

Champions and Loyal Customers are already doing what every business wants:

These customers don’t need heavy discounts — they need recognition. Recommended actions:

The goal here isn’t acquisition, it’s retention.

2. Re-Engage High-Value Customers Before They’re Lost

The most dangerous segment for NovaShop isn’t “Lost” customers.
It’s At-Risk and Big Spenders. These customers:

Recommended actions:

Winning back an existing customer is almost always cheaper than acquiring a new one.

3. Don’t Over-Invest in Truly Lost Customers

Some customers will inevitably churn. RFM helps NovaShop identify those customers early and avoid spending ad budget, discounts and marketing effort on users who are unlikely to return. This isn’t about being cold — it’s about being efficient.

4. Use RFM as a Living Framework, Not a One-Off Analysis

The real power of RFM comes when it’s:

For NovaShop, this means asking questions like:

RFM turns customer behaviour into something measurable and trackable.

Final Thoughts: Closing the EDA in Public Series

When I started this EDA in Public series, I wasn’t trying to build the perfect analysis or demonstrate advanced techniques. I wanted to slow down and share how I actually think when working with real data. Not the polished version, but the messy, iterative process that usually stays hidden. This project began with a noisy CSV and a lot of open questions. Along the way, there were small issues that only surfaced once I paid closer attention — dates stored as strings, assumptions that didn’t quite hold up, metrics that needed context before they made sense. Working through those moments in public was uncomfortable at times, but also genuinely valuable. Each correction made the analysis stronger and more honest. One thing this process reinforced for me is that most meaningful insights don’t come from complexity. They come from slowing down, structuring the data properly, and asking better questions. By the time I reached the RFM analysis, the value wasn’t in the formulas themselves — it was in what they forced me to confront. A customer who spent a lot once isn’t necessarily valuable today. Recency matters. Frequency matters. And none of these metrics mean much in isolation. Ending the series with RFM felt deliberate. It sits at the point where technical work meets business thinking, where tables turn into conversations and numbers turn into decisions. It’s also where exploratory analysis stops being purely descriptive and starts becoming practical. At that stage, the goal is no longer just to understand the data, but to decide what to do next. Doing this work in public changed how I approach analysis. Writing things out forced me to explain my reasoning, question my assumptions, and be comfortable showing imperfect work. It reminded me that EDA isn’t a checklist you rush through — it’s a dialogue with the data. Sharing that dialogue makes you more thoughtful and more accountable. This may be the final part of the EDA in Public series, but it doesn’t feel like an endpoint. Everything here could evolve into dashboards, automated pipelines, or deeper customer analysis. And if you’re a founder, analyst, or team working with customer or sales data and trying to make sense of it, this kind of exploratory work is often where the biggest clarity comes from. These are exactly the kinds of problems I enjoy working through — slowly, thoughtfully, and with the business context in mind. If you’re documenting your own analyses, I’d love to see how you approach it. And if you’re wrestling with similar questions in your data and want to talk through them, feel free to reach out on any of the platforms below. Good data conversations usually start there. Thanks for following along! Medium LinkedIn Twitter YouTube

Register Now to Continue Reading

Unlock Towards Data Science with a Free Account

All articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more. Register for freeSign in to your account


Written By

Ibrahim Salami

See all from Ibrahim Salami Deep DivesExploratory Data AnalysisMachine LearningPandasPython Share This Article

Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program. Write for TDS

Related Articles

Instacart Users Segmentation and Market Basket Analysis

Machine Learning Xin Yu, Yingyuan (Valerie) Zhang, Yutong (Grace) Zhu Yingyuan (Valerie) Zhang October 12, 20205 min read

Introduction to Applied Linear Algebra: Vectors

Artificial Intelligence This article gives an introduction to vectors, vector operations and their application in the field of data science. Deniz Tuzsus November 25, 20215 min read

Creating a Machine Learning Model with SQL

Machine Learning Build an ML model using SQL on Google Big Query Anuj Syal November 12, 20215 min read

How to Build a Machine Learning Demo in 2022

Deep Learning Learn why you should build demos for your Machine Learning models in 2022 Nicolas Jaccard January 18, 202212 min read

How to Find the Best Multilingual Embedding Model for Your RAG

Artificial Intelligence Optimize the Embedding Space for Improving RAG Iulia Brezeanu January 27, 20248 min read

Introduction to Pandas

Programming Enhance your Data Science skills in Python with the Pandas API. Dan Root October 30, 20201 min read

Monitoring NLP models in production

NLP A code tutorial on detecting drift in text data Elena Samuylova February 20, 202313 min read

Model Interpretation Strategies

Artificial Intelligence Learn about model interpretation techniques, limitations and advances Dipanjan (DJ) Sarkar October 31, 201821 min read

Serving TensorRT Models with NVIDIA Triton Inference Server

Artificial Intelligence Achieving optimal throughput and latency with model inference on high client-server traffic Tan Pengshi Alvin December 15, 20226 min read

Your home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals. Subscribe to Our NewsletterWrite For TDS•About•Advertise•Privacy Policy•Terms of Use © Insight Media Group, LLC 2026•The New Stack•roadmap.sh•Creator Network

Recommended Articles

Close