Skip to content

EDA in Python for Absolute Beginners

In this live training, we'll be doing Exploratory Data Analysis, or EDA, on a dataset that consists of hotel booking data. It includes many details about the bookings, including room specifications, the length of stay, the time between the booking and the stay, whether the booking was canceled, and how the booking was made. The data was gathered between July 2015 and August 2017. You can consult the appendices at the bottom of the notebook for citations and an overview of all variables.

# Import the required packages
import pandas as pd
import plotly.express as px

Import the data

# Import hotel_bookings_clean_v2.csv
data = pd.read_csv("hotel_bookings_clean_v2.csv")
data.head()

Basic exploration

# Show dimensions
#shape -> returns rows x cols count [number of observations x number of variables]
data.shape
# Are there missing values?
# sum() will return the count for all cols
data.isnull().sum()
# Describe with summary statistics
data.describe()
# How many bookings were canceled?
n_cancellations = data['is_canceled'].sum()

# % cancelations
pct_cancelations = data['is_canceled'].mean()

print(f"{n_cancellations} Bookings were cancelled, which is {pct_cancelations*100:.2f}% of all bookings")

Are the cancellation rates different during different times of the year?

# Calculate and plot cancellations every month
cancellations = data\
    .filter(['arrival_date_month', 'is_canceled'])\
    .groupby(by = 'arrival_date_month', as_index=False)\
    .sum()
# as_index = True will become an index in the new dataframe 

cancellations
# Create bar chart of cancellations per month
px.bar(cancellations, x ="arrival_date_month", y = "is_canceled")
# Calculate and plot total bookings every month
total_bookings = data\
    .filter(['arrival_date_month', 'is_canceled'])\
    .groupby(by = 'arrival_date_month', as_index=False)\
    .count()\
    .rename(columns={'is_canceled':'total_bookings'})

total_bookings

# Create bar chart of total bookings per month
px.bar(total_bookings, x="arrival_date_month" ,y ="total_bookings")
# Calculate cancellation rates every month
# perform merge between cancelations and total_bookings
merged = pd.merge(cancellations, total_bookings, on = 'arrival_date_month')
merged['cancellation_rate'] = merged['is_canceled']/merged['total_bookings']

merged
# Create bar chart of cancellation rate every month
px.bar(merged, x='arrival_date_month', y='cancellation_rate')

Does the amount of nights influence the cancellation rate?

# Prepare the data
df_sel = data\
        .assign(stays = lambda x: x['stays_in_week_nights'] + x['stays_in_weekend_nights'])\
        .query('stays < 15')
df_sel