Reputation: 25
I'm using GeoPandas in Python to create a heatmap of the state of Florida from a given CSV dataset and a shapefile of Florida:
This is the code I have for displaying the state from the shapefile, and the CSV dataset contents (It's a list of Covid cases in the Florida counties),
The shapefile also conveniently has data for the the name of the counties along with their respective polygons:
My plan is to parse through the CSV and keep track of how many cases there are for each county then build a heatmap from that, but I'm unsure of how to work with shapefiles.
Upvotes: 0
Views: 654
Reputation: 31166
merge()
to combine / join geometry and COVID dataimport geopandas as gpd
import pandas as pd
gdf = gpd.read_file(
"https://www2.census.gov/geo/tiger/TIGER2016/COUSUB/tl_2016_12_cousub.zip"
)
# NY Times data is by county not sub-county. rollup geometry to county
gdf_county = (
gdf.dissolve("COUNTYFP")
.reset_index()
.assign(fips=lambda d: d["STATEFP"] + d["COUNTYFP"])
.loc[:, ["fips", "geometry", "STATEFP", "COUNTYFP", "NAME"]]
)
# get NY times data by county
df = pd.read_csv(
"https://raw.githubusercontent.com/nytimes/covid-19-data/master/live/us-counties.csv"
)
# limit to florida and make fips same type as geometry
df_fl = (
df.loc[df["state"].eq("Florida")]
.dropna(subset=["fips"])
.assign(fips=lambda d: d["fips"].astype(int).astype(str))
)
# merge geometry and covid data
gdf_fl_covid = gdf_county.merge(df_fl, on="fips")
# interactive folium choropleth
gdf_fl_covid.explore(column="cases")
# static matplotlib choropleth
gdf_fl_covid.plot(column="cases")
Upvotes: 1