Customer Analytics and A/B Testing in Python
Ryan Grossman
Data Scientist, EDO
import pandas as pd from datetime import timedelta # Define the most recent date in our data current_date = pd.to_datetime('2018-03-17')
# The last date a user could lapse be included max_lapse_date = current_date - timedelta(days=14)
# Filter down to only eligible users conv_sub_data = sub_data_demo[ sub_data_demo.lapse_date < max_lapse_date]
# How many days passed before the user subscribed sub_time = conv_sub_data.subscription_date - conv_sub_data.lapse_date
# Save this value in our dataframe conv_sub_data['sub_time'] = sub_time
sub_time
from a timedelta
to an int
# Extract the days field from the sub_time
conv_sub_data['sub_time'] = conv_sub_data.sub_time.dt.days
# filter to users who have did not subscribe in the right window
conv_base = conv_sub_data[(conv_sub_data.sub_time.notnull()) | \
(conv_sub_data.sub_time > 7)]
total_users = len(conv_base)
total_subs = np.where(conv_sub_data.sub_time.notnull() & \
(conv_base.sub_time <= 14), 1, 0)
total_subs = sum(total_subs)
conversion_rate = total_subs / total_users
0.0095877277085330784
pandas.read_csv(...,
parse_dates=False,
infer_datetime_format=False,
keep_date_col=False,
date_parser=None,
dayfirst=False,...)
customer_demographics = pd.read_csv('customer_demographics.csv',
parse_dates=True,
infer_datetime_format=True)
uid reg_date device gender country age
0 54030035.0 2017-06-29 and M USA 19
1 72574201.0 2018-03-05 iOS F TUR 22
2 64187558.0 2016-02-07 iOS M USA 16
3 92513925.0 2017-05-25 and M BRA 41
4 99231338.0 2017-03-26 iOS M FRA 59
pandas.to_datetime(arg, errors='raise', ..., format=None, ...)
strftime
1993-01-27 --"%Y-%m-%d"
05/13/2017 05:45:37 -- "%m/%d/%Y %H:%M:%S"
September 01, 2017 -- "%B %d, %Y"
Customer Analytics and A/B Testing in Python