開始使用免費開始

上個月與去年的平均金額比率

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())
編輯並執行程式碼