I'm working on a Folium choropleth map, using a shapefile from the Census bureau and religion data from the US Religion Census. I can get both sets of data loaded and cleaned up, so I have two DFs:
geojson
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 3234 entries, 0 to 3233
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 FIPS 3234 non-null object
1 geometry 3234 non-null geometry
map_data
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3142 entries, 0 to 3141
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 FIPS 3142 non-null object
1 PctAllPop 3142 non-null float64
When I try to create the map with the following code, I get a value error:
m1 = folium.Map(location=[40, -96], zoom_start=4,tiles='openstreetmap')
folium.Choropleth(
geo_data = geojson,
key_on = 'FIPS',
name = 'choropleth',
data = map_data,
columns = ['FIPS','PctAllPop'],
fill_color='OrRd',
fill_opacity=0.8,
line_opacity=0.2,
nan_fill_color = 'grey',
legend_name='Pct of population attending church'
).add_to(m1)
m1
I thought the problem might be that there are some FIPS values in the map_data table that are not in the geojson table, but I checked this with this code:
map_data[~map_data['FIPS'].isin(geojson['FIPS'])]
and it showed there were no unmatched FIPS values.
I thought the FIPS data were already string objects, but to double-check, I ran .astype() on each table:
map_data['FIPS'] = map_data['FIPS'].astype(str)
Even after doing this, folium is still reporting an error:
ValueError: key_on
'FIPS'
not found in GeoJSON.
What am I am doing incorrectly?