Get startedGet started for free

Calculate an aggregated target

Assume you want to construct a predictive model that predicts which donors are most likely to donate more than 50 euro in a certain month.

Given is a basetable basetable that already has one row for each donor in the population, the column donor_id represents the donor. The timeline indicates that the target should be 1 if the donor has donated more than 50 euro in January 2017 and 0 else.

The pandas dataframe gifts_201701 contains all donations in January 2017. In this exercise you will add the target column to the basetable.

This exercise is part of the course

Intermediate Predictive Analytics in Python

View Course

Exercise instructions

  • Construct gifts_summed, which has for each donor in gifts_201701 the sum of donations.
  • Derive from gifts_summed a list targets with donors that donated more than 50 Euro in the target period.
  • Add the target to the basetable.
  • Calculate and print the target incidence.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Sum of donations for each donor in gifts_201701
gifts_summed = ____.groupby("____")["____"].____().reset_index()

# List with targets
targets = list(gifts_summed["id"][____["____"] > ____])

# Add targets to the basetable
basetable["target"] = pd.Series([____ if donor_id in targets else ____ for donor_id in basetable["donor_id"]])

# Calculate and print the target incidence
print(round(____["____"].____() / ____(____), 2))
Edit and Run Code