Ratio of last month's and last year's average
An interesting variable to add to the basetable
is the average gift a donor donated last month compared to the average gift a donor donated last year. In this exercise, you will learn how to add this variable to the basetable. The gifts made last month by donors are already selected in gifts_last_month
and the gifts made last year are selected in gifts_last_year
.
This exercise is part of the course
Intermediate Predictive Analytics in Python
Exercise instructions
- Calculate for each donor in
gifts_last_month
the average donation in the last month. - Calculate for each donor in
gifts_last_year
the average donation in the last year. - Add the average donation last month and average donation last year to the basetable.
- Calculate the ratio of last month's and last year's average donation in the basetable.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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())