Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created July 14, 2021 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/95f6b1081b0cced02020372f4657dbcc to your computer and use it in GitHub Desktop.
Save codecademydev/95f6b1081b0cced02020372f4657dbcc to your computer and use it in GitHub Desktop.
Codecademy export
import codecademylib
import pandas as pd
visits = pd.read_csv('visits.csv',
parse_dates=[1])
cart = pd.read_csv('cart.csv',
parse_dates=[1])
checkout = pd.read_csv('checkout.csv',
parse_dates=[1])
purchase = pd.read_csv('purchase.csv',
parse_dates=[1])
# print(visits.head(), cart.head(), checkout.head(), purchase.head())
# print(visits)
# Calculate percentage of not carting
vi_cart = pd.merge(visits, cart, how='left')
# notcart_percentage = 100 - (float(vi_cart.cart_time.count())/float(vi_cart.visit_time.count()))*100
null_cart = vi_cart[vi_cart.cart_time.isnull()]
notcart_percentage = (float(len(null_cart))/len(vi_cart))*100
print(notcart_percentage)
# print(vi_cart)
# Calculate percentage of not checking out
cart_checkout = pd.merge(cart, checkout, how='left')
# notcheck_percentage = 100 - round((float(cart_checkout.checkout_time.count())/float(cart_checkout.cart_time.count()))*100, 2)
null_checkout = cart_checkout[cart_checkout.checkout_time.isnull()]
notcheck_percentage = (float(len(null_checkout))/len(cart_checkout))*100
print(notcheck_percentage)
# Calculate percentage of not buying
check_purchase = pd.merge(checkout, purchase, how='left')
# not_buy_percentage = 100 - float(check_purchase.purchase_time.count())/float(check_purchase.checkout_time.count())*100
null_purchase = check_purchase[check_purchase.purchase_time.isnull()]
not_buy_percentage = (float(len(null_purchase))/len(check_purchase))*100
print(not_buy_percentage)
# Merge all data
all_data = visits.merge(cart, how='left').merge(checkout, how='left').merge(purchase, how='left').reset_index()
# print(all_data.head(30))
all_data['time_to_purchase'] = all_data.purchase_time - all_data.visit_time
# print(all_data.head(40))
avg_time_to_purchase = all_data.time_to_purchase.mean()
print(avg_time_to_purchase)
# print(all_data.time_to_purchase)
# print(all_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment