开始使用免费开始使用

上个月与去年平均值的比率

一个值得添加到 basetable 的有趣变量是:捐赠者在上个月的平均捐赠额,和其在去年平均捐赠额的比较。在本练习中,您将学习如何把这个变量加入 basetable。捐赠者上个月的捐赠已在 gifts_last_month 中选出,去年的捐赠已在 gifts_last_year 中选出。

本练习是课程的一部分

Python 预测分析进阶

查看课程

练习说明

  • 计算 gifts_last_month 中每位捐赠者上个月的平均捐赠额。
  • 计算 gifts_last_year 中每位捐赠者去年的平均捐赠额。
  • 将上个月的平均捐赠额和去年的平均捐赠额添加到 basetable。
  • 在 basetable 中计算上个月平均值与去年平均值的比率。

交互式实操练习

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

# Average gift last month for each donor
average_gift_last_month = gifts_last_month.____("____")["____"].____().reset_index()
average_gift_last_month.columns = ["donor_ID", "mean_gift_last_month"]

# Average gift last year for each donor
average_gift_last_year = ____
average_gift_last_year.columns = ["donor_ID", "mean_gift_last_year"]

# Add average gift last month and year to basetable
basetable = pd.merge(____, ____, on="____", how="____")
basetable = pd.merge(____, ____, on="____", how="____")

# Calculate ratio of last month's and last year's average
basetable["ratio_month_year"] = basetable["____"] / basetable["____"]
print(basetable.head())
编辑并运行代码