Skip to content

Online shopping decisions rely on how consumers engage with online store content. You work for a new startup company that has just launched a new online shopping website. The marketing team asks you, a new data scientist, to review a dataset of online shoppers' purchasing intentions gathered over the last year. Specifically, the team wants you to generate some insights into customer browsing behaviors in November and December, the busiest months for shoppers. You have decided to identify two groups of customers: those with a low purchase rate and returning customers. After identifying these groups, you want to determine the probability that any of these customers will make a purchase in a new marketing campaign to help gauge potential success for next year's sales.

Data description:

You are given an online_shopping_session_data.csv that contains several columns about each shopping session. Each shopping session corresponded to a single user.

ColumnDescription
SessionIDunique session ID
Administrativenumber of pages visited related to the customer account
Administrative_Durationtotal amount of time spent (in seconds) on administrative pages
Informationalnumber of pages visited related to the website and the company
Informational_Durationtotal amount of time spent (in seconds) on informational pages
ProductRelatednumber of pages visited related to available products
ProductRelated_Durationtotal amount of time spent (in seconds) on product-related pages
BounceRatesaverage bounce rate of pages visited by the customer
ExitRatesaverage exit rate of pages visited by the customer
PageValuesaverage page value of pages visited by the customer
SpecialDaycloseness of the site visiting time to a specific special day
Weekendindicator whether the session is on a weekend
Monthmonth of the session date
CustomerTypecustomer type
Purchaseclass label whether the customer make a purchase
# Import required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

# Load and view you data
shopping_data = pd.read_csv('online_shopping_session_data.csv')
shopping_data.head()
# Start your code here!
# Use as many cells as you like
shopping_data.info()
shopping_data['CustomerType'].value_counts()

1 - Calculate online purchase rates by customer

# Subset the DataFrame for November and December shopping sessions
shopping_nd = shopping_data[shopping_data['Month'].isin(['Nov', 'Dec'])]
shopping_nd.head()
shopping_nd.sample(5)
shopping_nd.shape
# Calculate the frequency of sessions by customer type
# Get row counts for CustomerTypes
session_count = shopping_nd.groupby('CustomerType')['Purchase'].value_counts()
print(session_count)
# Get total number of sessions by CustomerType
total_new = np.sum(session_count['New_Customer'])
total_returning = np.sum(session_count['Returning_Customer'])
print(total_new)
print(total_returning)
# Get number of purchases by CustomerType
purchase_new = session_count[('New_Customer', 1)]
purchase_returning = session_count[('Returning_Customer', 1)]
print(purchase_new)
print(purchase_returning)
# Calculate purchase rates by customer type
ratio_new = purchase_new / total_new
ratio_returning = purchase_returning / total_returning
print(ratio_new)
print(ratio_returning)
# Create dictionary of customer types and ratios
purchase_rates = {'Returning_Customer': ratio_returning, 'New_Customer': ratio_new}
print(purchase_rates)