Skip to content

A prominent airline company in the Pacific Northwest has accumulated extensive data related to flights and weather patterns and needs to understand the factors influencing the departure delays and cancellations to benefit both airlines and passengers. As the data analyst on the team, you decide to embark on this analytical project.

The aviation industry is dynamic with various variables impacting flight operations. To ensure the relevance and applicability of your findings, you choose to focus solely on flights from the 'pnwflights2022' datasets available from the ModernDive team exported as CSV files. These datasets provide comprehensive information on flights departing in the first half of 2022 from both of the two major airports in this region: SEA (Seattle-Tacoma International Airport) and PDX (Portland International Airport):

  • flights2022.csv contains information about about each flight including
VariableDescription
dep_timeDeparture time (in the format hhmm) whereNA corresponds to a cancelled flight
dep_delayDeparture delay, in minutes (negative for early)
originOrigin airport where flight starts (IATA code)
airlineCarrier/airline name
destDestination airport where flight lands (IATA code)
  • flights_weather2022.csv contains the same flight information as well as weather conditions such as
VariableDescription
visibVisibility (in miles)
wind_gustWind gust speed (in mph)
# Import required libraries
import pandas as pd
import matplotlib.pyplot as plt

# Start your code here!
df_flights2022 = pd.read_csv('flights2022.csv')
df_weather2020 = pd.read_csv('flights_weather2022.csv')

df_flights2022.head()
# Checking dtype of columns etc.

df_flights2022.info()
#Generate a new column called route combining origin and dest with a "-" separating

df_flights2022['route'] = df_flights2022['origin'] + "-" + df_flights2022['dest']
#  the top 9 highest number of cancellations by route

cancelled_func = lambda x: x.isna().sum()

routes_delays_cancels = df_flights2022.groupby('route').agg(mean_dep_delay=('dep_delay','mean'), total_cancellations=('dep_time', cancelled_func)).reset_index()
route9 = routes_delays_cancels.sort_values('total_cancellations', ascending=False).head(9)

# Top routes by delay and cancellations

top_routes_by_delay = routes_delays_cancels.sort_values("mean_dep_delay", ascending=False).head(9)

top_routes_by_cancellations = routes_delays_cancels.sort_values('total_cancellations', ascending=False).head(9)
# the top 9 highest average departure delays by airline

airlines_delays_cancels = df_flights2022.groupby('airline').agg(mean_dep_delay=('dep_delay','mean'), total_cancellations=('dep_time', cancelled_func)).reset_index()
airlines_delays_cancels.sort_values('mean_dep_delay', ascending=False).head(9)

top_airlines_by_delay = airlines_delays_cancels.sort_values("mean_dep_delay", ascending=False).head(9)

top_airlines_by_cancellations = airlines_delays_cancels.sort_values("total_cancellations", ascending=False).head(9)

route9 = routes_delays_cancels.sort_values('total_cancellations', ascending=False).head(9)

top9_route_cancels_bar, ax = plt.subplots(figsize=(10,6))

ax.bar(route9.route, route9.total_cancellations, width=0.7)
ax.set_xlabel('Route', size=12, color='b')
ax.set_ylabel('Num Cancelled Flights', size=12, color='b')
ax.set_title('Top 9 Highest Number of Cancellations by Route', y=1.05, color='darkblue')
ax.set_xticklabels(route9.route, size=10, rotation=0)
ax.set_facecolor('#d8dcd6')
ax.spines[['right', 'top']].set_visible(False)
plt.show()
airline9 = airlines_delays_cancels.sort_values('mean_dep_delay', ascending=False).head(9)

top9_airline_delays_bar, ax = plt.subplots(figsize=(10,6))

ax.bar(airline9.airline, airline9.mean_dep_delay, width=0.7)
ax.set_xlabel('Airline', size=12, color='b')
ax.set_ylabel('Avg Delay, minutes', size=12, color='b')
ax.set_title('Top 9 Highest Average Departure Delays by Airline', y=1.05, color='darkblue')
ax.set_xticklabels(airline9.airline, size=8)
plt.setp( ax.xaxis.get_majorticklabels(), rotation=30, ha="right" )
ax.set_facecolor('#d8dcd6')
ax.spines[['right', 'top']].set_visible(False)
plt.show()
df_weather2020.head()
df_weather2020.wind_gust.describe()
# Create a new column with the flights_weather2022 data, splitting the data at 10 miles per hour 

wind10_func = lambda x: '>= 10 mph' if x >= 10 else '< 10 mph'

df_weather2020['wind10'] = df_weather2020['wind_gust'].apply(wind10_func)

# Determine if 10 mile per hour wind gusts or more have a larger average departure delay for both of SEA and PDX, setting wind_response to True if so and False if not.

wind_delay = df_weather2020.groupby(['origin', 'wind10']).agg(mean_dep_delay=('dep_delay', 'mean')).reset_index()
wind_delay.head()
wind_response = True