Aan de slagGa gratis aan de slag

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:

Deze oefening maakt deel uit van de cursus

Intermediate Predictive Analytics in Python

Cursus bekijken

Oefeninstructies

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

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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 bewerken en uitvoeren