码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【python笔记】客户运营 - cohort分析


    一、数据

    本文涉及数据下载链接。

    二、数据预处理

    2.1 读取数据

    import pandas as pd
    
    df = pd.read_csv('your_path/Year 2010-2011.csv', encoding='ISO-8859-1')
    df.head()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    2.2 检查数据

    • 检查空值情况
    df.isna().sum() 
    # 结果
    Invoice             0
    StockCode           0
    Description      1454
    Quantity            0
    InvoiceDate         0
    Price               0
    Customer ID    135080
    Country             0
    dtype: int64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 检查重复情况
    df.duplicated().sum()
    # 结果
    5268
    
    • 1
    • 2
    • 3
    • 检查数值分布
    df.describe()
    
    • 1

    df.describe()

    2.3 小结:进行数据预处理

    df = df.drop_duplicates()
    df = df.dropna()
    df = df.query('Quantity>0 & Price>0')
    
    • 1
    • 2
    • 3

    三、可视化

    3.1 把时间调整成月份

    df['date_new'] = df['InvoiceDate'].copy()
    df['date_new'] = pd.to_datetime(df.date_new, format='%m/%d/%Y %H:%M')
    df['yyyymm'] = df.date_new.dt.to_period('M')
    
    • 1
    • 2
    • 3

    3.2 获取所需字段

    df['start_month'] = df.groupby('Customer ID')['yyyymm'].transform(min)
    df['lasted_months'] = (df.yyyymm- df.start_month).apply(lambda x: x.n)
    
    print(df.head())
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    3.3 做成客户留存表

    pt = df.pivot_table(index='start_month', columns='lasted_months', values='Customer ID', aggfunc='nunique')
    pt_cohort = pt.divide(pt.iloc[:,0], axis=0)
    
    print(pt_cohort.head(2))
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    3.4 做成热力图

    import seaborn as sns
    import matplotlib.pyplot as plt
    import matplotlib.colors as mcolors
    
    with sns.axes_style('white'):
        plt.rcParams['font.family'] = 'simhei'
        fig, axes = plt.subplots(1, 2, figsize=(12, 8), 
        						sharey=True, 
        						gridspec_kw={'width_ratios': [1, 11]})
    
        sns.heatmap(pt_cohort, annot=True, fmt='.0%', ax=axes[1])
        axes[1].set_title('月度Cohorts: 客户留存', fontsize=16)
        axes[1].set(xlabel='# of periods', ylabel='')
    
        sns.heatmap(pd.DataFrame(pt.iloc[:,0]),
                    annot=True, fmt='g',
                    cbar=False,
                    cmap=mcolors.ListedColormap('white'),
                    ax=axes[0])
        fig.tight_layout()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

  • 相关阅读:
    计算机毕业设计springboot+vue+elementUI汽车车辆充电桩管理系统
    fatal: unable to access ‘https://github.com/alibaba/flutter_boost.git/
    tcp协议讲解
    【教学类-07-03】20221106《破译电话号码-2款图形篇+自制(PDF打印)》(大班主题《我要上小学》)
    SpringIOC容器Bean对象的实例化模拟
    【01背包问题】
    经典二分法查找的进阶题目——LeetCode33 搜索旋转排序数组
    入门JavaWeb之 Response 验证码和重定向
    umi4项目:支持适配IE浏览器
    Java网络编程套接字
  • 原文地址:https://blog.csdn.net/htuhxf/article/details/134521440
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号