search
HomeBackend DevelopmentPython TutorialHow to apply Python in cohort analysis

How to apply Python in cohort analysis

Apr 22, 2023 am 08:43 AM
python

Cohort Analysis

Cohort Analysis Concept

Cohort literally means a group of people (with common characteristics or similar behaviors), such as different genders and different ages.

Cohort analysis: Compares changes in similar groups over time.

The product will continue to iterate as you develop and test it, which results in users who join in the first week of product release having a different experience than those who join later. For example, each user will go through a life cycle: from free trial, to paid use, and finally stop using it. At the same time, during this period, you are constantly making adjustments to your business model. Therefore, users who “eat crabs” in the first month after the product is launched are bound to have a different onboarding experience than users who join after four months. What impact will this have on churn rates? We used cohort analysis to find out.

Each group of users forms a cohort and participates in the entire experimental process. By comparing different cohorts, you can learn whether overall performance on key metrics is getting better.

Combined with the user analysis level, such as users acquired in different months, new users from different channels, and users with different characteristics (such as users on WeChat who communicate with at least 10 friends on WeChat every day).

Cohort Analysis is a comparative analysis of these groups of people with different characteristics to discover their behavioral differences in the time dimension.

Therefore, cohort analysis is mainly used for the following two points:

  • Compare the data indicators of different cohort groups in the same experience cycle to verify the effect of product iteration optimization

  • Compare the data indicators of different experience cycles (life cycles) of the same cohort group and discover the problem of long-term experience

We are conducting cohort analysis At this time, it can be roughly divided into two processes: determining the grouping logic of the cohort and determining the key data indicators of the cohort analysis.

  • Groups with similar behavioral characteristics

  • Groups with the same time period

For example:

  1. By customer acquisition month (grouped by week or even day)

  2. By customer acquisition channel

  3. Category according to the specific actions completed by the user, such as the number of times the user visits the website or the number of purchases.

Regarding key data indicators, they need to be based on the time dimension, such as retention, revenue, self-propagation coefficient, etc.

The following is a case example using retention rate as an indicator:

How to apply Python in cohort analysis

The following is the operating data of an e-commerce company. We will use this data to demonstrate using python Cohort analysis.

Detailed explanation of cohort analysis case:

The data is an e-commerce user's payment log. The log fields include date, payment amount and user ID, which have been desensitized.

Data reading

import pandas as pd

df = pd.read_csv('日志.csv', encoding="gb18030")
df.head()

How to apply Python in cohort analysis

Analysis direction

Group logic:

Here only based on the user’s initial purchase month For grouping, if the log contains more classification fields (such as channel, gender or age, etc.), you can consider more grouping logic.

Key data indicators:

For this data, there are at least 3 data indicators that can be analyzed:

  1. Retention Rate

  2. Payment amount per capita

  3. Number of purchases per capita

Data preprocessing

Because we are grouping by month, we need to resample the dates into months first:

df['购买月份'] = pd.to_datetime(df.日期).dt.to_period("M")
df.head()

How to apply Python in cohort analysis

Calculate the total payment of each user in each month:

order = df.groupby(["uid", "购买月份"], as_index=False).agg(
    月付费总额=("付费金额","sum"),
    月付费次数=("uid","count"),
)
order.head()

How to apply Python in cohort analysis

Calculate the first purchase month of each user as a cohort group, and map it to the original data:

order["首单月份"] = order.groupby("uid")['购买月份'].transform("min")
order.head()

How to apply Python in cohort analysis

Calculate the month difference between the time of each purchase record and the first purchase time, and reset the month difference label:

order["标签"] = (order.购买月份-order.首单月份).apply(lambda x:"同期群人数" if x.n==0 else f"+{x.n}月")
order.head()

How to apply Python in cohort analysis

Both months are Period type, after subtraction, a column of object type is obtained, and the type of each element of this column is pandas._libs.tslibs.offsets.MonthEnd

The MonthEnd type has an attribute n that can return a specific difference integer.

Cohort analysis

We said earlier that there are at least 3 data indicators that can be analyzed:

  1. Retention rate

  2. Payment amount per person

  3. Number of purchases per person

从留存率角度进行同期群分析

通过数据透视表可以一次性计算所需的数据:

cohort_number = order.pivot_table(index="首单月份", columns="标签",
                             values="uid", aggfunc="count",
                             fill_value=0).rename_axis(columns="留存率")
