DEV Community

Cover image for RFM Analysis in Python | Simplified.
Suresh Sonwane
Suresh Sonwane

Posted on

RFM Analysis in Python | Simplified.

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
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

4 . See the results using:

>>> r.rfm_table
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

>>> r.plot_segment_distribution()
Enter fullscreen mode Exit fullscreen mode

Image description

Find out more:
Medium Blog link
GitHub
PyPi

Top comments (0)