CommencerCommencer gratuitement

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:

Cet exercice fait partie du cours

Intermediate Predictive Analytics in Python

Afficher le cours

Instructions

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

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# 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="____")
Modifier et exécuter le code