0

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?

1 Answer 1

0

So the problem may be as simple as I was using the wrong value for "key_on". This website says

The tricky part is the key_on parameter.

It should start with ‘feature’ and be in JavaScript objection notation, so in our case, it is feature.properties.ID_NIL

In my case, when I changed the key_on value to

key_on = 'feature.properties.FIPS'

I stopped getting the error. Frankly, I don't understand this, since my geo_data is in a GeoPandas table rather than a GeoJSON file (with dictionary-like structures, such as feature.properties).

Perhaps this is encoded behind the scenes in the GeoPandas table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.