Skip to content

Instantly share code, notes, and snippets.

@codecademydev codecademydev/script.py Secret

Created May 3, 2020
Embed
What would you like to do?
Codecademy export
import codecademylib
import pandas as pd
ad_clicks = pd.read_csv('ad_clicks.csv')
# reviewing the table
print(ad_clicks.head())
ad_clicks_count = ad_clicks.groupby('utm_source').user_id.count().reset_index()
print(ad_clicks_count)
ad_clicks['is_click'] = ~ad_clicks.ad_click_timestamp.isnull()
print(ad_clicks.head())
clicks_by_source = ad_clicks.groupby(['utm_source', 'is_click']).user_id.count().reset_index()
clicks_pivot = clicks_by_source.pivot(
columns = 'is_click',
index = 'utm_source',
values = 'user_id'
).reset_index()
print(clicks_pivot)
clicks_pivot['percent_clicked'] = clicks_pivot[True]\
/ (clicks_pivot[True] + clicks_pivot[False])
print(clicks_pivot)
print(ad_clicks.groupby('experimental_group').user_id.count())
is_click_group = ad_clicks.groupby(['experimental_group','is_click']).user_id.count().reset_index()
is_click_group_pivot = is_click_group.pivot(
columns = 'is_click',
index = 'experimental_group',
values = 'user_id'
).reset_index()
print(is_click_group_pivot)
percentage = is_click_group_pivot[True]/(is_click_group_pivot[True] + is_click_group_pivot[False])
print(percentage)
a_clicks = ad_clicks[ad_clicks.experimental_group == 'A']
b_clicks = ad_clicks[ad_clicks.experimental_group == 'B']
print(b_clicks.head())
a_b_clicks = a_clicks.groupby(['is_click', 'day']).user_id.count().reset_index()
print(a_b_clicks.head())
a_b_clicks_pivot = a_b_clicks.pivot(
columns = 'is_click',
index = 'day',
values = 'user_id'
).reset_index()
print(a_b_clicks_pivot.head())
a_percent = a_b_clicks_pivot[True] / (a_b_clicks_pivot[True] + a_b_clicks_pivot[False])
print(a_percent)
ab_clicks = b_clicks.groupby(['is_click', 'day']).user_id.count().reset_index()
print(ab_clicks.head())
ab_clicks_pivot = ab_clicks.pivot(
columns = 'is_click',
index = 'day',
values = 'user_id'
).reset_index()
print(ab_clicks_pivot.head())
b_percent = ab_clicks_pivot[True] / (ab_clicks_pivot[True] + ab_clicks_pivot[False])
print(b_percent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.