Path: blob/master/python/cohort/cohort.ipynb
1480 views
Cohort Analysis
What is Cohort Analysis? and why is it valuable? To begin with, a cohort is a group of users who share something in common, be it their sign-up date, first purchase month, birth date, acquisition channel, etc. Cohort analysis is the method by which these groups are tracked over time, helping you spot trends, understand repeat behaviors (purchases, engagement, amount spent, etc.) and monitor your customer and revenue retention.
It's common for cohorts to be created based on a customer's first usage of the platform, where "usage" is dependent on your business's key metrics. For Uber or Lyft, usage would be booking a trip through one of their apps. For GrubHub, it's ordering some food. For AirBnB, it's booking a stay. With these companies, a purchase is at their core, be it taking a trip or ordering dinner — their revenues are tied to their users' purchase behavior. In others, a purchase is not central to the business model and the business is more interested in "engagement" with the platform. Facebook and Twitter are examples of this - are you visiting their sites every day? Are you performing some action on them - maybe a "like" on Facebook or a "favorite" on a tweet? Thus when building up a cohort analysis, it's important to consider the relationship between the event or interaction you're tracking and its relationship to your business model.
Example of Monthly Cohort
Imagine we have the following dataset that has the standard purchase data with IDs for the order and user, order data and purchase amount. To create our monthly cohort, we'll first have to:
Convert our date to a monthly-time basis.
Determine the user's cohort group based on their first order, which is the year and month in which the user's first purchase occurred.
Since we're looking at monthly cohorts, we need to aggregate users, orders, and amount spent by the CohortGroup within each month (OrderPeriod). After that, we wish to look at how each cohort has behaved in the months following their first purchase, so we'll need to index each cohort with respect to their first purchase month. For example, CohortPeriod = 1 will be the cohort's first month, CohortPeriod = 2 is their second, and so on. This allows us to compare cohorts across various stages of their lifetime.
We're now half way done, before we proceed on with the process, we will do some sanity checking to make sure that we did everything right by performing some simple testing. We'll test data points from the original DataFrame with their corresponding values in the new cohorts DataFrame to make sure all our data transformations worked as expected. As long as none of these raise an exception, we're good.
To calculate the user retention by cohort group, We want to look at the percentage change of each CohortGroup
over time -- not the absolute change. To do this, we'll first need to create a pandas Series containing each CohortGroup
and its size.
Now, we'll need to divide the TotalUsers
values in cohorts by cohort_size
. Since DataFrame operations are performed based on the indices of the objects, we'll use unstack
on our cohorts DataFrame to create a matrix where each column represents a CohortGroup
and each row is the CohortPeriod
corresponding to that group.
Stack/Unstack
In case you're not familiar with what unstack
and stack does:
Stacking a DataFrame means moving (also rotating or pivoting) the innermost column index to become the innermost row index. The inverse operation is called unstacking. It means moving the innermost row index to become the innermost column index. The following diagram depicts the operations:
The resulting DataFrame, user_retention
, contains the percentage of users from the cohort purchasing within the given period. For instance, 38.4% of users in the 2009-03 cohort purchased again in month 3 (which would be May 2009).
Finally, we can plot the cohorts over time in an effort to spot behavioral differences or similarities. Two common cohort charts are line graphs and heatmaps, both of which are shown below.
Notice that the first period of each cohort is always 100% -- this is because our cohorts are based on each user's first purchase. Meaning everyone in the cohort should have made a purchase in the first month.
Unsurprisingly, we can see from the above chart that fewer users tend to purchase as time goes on. However, we can also see that the 2009-01 cohort is the strongest, which enables us to ask targeted questions about this cohort compared to others -- What other attributes (besides first purchase month) do these users share which might be causing them to stick around? How were the majority of these users acquired? Was there a specific marketing campaign that brought them in? Did they take advantage of a promotion at sign-up? The answers to these questions would inform future marketing and product efforts.
Further Work
User retention is only one way of using cohorts to look at your business — we could have also looked at revenue retention. That is, the percentage of each cohort’s first month revenue returning in subsequent periods. User retention is important, but we shouldn’t lose sight of the revenue each cohort is bringing in (and how much of it is returning).
To sum it up, cohort analysis can be valuable when it comes to understanding your business's health and "stickiness" - the loyalty of your customers. Stickiness is critical since it’s far cheaper and easier to keep a current customer than to acquire a new one. For startups, it’s also a key indicator of product-market fit.
Additionally, your product evolves over time. New features are added and removed, the design changes, etc. Observing individual groups over time is a starting point to understanding how these changes affect user behavior. It’s also a good way to visualize your user retention/churn as well as formulating a basic understanding of their lifetime value.