ComeçarComece de graça

Recency of donations

Another interesting variable to add to the basetable is the recency, the time since the last donation. Given are two dataframes basetable and gifts, that contain the early stage basetable and the gifts made by donors over time. Add for each donor in the population the recency in days.

Keep in mind the following timeline that is used to calculate the basetable:

Este exercício faz parte do curso

Intermediate Predictive Analytics in Python

Ver curso

Instruções do exercício

  • Fill out the reference date.
  • Create a pandas dataframe gifts_before_reference that contains gifts made before the reference date.
  • Create a pandas dataframe last_gift that has for each donor in gifts_before_reference the last donation made and add the recency in days to last_gift.
  • Add this recency to the basetable.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Reference date to calculate the recency
reference_date = datetime.date(____, ____, ____)

# Select gifts made before the reference date
gifts_before_reference = gifts[(gifts["date"] < ____)]

# Latest gift per donor in 2017
last_gift = gifts_before_reference.groupby(["____"])["____"].____().reset_index()
last_gift["recency"] = ____ - ____["____"]   

# Add recency to the basetable
basetable = pd.merge(____, ____[["id", "recency"]], how="____")
Editar e executar o código