[GIS] How to identify correct key_on parameter from JSON file for Folium Choropleth

choroplethfoliumjson

I'm trying to make a Folium Choropleth map. But I'm stuck for long time on completing it. My map shows boundaries correctly, even shows legend correctly but the map is not filled with choropleth colors.

I'm running my code on Jupyter Notebook. I have combined the geopandas dataframe and pandas dataframe as df_mergeplz

My simple code is

plzchoromap = folium.Map(location = [50.935173,6.953101],tiles='cartodbpositron',zoom_start = 11)

plzchoromap.choropleth(geo_data=df_mergeplz,
                       data = df_mergeplz,
                       columns=["Postal Code","Population"],
                       key_on="feature.NUMMER",
                       fill_color='YlGnBu',
                       fill_opacity = 0.3,
                       line_weight=2,)
plzchoromap

my JSON file looks like

{
 "displayFieldName": "NUMMER",
 "fieldAliases": {"OBJECTID": "OBJECTID","NUMMER": "Postzustellbezirk (Nr.)"},
 "geometryType": "esriGeometryPolygon",
 "spatialReference": {"wkid": 4326,"latestWkid": 4326},
 "fields": [
      {"name": "OBJECTID","type": "esriFieldTypeOID","alias": "OBJECTID"},
      {"name": "NUMMER","type": "esriFieldTypeString","alias": "Postzustellbezirk (Nr.)","length": 5}
            ],
 "features": [
      {"attributes": {"OBJECTID": 16,"NUMMER": "51065"},
      "geometry": {"rings": [[[7.0088577711265421,50.964379468107985],,,....,]]}
      },
      {"attributes": {"OBJECTID": 18,"NUMMER": "50823"},
      "geometry": {"rings": [[[6.9339208198918785,50.96181483933605],,,....,]]}
      },
 .
 .
 .
 .
 . 
   
   }

I want to use the parameter NUMMER inside features.attributes.NUMMER.
But passing features.attributes.NUMMER throws error that

~/opt/anaconda3/envs/ML1env/lib/python3.8/site-packages/folium/folium.py in get_by_key(obj, key)
    285             def get_by_key(obj, key):
    286                 return (obj.get(key, None) if len(key.split('.')) <= 1 else
--> 287                         get_by_key(obj.get(key.split('.')[0], None),
    288                                    '.'.join(key.split('.')[1:])))
    289 

~/opt/anaconda3/envs/ML1env/lib/python3.8/site-packages/folium/folium.py in get_by_key(obj, key)
    284 
    285             def get_by_key(obj, key):
--> 286                 return (obj.get(key, None) if len(key.split('.')) <= 1 else
    287                         get_by_key(obj.get(key.split('.')[0], None),
    288                                    '.'.join(key.split('.')[1:])))

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

I have referred several other posts which primarily showed correction on key_on parameter. I doubt I am missing something obvious here.
Summary:

key_on = feature.NUMMER #outputs unfilled folium map
key_on = feature.attributes.NUMMER #throws error.

Can someone please let me know what is the right key_on parameter for me or a guide on how to play with JSON object to find the correct key_on parameter.

Just to add on my df_mergeplz looks like below:

OBJECTID    NUMMER  geometry    minx    miny    maxx    maxy    Unnamed: 0  Postal Code City    Administrative Region   Population  Area    Area Codes  Neighborhoods   Latitude    Longitude
0   1   50769   POLYGON ((6.84866 51.08444, 6.84873 51.08420, ...   6.772530    51.007784   6.973939    51.084964   15  50769   Cologne North Rhine-Westphalia  63970   44.800  2203, 221   Chorweiler  51.046949   6.877612

Best Answer

There is a type mismatch between the data you have a key on. I encountered the same issue and got it fixed now. Before passing your NUMMER, convert it to the string and pass the new column to choropleth:

df_mergeplz["NUMMERtxt"] = df_mergeplz["NUMMER"].astype(str)
Related Question