Search code examples
pythonfoliumchoropleth

Folium Choropleth Map - ValueError: dictionary update sequence element #0 has length 1; 2 is required


I am following this tutorial to learn how to make basic maps using Folium, but I am receiving an error from what appears to be the fill_opacity argument when creating the Choropleth map. The choropleth method used in the tutorial is deprecated and has been replaced with the Choropleth class, and I have updated the code accordingly. I have consulted the Folium documentation, and I don't see anything different about my code versus the example code in the documentation. Below is my code, and the error follows after.

import pandas as pd
import folium
import json
from folium import plugins
from urllib.request import urlopen

df = pd.read_csv('https://raw.githubusercontent.com/ritvikmath/StarbucksStoreScraping/master/starbucksInLACounty.csv')

LAjson = 'https://raw.githubusercontent.com/ritvikmath/StarbucksStoreScraping/master/laMap.geojson'
response = urlopen(LAjson)
laArea = json.loads(response.read())

LAzipJson = 'https://raw.githubusercontent.com/ritvikmath/StarbucksStoreScraping/master/laZips.geojson'
response2 = urlopen(LAzipJson)
laZip = json.loads(response2.read())

numStoresSeries = df.groupby('zip').count().id

numStoresByZip = pd.DataFrame()

numStoresByZip['zipcode'] = [str(i) for i in numStoresSeries.index]
numStoresByZip['numStores'] = numStoresSeries.values

laMapChoro = folium.Map(location = [34.0522, -118.2437], tiles = 'stamenterrain', zoom_start = 9)

folium.Choropleth(geo_data = 'laZip', data = 'numStoresByZip', columns = ['zipcode', 'numStores'], 
                  key_on = 'feature.properties.zipcode', fill_color = 'YlGn', 
                  fill_opacity = 1).add_to(laMapChoro)
laMapChoro


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/ll/77znv_6d7jng9_kjkpdllhbh0000gn/T/ipykernel_960/1935902863.py in <module>
      9 folium.Choropleth(geo_data = 'laZip', data = 'numStoresByZip', columns = ['zipcode', 'numStores'], 
     10                   key_on = 'feature.properties.zipcode', fill_color = 'YlGn',
---> 11                   fill_opacity = 1).add_to(laMapChoro)
     12 laMapChoro

~/opt/anaconda3/envs/PopulationDensity/lib/python3.7/site-packages/folium/features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
   1203             color_data = data.to_dict()
   1204         elif data:
-> 1205             color_data = dict(data)
   1206         else:
   1207             color_data = None

ValueError: dictionary update sequence element #0 has length 1; 2 is required

Solution

  • The problem is that you are not passing in your data, but strings. This worked for me:

    folium.Choropleth(
        geo_data=laZip,
        data=numStoresByZip,
        columns=["zipcode", "numStores"],
        key_on="feature.properties.zipcode",
        fill_color="YlGn",
        fill_opacity=1.0,
    ).add_to(laMapChoro)