Get startedGet started for free

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:

This exercise is part of the course

Intermediate Predictive Analytics in Python

View Course

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

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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="____")
Edit and Run Code