python - 如何创建没有索引错误的 folium.choropleth map ?

标签 python python-3.x folium

我正在尝试使用 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

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

最佳答案

鲍勃·哈夫纳 (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/

相关文章:

python - 为什么 "m of A called"在此示例中没有打印两次?

python - 如何在Python中编写可用于异常/错误的if-else语句?

python - Python sqlite3数据库已锁定

python - 在 Folium map 上创建图例

python - 在 Folium 中突出显示一个特定国家

python - keras load_weights() 无法加载权重

python - 如何以编程方式将简单数据写入复杂的 .xlsm 文档而不破坏它?

django - 在 Django 1.7、Python3 中,使用 Floppyforms 1.3,不断出现错误 "TypeError: object() takes no parameters"

python - 使用 Pandas 解析一列中的 CSV 数据

python - Folium map 未在 Django 网页中显示