# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # Pandas basic manipulation
# ## A/B Testing for ShoeFly.com

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline

df=pd.read_csv("./data/ad_clicks.csv")
df.sample(10)

# ## Check datatypes of columns

df.dtypes

# ## Check NAs

df.isnull().sum()  # for now, we do not perform any operation on the NAs

# ## Descriptive Statistics

df.describe().T

# ## Transform day to weekday

# df.day.head(1)
df['weekday'] = df.day.str.split('-').str.get(1)
df.sample(5)

# # Click Thru Rate

df['is_click'] = ~df.ad_click_timestamp.isnull()
df.sample(4)[['is_click', 'ad_click_timestamp']]

# # Calculate the click thru rate by platform

platform_visit = df.groupby(['utm_source', 'is_click'])['user_id'].count().reset_index().rename(columns={'user_id':'count'})
platform_visit

# ## Pivot the table to see more clearly

pltform_pivoted = platform_visit.pivot(
    index = 'utm_source', 
    columns = 'is_click', 
    values = 'count'
).reset_index()
platform_visit

# ## Click thru rate

pltform_pivoted['click_thru_rate'] = pltform_pivoted.apply(lambda row :\ 
                                                           row[True]/(row[True] + row[False]),\
                                                           axis=1)
pltform_pivoted

# # Simple A/B testing

grp_a = df[df.experimental_group == "A"]
grp_a.sample(5)

grp_a=grp_a.groupby(['is_click', 'day'])['user_id'].count().reset_index().rename(columns = {'user_id':'count'})

grp_a = grp_a.pivot(index='day', columns='is_click', values='count').reset_index()
click_thru_rate_a = grp_a[True]/(grp_a[True] + grp_a[False])
print(type(click_thru_rate_a))

# +
grp_b = df[df.experimental_group == "B"]
grp_b.sample(5)
grp_b=grp_b.groupby(['is_click', 'day'])['user_id'].count().reset_index().rename(columns = {'user_id':'count'})

grp_b = grp_b.pivot(index='day', columns='is_click', values='count').reset_index()
click_thru_rate_b = grp_b[True]/(grp_b[True] + grp_b[False])
print(type(click_thru_rate_b))
# -

df = pd.concat([click_thru_rate_a, click_thru_rate_b], axis=1).rename(columns = {0:'A', 1:'B'})

labels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
ax = plt.subplot(1,1,1)
plt.plot(df.A, marker='o', linestyle='--')
plt.plot(df.B, marker='*')
plt.legend(['GroupA', 'GroupB'], loc=0)
plt,
plt.title('Simple A/B tests')
ax.set_xticks(range(len(labels)))
ax.set_xticklabels(labels, rotation=45)
plt.figure(figsize=(10,4))



StarcoderdataJupyterScriptsDedupFiltered