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
# Start your code here!1 - Loading and manipulating data
# Reading in CSV files
flights2022 = pd.read_csv('flights2022.csv')
flights_weather2022 = pd.read_csv('flights_weather2022.csv')flights2022.head()flights_weather2022.head()# Generate a new column called route combining origin and dest with a "-" separating
flights2022['route'] = flights2022['origin'] + '-' + flights2022['dest']
flights2022[['origin', 'dest', 'route']].head()flights2022.head()2 - Finding the average of aggregated data
# Mean departure delay and number of canceled flights for routes
routes_delays_cancels =flights2022.groupby('route').agg(mean_dep_delay=('dep_delay', 'mean'), total_cancellations=('dep_time', lambda x: x.isna().sum())).reset_index()
routes_delays_cancels# Mean departure delays and number of canceled flights for airlines
airlines_delays_cancels = flights2022.groupby('airline').agg(mean_dep_delay=('dep_delay', 'mean'), total_cancellations=('dep_time', lambda x: x.isna().sum())).reset_index()
airlines_delays_cancels3 - Identifying the airlines and routes most affected
# Top 9 highest number of cancellations by route
top_routes_by_cancellations = routes_delays_cancels.sort_values('total_cancellations', ascending=False).head(9)
top_routes_by_cancellations# Top 9 highest average departure delays by airline
top_airlines_by_delay = airlines_delays_cancels.sort_values('mean_dep_delay', ascending=False).head(9)
top_airlines_by_delay4 - Creating data visualizations