Search code examples
pythonpandasgeojsonfolium

How to access the geojson file correctly?


I want to use folium and GeoJSON file to map data on the world map. Therefore I have to access the GeoJSON file correctly. But I haven't figured out how.

KeyError: "None of [Index(['STATE_CODE', 'STATE_NAME'], dtype='object', name='Province')] are in the [index]"

Index is from the data_AU_series (pd.Series)

STATE_NAME is in the geoJSON file and has to be accessed and act as key for the Series to get the date.

geodata_australia from a GeoJSON file:

{'type': 'FeatureCollection',
 'features': [{'type': 'Feature',
   'id': 0,
   'properties': {'STATE_CODE': '1', 'STATE_NAME': 'New South Wales'},
   'geometry': {'type': 'MultiPolygon',
    'coordinates': [[[[159.10542, -31.563994],
       [159.09775, -31.564275],
       [159.099634, -31.573372],...

data_AU_series (the data (pd.Series) I want to display on the map):

Province
Victoria                       2020-03-24
...

The line of code I wanted to assign the value from the series to the geodata and color

folium

folium.GeoJson(
    geodata_australia,
    name='data_AU',
    style_function=lambda feature: {
        'fillColor': colormap(data_AU_series[feature['properties']]),
        'color': 'black',
        'weight': 1,
        'dashArray': '5, 5',
        'fillOpacity': 0.9,
    }
).add_to(m)

Solution

  • It works with a second key ['STATE_NAME']

    colormap(data_AU_series[feature['properties']['STATE_NAME']]