我正在尝试使用 Folium 创建叶绿素图。但是,我收到错误:
IndexError: list index out of range when executing
我不知道如何使用像 Folium 这样的包装器来解决这个错误。如果我注释掉 folium.chromopleth 中的数据属性,我不会看到错误,但是当然,不会引入数据,并且我的所有 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
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
最佳答案
鲍勃·哈夫纳 (Bob Haffner) 给出了正确答案。我需要将两个参数传递给列。更正后的代码如下:
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)
关于python - 如何创建没有索引错误的 folium.choropleth map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57172647/