Skip to content
Exploratory Data Analysis in Python for Absolute Beginners
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.
To consult the solution, head over to the file browser and select notebook-solution.ipynb.
# Import the required packages
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as pxImport the data
# Import hotel_bookings_clean_v2.csv
hotel_booking=pd.read_csv("hotel_bookings_clean_v2.csv")Basic exploration
# Show dimensions
hotel_booking.shape# Are there missing values?
hotel_booking.isna().sum()# Describe with summary statistics
hotel_booking.describe()# How many bookings were canceled?
hotel_booking["is_canceled"].sum()Are the cancellation rates different during different times of the year?
# Calculate and plot cancellations every month
cancellations = hotel_booking.filter(['arrival_date_month', 'is_canceled']).groupby(by = 'arrival_date_month', as_index=False).sum()
# Create bar chart of cancellations per month
plt.bar(cancellations["arrival_date_month"],cancellations["is_canceled"])
plt.xlabel("arrival_date_month")
plt.title("Cancellations Per Month")# Calculate and plot total bookings every month
total_bookings=hotel_booking.groupby("arrival_date_month")["is_canceled"].count().reset_index()
total_bookings.rename(columns={"is_canceled":"Total Bookings"},inplace=True)
# Create bar chart of total bookings per month
px.bar(total_bookings,x="arrival_date_month",y="Total Bookings")# Calculate cancellation rates every month
cancellation_rates=hotel_booking.groupby("arrival_date_month").agg({"is_canceled":"sum","lead_time":"count"})
cancellation_rates.rename(columns={"is_canceled":"no of cancellations","lead_time":"total_bookings"},inplace=True)
cancellation_rates["pct_canceled"]=cancellation_rates["no of cancellations"]/cancellation_rates["total_bookings"]
# Create bar chart of cancellation rate every month
sns.barplot(data=cancellation_rates,x="arrival_date_month",y="pct_canceled")
#px.bar(merged, x='arrival_date_month', y='pct_canceled')Does the amount of nights influence the cancellation rate?
# Prepare the data
hotel_booking["Total_stay"]=hotel_booking["stays_in_weekend_nights"]+hotel_booking["stays_in_week_nights"]
Total_stays=hotel_booking[["is_canceled","Total_stay","lead_time"]]
Total_stays