如何创建没有索引错误的folium.chloropleth映射?

时间:2019-07-23 21:47:51

标签: python python-3.x folium

我正在尝试使用Folium创建一个chloropleth贴图。但是,出现错误:

  

IndexError:执行时列表索引超出范围

我不确定如何使用Folium这样的包装器解决此错误。如果我在folium.chloropleth中注释掉data属性,我看不到错误,但是当然,不会引入数据,而且我所有的POLYGON形状都是相同的颜色。

m = folium.Map(location=[19.433368, -99.137400],zoom_start=9)
type(mexico_city_map)

mexico_city_map.head()

folium.Choropleth(
    geo_data=mexico_city_map,
    data=mexico_city_map,
    columns=['NumberofTrips'],
    fill_color='BuPu',
    fill_opacity=0.7,
    line_opacity=1,
).add_to(m)

m

Head of geodataframe

geopandas.geodataframe.GeoDataFrame

IndexError             Traceback (most recent call last)
<ipython-input-142-11e84157793c> in <module>
     11     fill_color='BuPu',
     12     fill_opacity=0.7,
---> 13     line_opacity=1,
     14 ).add_to(m)
     15 

/anaconda3/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)
   1061         if hasattr(data, 'set_index'):
   1062             # This is a pd.DataFrame
-> 1063             color_data = data.set_index(columns[0])[columns[1]].to_dict()
   1064         elif hasattr(data, 'to_dict'):
   1065             # This is a pd.Series

IndexError: list index out of range

1 个答案:

答案 0 :(得分:0)

Bob Haffner的回答正确。我需要将两个参数传递给column。更正后的代码如下:

folium.Choropleth(
    geo_data=mexico_city_map,
    data=mexico_city_map,
    columns=['Distrito','NumberofTrips'],
    fill_color='BuPu',
    fill_opacity=0.3,
    line_opacity=1.0,
).add_to(m)