Calculate your monthly recurring customer by Cohort Analysis | by Md. Hasibul Islam | Towards Data Science

Imagine you have opened a business, your business is going well. But this could be better. Some of your customers may ask you, “Hey man, I often buy from you, don’t I deserve some offer?”. Now you think that “Well, I should see who are my recurring customers! I should offer them more so that they keep coming regularly.”. And you can do it by cohort analysis. Today I’ll discuss cohort analysis using python. Let’s dive into the ocean of data analysis.
For this analysis, I have used the online retail data set from Kaggle. Go to the link and download it. Okay, so let’s see what’s inside our dataset!
First of all, import the necessary libraries.
import pandas as pdimport seaborn as snsimport datetime as dtimport matplotlib.pyplot as pltimport plotly.express as px
Now load the dataset and observe what’s inside it.
df = pd.read_excel('Online Retail.xlsx')df.head()
So here’re the first five rows of our dataset that has columns,
InvoiceNo: Simply Invoice number of the purchase.
StockCode: Probable product code
Description: What the product is
Quantity: How many products
InvoiceDate: Purchase date
UnitPrice: Price of each single product
CustomerID: A unique string by which each customer will be identified
Country: From which region
Okay, let’s dive in more. Now we shall make a function to get the month number of each invoice number so that we can know on which month the product got purchased.
def get_month(x):    return dt.datetime(x.year, x.month, 1)df['InvoiceMonth'] = df['InvoiceDate'].apply(get_month)df.head()
Look at the last column where I marked the middle part, which is ‘month’. We shall use it later. Now let’s see which customers visited on which months. For that, we shall use pandas ‘groupby’. Don’t worry. it’s easy.
grouping = df.groupby('CustomerID')['InvoiceMonth']grouping.head()
Now let’s see on which month each individual customer made their first purchase.
df['CohortMonth'] = grouping.transform('min')df.head()
Now this ‘CohortMonth’ is the time when the customers made their first purchase. Now we shall again make a function to extract on which months the customers came by so that we can get our recurring customers.
def get_date_int(df, column):    year = df[column].dt.year    month = df[column].dt.month    date = df[column].dt.date    return year, month, dateInvoice_Year, Invoice_Month, _ = get_date_int(df, 'InvoiceMonth')Cohort_Year, Cohort_Month , _ = get_date_int(df, 'CohortMonth')Year_Diff = Invoice_Year - Cohort_YearMonth_Diff = Invoice_Month - Cohort_Monthdf['CohortIndex'] = Year_Diff*12 + Month_Diff +1df.head()
So the marked ‘CohortIndex’ is the column where we get the difference between the first purchase and all the purchases that the customer made later. So here the first one is ‘1.0’ which means he did that purchase in the same month he did his first purchase.
Now let’s ‘groupby’ the customers by ‘CohortMonth’ and ‘CohortIndex’ so that we can identify the customers who came to purchase in the same month with the same interval.
grouping = df.groupby(['CohortMonth', 'CohortIndex'])grouping.head()
Now we shall take only the first month of each customer to calculate a pivot table so that we can calculate later the recurring customers.
cohort_data = grouping['CustomerID'].apply(pd.Series.nunique)cohort_data = cohort_data.reset_index()cohort_data.head()
print(df.shape) #output: 91, 3print(cohort_data.shape)#output: 541909, 11
So we can see only 91 unique customers made 541909 purchases during this period. Let’s make the pivot table.
cohort_counts = cohort_data.pivot(index="CohortMonth",                                  columns="CohortIndex",                                  values="CustomerID")retention = cohort_countsretention
Now we can see some NaN values here means From 2010–12–01 to 2011–12–01 is in total 13 months. So the first index value has the recurring customers of each 13 months, but the next index 2011–01–01 has only 12 months difference from 2011–12–01. That’s why the last row value is NaN. So in the first month, 948 customers came to purchase from which 362 customers returned to buy again in the next month and, it goes on.
Now we shall take the number of customers that came in each month. Because we want to keep them as a base and the later came by customers will be followers.
cohort_sizes = cohort_counts.iloc[:,0]cohort_sizes.head()
Now we will reshape the data in the interval of 1–0 so that we can show a percentage of recurring customers.
retention = cohort_counts.divide(cohort_sizes, axis=0)retention.head()
retention.round(3)*100
So we have converted the retention rate in percentage. Now let’s plot this in a heatmap to observe our monthly recurring customer percentage.
plt.figure(figsize=(10,8))plt.title("Retention Rate")sns.heatmap(data=retention,           annot=True,           fmt='.0%',           vmin=0.0,           vmax=0.5,           cmap="BuGn")plt.show()