Adding evolutions

Intermediate Predictive Analytics in Python

Nele Verbiest

Senior Data Scientist @PythonPredictions

Motivation for evolutions (1)

Intermediate Predictive Analytics in Python

Motivation for evolutions (2)

Intermediate Predictive Analytics in Python

Motivation for evolutions (3)

Intermediate Predictive Analytics in Python

Motivation for evolutions (4)

Intermediate Predictive Analytics in Python

Motivation for evolutions (5)

Intermediate Predictive Analytics in Python

Adding evolutions to the basetable (1)

# Reference dates
start_2017 = datetime.date(2017,1,1)
start_2016 = datetime.date(2016,1,1)
start_2015 = datetime.date(2015,1,1)
# Gifts last month and last year
gifts_2016 = gifts[
   (gifts["date"]<start_2017) 
    & (gifts["date"]>=start_2016)]

gifts_2015_and_2016 = gifts[
   (gifts["date"]<start_2017) 
    & (gifts["date"]>=start_2015)]
Intermediate Predictive Analytics in Python

Adding evolutions to the basetable (2)

# Number of gifts in these periods per donor
number_gifts_2016 = gifts_2016.groupby("id")["amount"].size().reset_index()
number_gifts_2016.columns = ["donor_ID", "number_gifts_2016"]

number_gifts_2015_and_2016 = gifts_2015_and_2016 .groupby("id")["amount"].size().reset_index() number_gifts_2015_and_2016.columns = ["donor_ID", "number_gifts_2015_and_2016"]
Intermediate Predictive Analytics in Python

Adding evolutions to the basetable (3)

# Add these numbers to the basetable
basetable = pd.merge(basetable, 
                     number_gifts_2016, 
                     on="donor_ID",
                     how = "left")
basetable = pd.merge(basetable, 
                     number_gifts_2015_and_2016, 
                     on="donor_ID",
                     how = "left")

# Calculate ratio of last month's and last year's average basetable["ratio_2015_to_2015_and_2016"] = basetable["number_gifts_2016"] / basetable["number_gifts_2015_and_2016"]
Intermediate Predictive Analytics in Python

Adding evolutions to the basetable (4)

print(basetable.head())
donor_id number_gifts_2016 number_gifts_2015_and_2016 ratio_2015_to_2015_and_2016
1        Na                5                          Na
2        9                 12                         0.75
3        3                 6                          0.5
Intermediate Predictive Analytics in Python

Let's practice!

Intermediate Predictive Analytics in Python

Preparing Video For Download...