从零开始计算留存率
您已经学习了如何为按月获取的新客分组创建留存率与平均购买数量的度量表。现在请您亲自构建留存度量。
本课中已为您加载了带有月度分组与分组索引的 online 数据集。可在控制台中打印查看。
另外,我们已经通过以下命令创建并加载了一个 groupby 对象为 grouping DataFrame:
grouping = online.groupby(['CohortMonth', 'CohortIndex'])
本练习是课程的一部分
Python 中的客户细分
练习说明
- 选择客户 ID 列,计算其唯一值的数量,保存为
cohort_data,并重置索引。 - 创建一个透视表:行索引为月度分组,列为分组索引,值为客户 ID 计数。
- 选择第一列并保存到
cohort_sizes。 - 将各期的分组客户数按行除以
cohort_sizes。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Count the number of unique values per customer ID
cohort_data = grouping[____].apply(pd.Series.____).reset_index()
# Create a pivot
cohort_counts = cohort_data.____(index=____, columns=____, values=____)
# Select the first column and store it to cohort_sizes
cohort_sizes = cohort_counts.iloc[:,____]
# Divide the cohort count by cohort sizes along the rows
retention = cohort_counts.____(____, axis=____)