LoslegenKostenlos loslegen

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:

Diese Übung ist Teil des Kurses

Intermediate Predictive Analytics in Python

Kurs anzeigen

Anleitung zur Übung

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

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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="____")
Code bearbeiten und ausführen