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.csvcontains information about about each flight including
| Variable | Description |
|---|---|
dep_time | Departure time (in the format hhmm) whereNA corresponds to a cancelled flight |
dep_delay | Departure delay, in minutes (negative for early) |
origin | Origin airport where flight starts (IATA code) |
airline | Carrier/airline name |
dest | Destination airport where flight lands (IATA code) |
flights_weather2022.csvcontains the same flight information as well as weather conditions such as
| Variable | Description |
|---|---|
visib | Visibility (in miles) |
wind_gust | Wind gust speed (in mph) |
# Import required libraries
import pandas as pd
import matplotlib.pyplot as plt
# Load the data
flights = pd.read_csv("flights2022.csv")
flights_weather = pd.read_csv("flights_weather2022.csv")
# Create a new column
flights['route'] = flights['origin'] + '-' + flights['dest']
# Calculate mean departure delay and number of canceled flights
routes_delays_cancels = flights.groupby('route').agg(
mean_dep_delay=('dep_delay', 'mean'),
total_cancellations=('dep_time', lambda x: x.isna().sum()) # Use lambda function
).reset_index() # Reset index after calculation
# Identify routes with the highest mean departure delays
top_routes_by_delay = routes_delays_cancels.sort_values('mean_dep_delay', ascending=False).head(9)
# Identify routes with the highest number of cancellations
top_routes_by_cancellations = routes_delays_cancels.sort_values('total_cancellations', ascending=False).head(9)
# Create bar graph for highest number of cancellations
top9_route_cancels_bar, ax = plt.subplots()
ax.bar(top_routes_by_cancellations['route'], top_routes_by_cancellations['total_cancellations'])
ax.set_xlabel('Route')
ax.set_ylabel('Total Cancellations')
ax.set_title('Route with Highest Number of Cancellations')
ax.set_xticklabels(top_routes_by_cancellations['route'], rotation=90)
plt.show()
plt.close()
# Calculate mean departure delay and number of canceled flights for airlines
airlines_delays_cancels = flights.groupby('airline').agg(
mean_dep_delay=('dep_delay', 'mean'),
total_cancellations=('dep_time', lambda x: x.isna().sum()) # Use lambda function
).reset_index() # Reset index after calculation
# Identify airlines with the highest mean departure delays
top_airlines_by_delay = airlines_delays_cancels.sort_values('mean_dep_delay', ascending=False).head(9)
# Identify airlines with the highest number of cancellations
top_airlines_by_cancellations = airlines_delays_cancels.sort_values('total_cancellations', ascending=False).head(9)
# Create bar graph for highest mean departure delays
top9_airline_delays_bar, ax = plt.subplots()
ax.bar(top_airlines_by_delay['airline'], top_airlines_by_delay['mean_dep_delay'])
ax.set_xlabel('Airline')
ax.set_ylabel('Mean Departure Delay')
ax.set_title('Airlines with Highest Mean Departure Delays')
ax.set_xticklabels(top_airlines_by_delay['airline'], rotation=90)
plt.show()
plt.close()
# Identify whether larger wind gusts contribute to bigger delays using lambda function with if-else
flights_weather['group'] = flights_weather['wind_gust'].apply(lambda x: '>= 10mph' if x >= 10 else '< 10 mph')
wind_grouped_data = flights_weather.groupby(['group', 'origin']).agg(
mean_dep_delay=('dep_delay', 'mean')
)
wind_response = True
print(wind_grouped_data)