計算平均價格
現在你要計算平均價格這個指標,並分析不同時間與不同同群之間的消費行為是否有差異。
本課程已為你載入 online 資料集,且加入了月度同群與同群索引。你可以在主控台列印查看。
本練習屬於課程
Python 的客群分群
練習說明
- 建立一個
groupby物件,並以月度同群與同群索引組成的清單作為參數。 - 選取單價欄位,計算平均值,並將結果存入
cohort_data。 - 重設
cohort_dataDataFrame 的索引。 - 建立一個 pivot:索引用月度同群,欄用同群索引,值為單價,並印出結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a groupby object and pass the monthly cohort and cohort index as a list
grouping = online.groupby([____, ____])
# Calculate the average of the unit price column
cohort_data = grouping[____].____()
# Reset the index of cohort_data
cohort_data = cohort_data.____()
# Create a pivot
average_price = cohort_data.____(index=____, columns=____, values=____)
print(average_price.round(1))