本文涉及数据下载链接。
二、数据预处理 2.1 读取数据 import pandas as pddf = pd.read_csv('your_path/Year 2010-2011.csv', encoding='ISO-8859-1')df.head() 2.2 检查数据 检查空值情况 df.isna().sum() # 结果Invoice 0StockCode 0Description 1454Quantity 0InvoiceDate 0Price 0Customer ID 135080Country 0dtype: int64 检查重复情况 df.duplicated().sum()# 结果5268 检查数值分布 df.describe() 2.3 小结:进行数据预处理 df = df.drop_duplicates()df = df.dropna()df = df.query('Quantity>0 & Price>0') 三、可视化 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') 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()) 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)) 3.4 做成热力图 import seaborn as snsimport matplotlib.pyplot as pltimport matplotlib.colors as mcolorswith 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()【python笔记】客户运营 - cohort分析,格力一夜完成河北市场系统切换
0evadmin
编程语言
11
文件名:【python笔记】客户运营 - cohort分析,格力一夜完成河北市场系统切换
【python笔记】客户运营 - cohort分析
一、数据
同类推荐
-

【Python 千题 —— 基础篇】减法计算,lgp880
查看 -

【Python_GraphicsView 学习笔记(一)】Graphics View框架的基本介绍,cs单机版大灾变
查看 -

【Python】PySpark 数据计算 ⑤ ( RDD#sortBy方法 - 排序 RDD 中的元素 ),geak eye
查看 -

【Python】Pyinstaller打包Linux运行文件,暴露配置文件,泡泡手机网(python打包linux应用)
查看 -

【Python】SqlmapAPI调用实现自动化SQL注入安全检测,htc p860
查看 -

【Python】np.save()和np.load()函数详解和示例,product.pcpop.com(python np.save)
查看 -

【Python】translate包报错RuntimeError- generator raised StopIteration,硕美科e95
查看 -

【Python】【数据结构和算法】查找最大或最小的N个元素,kingmax存储卡
查看 -

【Python】字符串和变量拼接的写法,中国移动m701(python 字符串和变量拼接)
查看
控制面板
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接