開始使用免費開始

從零開始計算留存率

你已經看過如何為每月新增(acquisition)分群建立留存與平均數量的指標表。現在換你自己動手建立留存指標。

本課已為你載入 online 資料集,並指派了每月分群與分群索引。你可以在主控台列印查看。

另外,我們也用以下指令建立並載入了一個 groupby 物件為 grouping DataFrame: grouping = online.groupby(['CohortMonth', 'CohortIndex'])

本練習屬於課程

Python 的客群分群

檢視課程

練習說明

  • 選取客戶 ID 欄位,計算不重複值的數量,存成 cohort_data,並重設索引。
  • 建立一個樞紐分析表(pivot):索引放每月分群(monthly cohort),欄放分群索引(cohort index),值放客戶 ID。
  • 選取第一個欄位並存成 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=____)
編輯並執行程式碼