ComenzarEmpieza gratis

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.

Este ejercicio forma parte del curso

Intermediate Predictive Analytics in Python

Ver curso

Instrucciones del ejercicio

  • 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.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

# 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())
Editar y ejecutar código