Customer Analytics and A/B Testing in Python
Ryan Grossman
Data Scientist, EDO
import pandas as pd
from datetime import datetime, timedelta
current_date = pd.to_datetime('2018-03-17')
# What is the maximum lapse date in our data
print(sub_data_demo.lapse_date.max())
'2018-03-17'
# latest lapse date: a week before today max_lapse_date = current_date - timedelta(days=7)
# restrict to users lapsed before max_lapse_date conv_sub_data = sub_data_demo[(sub_data_demo.lapse_date < max_lapse_date)]
# count the users remaining in our data total_users_count = conv_sub_data.price.count() print(total_users_count)
2787
# latest subscription date: within 7 days of lapsing max_sub_date = conv_sub_data.lapse_date + timedelta(days=7)
# filter the users with non-zero subscription price # who subscribed before max_sub_date total_subs = conv_sub_data[ (conv_sub_data.price > 0) & (conv_sub_data.subscription_date <= max_sub_date) ]
# count the users remaining in our data total_subs_count = total_subs.price.count() print(total_subs_count)
648
# calculate the conversion rate with our previous values conversion_rate = total_subs_count / total_users_count
print(conversion_rate)
0.23250807319698599
# Create a copy of our dataframe
conv_sub_data = conv_sub_data.copy()
# keep users who lapsed prior to the last 14 days (2 weeks)
max_lapse_date = current_date - timedelta(days=14)
conv_sub_data = sub_data_demo[
(sub_data_demo.lapse_date <= max_lapse_date)
]
# Find the days between lapse and subscription if they # subscribed ... and pd.NaT otherwise sub_time = np.where(
# if: a subscription date exists conv_sub_data.subscription_date.notnull(), # then: find how many days since their lapse (conv_sub_data.subscription_date - conv_sub_data.lapse_date).dt.days, # else: set the value to pd.NaT pd.NaT)
# create a new column 'sub_time' conv_sub_data['sub_time'] = sub_time
gcr7()
, gcr14()
: calculate the 7 and 14 day conversion rates# group by the relevant cohorts purchase_cohorts = conv_sub_data.groupby(by=['gender', 'device'], as_index=False)
# find the conversion rate for each cohort using gcr7,gcr14 purchase_cohorts.agg({sub_time: [gcr7,gcr14]})
gender device sub_time
gcr7 gcr14
0 F and 0.221963 0.230140
1 F iOS 0.229310 0.237931
2 M and 0.252349 0.257718
3 M iOS 0.218045 0.225564
Infinitely many potential KPIs
How long does it take to determine
Leverage Exploratory Data Analysis
Keep In Mind How do these KPIs and my Business goals relate
How does this KPI evolve over time?
See how changes can impact different groups differently
Customer Analytics and A/B Testing in Python