开始使用免费开始使用

计算平均价格

现在,您将计算平均价格这一指标,并分析不同时间和不同分群之间的购物模式是否存在差异。

本课已为您加载 online 数据集,并包含按月分群以及分群索引。您可以在控制台中打印查看。

本练习是课程的一部分

Python 中的客户细分

查看课程

练习说明

  • 创建一个 groupby 对象,并将月度分群和分群索引以列表形式传入。
  • 选择单价列,计算平均值,并将结果存入 cohort_data
  • 重置 cohort_data DataFrame 的索引。
  • 创建一个透视表:行索引为月度分群,列为分群索引,取值为单价,并打印结果。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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))
编辑并运行代码