This dataset was produced by the National Oceanic and Atmospheric Administration’s (NOAA) Storm Prediction Center. It contains data on tornadoes that took place in the United States between 1950 and 2021.
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/ProgramData/Anaconda3/Library/plugins/platforms'
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.ticker import FuncFormatter
import matplotlib.ticker as ticker
import geopandas as gpd
import folium
path = "U:/"
filename = "us_tornado_dataset_1950_2021_2.csv"
df = pd.read_csv(path+filename)
df = df.rename(columns={'yr': 'Year', 'mo': 'Month', 'dy': 'Day', 'st':'State', 'len':'Length', 'wid':"Width"})
df['date'] = pd.to_datetime(df['date'])
df['Month'] = df['date'].dt.month
df['Year'] = df['date'].dt.year
df['decade'] = (df['date'].dt.year //10) * 10
df['inj'].fillna(0, inplace=True)
df['fat'].fillna(0, inplace=True)
df['Total_Inj_Fat'] = df['inj'] + df['fat']
# Load GeoData
geoData = gpd.read_file('https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/US-counties.geojson');
# Convert STATE to string
geoData['STATE'] = geoData['STATE'].astype(str);
# Remove states to be excluded
stateToRemove = ['02', '15', '72'];
geoData = geoData[~geoData.STATE.isin(stateToRemove)];
# Process tornado data
tornado_start = df.groupby(['scounty', 'State']).size().reset_index(name='Count');
tornado_start = tornado_start[~tornado_start['State'].isin(['DC', 'PR', 'AL', 'HI'])];
tornado_start = tornado_start[~tornado_start['scounty'].str.contains('xxx')];
tornado_start['scounty'] = tornado_start['scounty'].str.replace(' County', '', regex=True) \
.str.replace(' Parish', '', regex=True) \
.str.replace(' Region', '', regex=True) \
.str.replace('Saint ', 'St. ', regex=True);
# Remove invalid counties
invalid_counties = tornado_start[~tornado_start['scounty'].isin(geoData['NAME'])];
tornado_start = tornado_start[~tornado_start['scounty'].isin(invalid_counties['scounty'])];
# Merge tornado data with GeoData on county name
merged_data_start = geoData.merge(tornado_start, left_on='NAME', right_on='scounty', how='left');
# Create map
center_of_map = [38, -97];
start_map = folium.Map(location=center_of_map,
zoom_start=4,
tiles='cartodbpositron',
width='90%', height='100%',
left='5%', right='5%',
top='0%');
# Create choropleth map
ch_map_start = folium.Choropleth(geo_data=merged_data_start,
name='choropleth',
data=merged_data_start,
columns=['scounty', 'Count'],
key_on='feature.properties.NAME',
fill_color='YlGnBu',
fill_opacity=0.9,
line_opacity=0.4,
legend_name='Number of Tornadoes that Originated',
highlight=True).add_to(start_map);
# Display tooltip
ch_map_start.geojson.add_child(folium.features.GeoJsonTooltip(fields=['NAME', 'State', 'Count'],
aliases=['County: ', 'State: ', 'Tornado Count: '],
labels=True,
style=('background-color:black; color:white;')));
start_map