Çağlar Laledemir
4 min readDec 14, 2021

Measuring Retention, Churn and Acquisition with Python

Photo by Glenn Carstens-Peters on Unsplash

There are many metrics you can track when the subject comes to measure growth in your business. These metrics can help us to understand the impacts of marketing activities such as campaigns, loyalty programs, discounts, social media etc.

In this post, I will explain some of the major metrics you can easily measure with Python tool. These are retention rate, churn rate and new customers acquisition during a certain period of time. By using these metrics, you can understand how your organization perform in gaining new customers and retaining existing ones.

I have 2 years of sales transaction data shows basket size, date of sale, customer id, gender, age and other relevant information. I will use pandas and numpy directories, these are very popular directories for data manipulation, calculation and plotting.

Firstly, I will read my data from my local computer. My data is comma seperated file so I will use the command below;

import pandas as pd
import numpy as np

mydata= pd.read_csv(R”C:\Users\YourFilePath\sales.csv”,”;”)

I will use the pandas head() function to check first 5 rows of the data;

mydata.head()

You will get a response like below ;

If your data seems fine then we can start to determine which metrics we will measure and how. Before the measurement, you should be sure about the quality of the data you are using. You can check data quality techniques on the internet if you have any hesitation about your data.

These are the metrics we will measure ;

Retention:CurrPer-NewCus

Customer Retention Rate: (Retention)/PrevPer

Churn Rate=1-Retention Rate

New Customer Rate= NewCus/CurrPer

CurrPer:# of total customers in current period we want to measure

PrevPer:# of total customers in current period we want to compare

NewCus:# of new customers in current period

For measuring these metrics, it is very important to decide measurement period. According to your business, you can measure the metrics yearly, monthly, weekly or even daily. In this study, I will prefer monthly period and write my codes according to it.

When the time period is monthly, my current month will be December of 2020 according to my data. Then, my previous period will be November of 2020. We will make our measurements according to these time intervals.

To find out total customers and new customers for current period, we should declare separate lists for unique ID’s of the customers bought products during these period.

PrevPer=[]
CurrPer=[]

In my data date format look like this “11.12.2020” , so I need to parse the DateSale records to find customers purchase products during previous and current periods.

for i in range(len(mydata)):

if (pd.DatetimeIndex(mydata[“DateSale”]))[i].year==2020 and (pd.DatetimeIndex(mydata[“DateSale”]))[i].month==11:
PrevPer.append(mydata[“Customer ID”][i])
elif (pd.DatetimeIndex(camp[“DateSale”]))[i].year==2020 and (pd.DatetimeIndex(mydata[“DateSale”]))[i].month==12:
CurrPer.append(mydata[“Customer ID”][i])
else:
pass

Now, our customer lists are ready but the ID’s are not unique, we should not use duplicate values when calculating new customers arriving or retention. So, we should make the records unique by using codes below;

CurrPer=np.unique(CurrPer)
PrevPer=np.unique(PrevPer)

After the process, we can compare which customers retained in current period with comparing previous period customer list. If the customerID exist in both of the lists then we can say that the customer is retained;

Retention=0
Churn=0
NewCustomer=0

for a in range(len(PrevPer)):
for b in range(len(CurrPer)):
if PrevPer[a]==CurrPer[b]:
Retention=Retention+1

Retention

You can see the result like below after the run these lines;

We find number of total customers retained during current period. Now, we can measure retention rate, churn rate and new customer rate with the lines below;

RetentionRate=Retention/len(PrevPer)
round(RetentionRate,2)

ChurnRate=1-RetentionRate
round(ChurnRate,2)

NewCustomerRate=(len(CurrPer)-Retention)/len(CurrPer)
round(NewCustomerRate,2)

In this post, I tried to explain how you can measure 3 major growth metrics, churn, retention and acquisition for a certain period of time defined. I hope you can find this useful for your work.

Çağlar Laledemir

Big Data & AI | Data Science Lead | Machine Learning & Analytics