Visualizing Covid-19’s impact on airport traffic in 2020

Thiago Santos Figueira
4 min readApr 14, 2021
Photo by Timo Wielink on Unsplash

Hello!

Choropleth allows us to create interactive map visualizations in Python. Today, we will create a map that shows how Covid-19 impacted airport traffic in the US and some other countries.

The dataset is available on Kaggle. There is an incredible introduction to choropleth here. Also, you can find other implementations on Kaggle.

Choropleth

It is a map visualization tool available for both Python and R that shows a map divided by regions. Choropleth allows us to create static or dynamic maps that describe change over time, for example. Choropleth uses colors to represent data, which is not as accurate as numbers would be. The official documentation is availaible here.

Understanding the Data

The data description page shows 11 columns.

  • AggregationMethod: Aggregation period used to compute this metric
  • Date: Date Traffic volume measured, in format YYYY-MM-DD.
  • Version: Version of this dataset
  • AirportName: Name of airport
  • PercentOfBaseline: Proportion of trips on this date as compared to Avg number of trips on the same day of week in baseline period i.e 1st February 2020–15th March 2020
  • Centroid: Geography representing centroid of the Airport polygon
  • City: City within which the Airport is located
  • State: State within which the Airport is located
  • ISO_3166_2: ISO-3166–2 code representing Country and Subdivision
  • Country: Country within which the Airport is located
  • Geography: Polygon of the Airport that is used to compute this metric

Visualizing Airport Impact

# Libraries
import numpy as np
import pandas as pd
import plotly as py
import plotly.express as px
import plotly.graph_objs as go
from plotly.subplots import make_subplots
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)# Read Data
df = pd.read_csv(‘covid_impact_on_airport_traffic.csv’)
# Sort by dates and get overall airport traffic by country
df_country = df.groupby([‘Date’, ‘Country’]).sum().reset_index().sort_values(‘Date’, ascending=False)
# Create Choropleth
fig = data = px.choropleth(df_country,
locations = “Country”,
locationmode = “country names”,
hover_name=”Country”,
color=”PercentOfBaseline”,
animation_frame=”Date”
)
# Set figure details
fig.update_layout(
title_text = ‘COVID-19\’s Impact on Airport Traffic Worldwide’,
title_x = 0.5,
geo=dict(
showframe = False,
showcoastlines = False,
)
)
fig.show()
Interactive map created by Chropleth

If we hover over the highlighted countries, we get the total traffic count per airport in that country.

Impact on American Airports

# Filter american airports and sort by date
df_states = df[df[‘Country’] == ‘United States of America (the)’].groupby([‘Date’, ‘State’]).sum().reset_index().sort_values(‘Date’, ascending=False)

One of the map options Choropleth gives us includes the American map. It identifies each state by its UPS State abbreviation. Therefore, we will create an additional column to accomodate these terms.

# State codes dictionary
state_codes = {
‘District of Columbia’ : ‘dc’,’Mississippi’: ‘MS’, ‘Oklahoma’: ‘OK’,
‘Delaware’: ‘DE’, ‘Minnesota’: ‘MN’, ‘Illinois’: ‘IL’, ‘Arkansas’: ‘AR’,
‘New Mexico’: ‘NM’, ‘Indiana’: ‘IN’, ‘Maryland’: ‘MD’, ‘Louisiana’: ‘LA’,
‘Idaho’: ‘ID’, ‘Wyoming’: ‘WY’, ‘Tennessee’: ‘TN’, ‘Arizona’: ‘AZ’,
‘Iowa’: ‘IA’, ‘Michigan’: ‘MI’, ‘Kansas’: ‘KS’, ‘Utah’: ‘UT’,
‘Virginia’: ‘VA’, ‘Oregon’: ‘OR’, ‘Connecticut’: ‘CT’, ‘Montana’: ‘MT’,
‘California’: ‘CA’, ‘Massachusetts’: ‘MA’, ‘West Virginia’: ‘WV’,
‘South Carolina’: ‘SC’, ‘New Hampshire’: ‘NH’, ‘Wisconsin’: ‘WI’,
‘Vermont’: ‘VT’, ‘Georgia’: ‘GA’, ‘North Dakota’: ‘ND’,
‘Pennsylvania’: ‘PA’, ‘Florida’: ‘FL’, ‘Alaska’: ‘AK’, ‘Kentucky’: ‘KY’,
‘Hawaii’: ‘HI’, ‘Nebraska’: ‘NE’, ‘Missouri’: ‘MO’, ‘Ohio’: ‘OH’,
‘Alabama’: ‘AL’, ‘Rhode Island’: ‘RI’, ‘South Dakota’: ‘SD’,
‘Colorado’: ‘CO’, ‘New Jersey’: ‘NJ’, ‘Washington’: ‘WA’,
‘North Carolina’: ‘NC’, ‘New York’: ‘NY’, ‘Texas’: ‘TX’,
‘Nevada’: ‘NV’, ‘Maine’: ‘ME’}
# Create abbreviation column
df_states[‘state_code’] = df_states[‘State’].apply(lambda x : state_codes[x])
# Create Choropleth
fig = data = px.choropleth(df_states,
locations = “state_code”,
locationmode = “USA-states”,
hover_name=”State”,
color=”PercentOfBaseline”,
animation_frame=”Date”,
)
fig.update_layout(
title_text = ‘COVID-19\’s Impact on American Airports’,
title_x = 0.5,
geo_scope=’usa’
)
fig.show()
Interactive map created by Choropleth

We can also look at the number of flights grouped by month.

from datetime import date# Change Date format from strng to date
df[“Date”] = df[“Date”].map(lambda x: date.fromisoformat(x))
df_month = pd.DataFrame(df[“Date”].map(lambda d: d.month).value_counts())
df_month = df_month.reset_index()
df_month = df_month.rename(columns={“Date”:”count”, “index”:”month”})
g = sns.barplot(data=df_month.reset_index(), y=”count”, x=”month”)
g.set_xticklabels(g.get_xticklabels(), rotation=90)
g.set_title(“records for each month”)

Conclusions

This last graph summarizes what the two previous ones show: the months of March and December have less overall traffic. The other months have on-average traffic numbers.

You can access the code and test the interactive maps on Github.

If you have any pieces of advice or feedback, you can find me at Linkedin. Thanks!

--

--