cohort_number

How to apply Python in cohort analysis

注意:rename_axis(columns=None)用于删除列标签的轴名称。rename_axis(columns=“留存率”)则设置轴名称为留存率。

将 本月新增 列移动到第一列:

cohort_number.insert(0, "同期群人数", cohort_number.pop("同期群人数"))
cohort_number

How to apply Python in cohort analysis

具体过程是先通过pop删除该列,然后插入到0位置,并命名为指定的列名。

 在本次的分析中,留存率的具体计算方式为:+N月留存率=+N月付款用户数/首月付款用户数

cohort_number.iloc[:, 1:] = cohort_number.iloc[:, 1:].divide(cohort_number.本月新增, axis=0)
cohort_number

How to apply Python in cohort analysis

以百分比形式显示,并设置颜色:

out1 = (cohort_number.style
        .format("{:.2%}", subset=cohort_number.columns[1:])
        .bar(subset="同期群人数", color="green")
        .background_gradient("Reds", subset=cohort_number.columns[1:], high=1, axis=None)
        )
out1

How to apply Python in cohort analysis

至此计算完毕。

从人均付款金额角度进行同期群分析

要从从人均付款金额角度考虑,需要考虑同期群基期这个整体。具体计算方式是先计算各月的付款总额,然后除以基期的总人数:

cohort_amount = order.pivot_table(index="首单月份", columns="标签",
                                  values="月付费总额", aggfunc="sum",
                                  fill_value=0).rename_axis(columns="人均付款金额")
cohort_amount.insert(0, "首月人均付费", cohort_amount.pop("同期群人数"))
cohort_amount.insert(0, "同期群人数", cohort_number.同期群人数)
cohort_amount.iloc[:, 1:] = cohort_amount.iloc[:, 1:].divide(cohort_amount.同期群人数, axis=0)
out2 = (cohort_amount.style
        .format("{:.2f}", subset=cohort_amount.columns[1:])
        .background_gradient("Reds", subset=cohort_amount.columns[1:], axis=None)
        .bar(subset="同期群人数", color="green")
        )
out2

How to apply Python in cohort analysis

可以看到,12月份的同期群首月新用户人均消费为703.43元,然后逐月递减,到+4月后这些用户人均消费仅11.41元。而随着版本的迭代发展,新增用户的首月消费并没有较大提升,且接下来的消费趋势反而不如12月份。由此可见产品的发展受到了一定的瓶颈,需要思考增长营收的出路了。

一般来说, 通过同期群分析可以比较好指导我们后续更深入细致的数据分析,为产品优化提供参考。

从人均购买次数角度进行同期群分析

依然按照上面一样的套路:

cohort_count = order.pivot_table(index="首单月份", columns="标签",
                                 values="月付费次数", aggfunc="sum",
                                 fill_value=0).rename_axis(columns="人均购买次数")
cohort_count.insert(0, "首月人均频次", cohort_count.pop("同期群人数"))
cohort_count.insert(0, "同期群人数", cohort_number.同期群人数)
cohort_count.iloc[:, 1:] = cohort_count.iloc[:,
                                             1:].divide(cohort_count.同期群人数, axis=0)
out3 = (cohort_count.style
        .format("{:.2f}", subset=cohort_count.columns[1:])
        .background_gradient("Reds", subset=cohort_count.columns[1:], axis=None)
        .bar(subset="同期群人数", color="green")
        )
out3

How to apply Python in cohort analysis

可以得到类似上述一致的结论。

每月总体付费情况

下面我们看看每个月的总体消费情况:

order.groupby("购买月份").agg(
    付费人数=("uid", "count"),
    人均付款金额=("月付费总额", "mean"),
    月付费总额=("月付费总额", "sum")
)

How to apply Python in cohort analysis

可以看到总体付费人数和付费金额都在逐月下降。

将结果导出网页或截图

对于Styler类型,我们可以调用render方法转化为网页源代码,通过以下方式即可将其导入到一个网页文件中:

with open("out.html", "w") as f:
    f.write(out1.render())
    f.write(out2.render())
    f.write(out3.render())

如果你的电脑安装了谷歌游览器,还可以安装dataframe_image,将这个表格导出为图片。

安装:pip install dataframe_image

import dataframe_image as dfi

