Search code examples
pythongeolocationgeospatialgeojsonfolium

Choropleth in folium: can not find key_on


I have boundaries in geo-json format

{'type': 'FeatureCollection',
 'features': [{'type': 'Feature',
   'properties': {'STATEFP10': '04',
    'ZCTA5CE10': '85347'},
   'geometry': {'type': 'Polygon',
    'coordinates': [[[-113.587371, 32.77836],
      [-113.587269, 32.77819],
      [-113.590707, 32.776887],
     ...

I have also some data

data

POSTCODE    X
0   85347   0
1   85356   0

I am trying to plot a basic Choropleth. If I do the following, I get

AttributeError: 'NoneType' object has no attribute 'get'

What is wrong in the following snippet? It is an example copied and pasted... I have already ensured boundaries keys and dataframe postcodes to be exactly same set...

m = folium.Map( zoom_start=8)
# Add the color for the chloropleth:
folium.Choropleth(
 geo_data=boundaries,
 name='choropleth',
 data=data,
 columns=['POSTCODE','X'],
 key_on='features.properties.ZCTA5CE10',
 fill_color='BuPu',
#fill_opacity=0.7
).add_to(m)
folium.LayerControl().add_to(m)
m

Solution

  • try feature instead of features for the key_on param ie feature.properties.ZCTA5CE10

    From the Choropleth docstring

    key_on: string, default None Variable in the geo_data GeoJSON file to bind the data to. Must start with 'feature' and be in JavaScript objection notation. Ex: 'feature.id' or 'feature.properties.statename'.