RFM Analysis in Python | Simplified.
Suresh Sonwane
Posted on March 2, 2022
An automated way to perform RFM analysis.
In this tutorial, we will perform RFM analysis using a python library called "rfm".
We will be using Kaggle E-commerce dataset.
1 . Install Package using:
$ pip install rfm
2 . Read the transaction dataset:
>>> import pandas as pd
>>> df = pd.read_csv('~./data.csv')
# create new column for transaction amount for each record
>>> df['Amount'] = df['Quantity'] * df['UnitPrice']
3 . Start RFM Analysis using:
>>> from rfm import RFM
# this will take some time depending upon size of the dataset.
# enter the required columns names: customerid, transaction date and amount
>>> r = RFM(df, customer_id='CustomerID', transaction_date='InvoiceDate', amount='Amount')
4 . See the results using:
>>> r.rfm_table
Ta-Da !!! It is that simple.
This way it automatically calculates recency, frequency, monetary values as well as rfm scores and along with their segments for you. You can save above results in memory by using pd.to_csv method.
The rfm package offers further functionalities and analytical graphs for your analysis reports for those who want it all.
Additional Extra Features:
1 . See the number of customers per segment or segment distribution table using:
>>> r.segment_table
>>> r.plot_segment_distribution()
Find out more:
Medium Blog link
GitHub
PyPi
Posted on March 2, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.