dfi.export(obj=out1, filename='留存率.jpg')
dfi.export(obj=out2, filename='人均付款金额.jpg')
dfi.export(obj=out3, filename='人均购买次数.jpg')

dfi.export的参数:

  • obj : 被导出的Datafream对象

  • filename : 文件保存位置

  • fontsize : 字体大小

  • max_rows : 最大行数

  • max_cols : 最大列数

  • table_conversion : 使用谷歌游览器或原生’matplotlib’, 只要写非’chrome’的值就会使用原生’matplotlib’

  • chrome_path : 指定谷歌游览器位置

 整体完整代码

import pandas as pd
import dataframe_image as dfi

df = pd.read_csv('日志.csv', encoding="gb18030")
df['购买月份'] = pd.to_datetime(df.日期).dt.to_period("M")
order = df.groupby(["uid", "购买月份"], as_index=False).agg(
    月付费总额=("付费金额", "sum"),
    月付费次数=("uid", "count"),
)
order["首单月份"] = order.groupby("uid")['购买月份'].transform("min")
order["标签"] = (
    order.购买月份-order.首单月份).apply(lambda x: "同期群人数" if x.n == 0 else f"+{x.n}月")
cohort_number = order.pivot_table(index="首单月份", columns="标签",
                                  values="uid", aggfunc="count",
                                  fill_value=0).rename_axis(columns="留存率")
cohort_number.insert(0, "同期群人数", cohort_number.pop("同期群人数"))
cohort_number.iloc[:, 1:] = cohort_number.iloc[:,1:].divide(cohort_number.同期群人数, axis=0)
out1 = (cohort_number.style
        .format("{:.2%}", subset=cohort_number.columns[1:])
        .bar(subset="同期群人数", color="green")
        .background_gradient("Reds", subset=cohort_number.columns[1:], high=1, axis=None)
        )
cohort_amount = order.pivot_table(index="首单月份", columns="标签",
                                  values="月付费总额", aggfunc="sum",
                                  fill_value=0).rename_axis(columns="人均付款金额")
cohort_amount.insert(0, "首月人均付费", cohort_amount.pop("同期群人数"))
cohort_amount.insert(0, "同期群人数", cohort_number.同期群人数)
cohort_amount.iloc[:, 1:] = cohort_amount.iloc[:, 1:].divide(cohort_amount.同期群人数, axis=0)
out2 = (cohort_amount.style
        .format("{:.2f}", subset=cohort_amount.columns[1:])
        .background_gradient("Reds", subset=cohort_amount.columns[1:], axis=None)
        .bar(subset="同期群人数", color="green")
        )
cohort_count = order.pivot_table(index="首单月份", columns="标签",
                                 values="月付费次数", aggfunc="sum",
                                 fill_value=0).rename_axis(columns="人均购买次数")
cohort_count.insert(0, "首月人均频次", cohort_count.pop("同期群人数"))
cohort_count.insert(0, "同期群人数", cohort_number.同期群人数)
cohort_count.iloc[:, 1:] = cohort_count.iloc[:,
                                             1:].divide(cohort_count.同期群人数, axis=0)
out3 = (cohort_count.style
        .format("{:.2f}", subset=cohort_count.columns[1:])
        .background_gradient("Reds", subset=cohort_count.columns[1:], axis=None)
        .bar(subset="同期群人数", color="green")
        )
outs = [out1, out2, out3]
with open("out.html", "w") as f:
    for out in outs:
        f.write(out.render())
        display(out)

dfi.export(obj=out1, filename='留存率.jpg')
dfi.export(obj=out2, filename='人均付款金额.jpg')
dfi.export(obj=out3, filename='人均购买次数.jpg')

How to apply Python in cohort analysis

The above is the detailed content of How to apply Python in cohort analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Merging Lists in Python: Choosing the Right MethodMerging Lists in Python: Choosing the Right MethodMay 14, 2025 am 12:11 AM

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

How to concatenate two lists in python 3?How to concatenate two lists in python 3?May 14, 2025 am 12:09 AM

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Python concatenate list stringsPython concatenate list stringsMay 14, 2025 am 12:08 AM

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

Python execution, what is that?Python execution, what is that?May 14, 2025 am 12:06 AM

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Python: what are the key featuresPython: what are the key featuresMay 14, 2025 am 12:02 AM

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software