You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jupyter/Colab运行folium添加地图标记报RecursionError如何修复

Folium打点触发RecursionError修复方案

问题现象

创建带位置描述点位的地图时,运行代码触发递归深度超限错误,报错信息为:
RecursionError: maximum recursion depth exceeded in comparison

原问题代码

for i in range(10):
    lat = map_data['LATLNG'].iloc[i][0]
    long = map_data['LATLNG'].iloc[i][1]
    location=map_data['address'].iloc[i]

    if map_data['ValueCount'].iloc[i] > 150:
        color = 'red'
    elif map_data['ValueCount'].iloc[i] >= 100:
        color = 'orange'
    elif map_data['ValueCount'].iloc[i] < 100:
        color = 'blue'
    
    popup_text = """
                Location: {}<br>
                Peace Disruptions : {}<br>"""
    popup_text = popup_text.format(
                               location, 
                               map_data['ValueCount'].iloc[i]
                             )

    folium.Marker(
        location = [lat, long],
        popup= popup_text,
        icon = folium.Icon(color= color)
    ).add_to(stl_crime_street)

报错Traceback

RecursionError                            Traceback (most recent call last)
<ipython-input-37-e1777bcd5945> in <module>()
     17                                map_data['ValueCount'].iloc[i]
     18                                )
---> 19     folium.Marker(location = [lat, long], popup= popup_text,icon=folium.Icon(color= color)).add_to(stl_crime_street)


   69     for i in container:
     70         if _is_sized_iterable(i):
---> 71             for j in _flatten(i):
     72                 yield j
     73         else:

RecursionError: maximum recursion depth exceeded in comparison

报错原因

报错卡在Folium内部的_flatten坐标展平函数,核心触发点是传入location参数的经纬度值不是Folium可识别的原生数值标量:
直接从pandas单元格取到的经纬度如果是numpy数值类型、或者LATLNG字段存在多层嵌套的异常结构,会被_flatten函数误判为可迭代对象,持续递归展平直到触发栈深度上限。

修复方法

  • 提取经纬度时强制转换为Python原生浮点类型,从根源上避免类型识别错误:
lat = float(map_data['LATLNG'].iloc[i][0])
long = float(map_data['LATLNG'].iloc[i][1])
  • 提前校验LATLNG字段的所有值,确保每个元素都是长度为2的一维经纬度序列,不存在多层嵌套列表/数组的异常数据。
  • (可选加固)传入popup参数时用Folium自带的Popup对象包裹,避免富文本解析异常:
popup = folium.Popup(popup_text, max_width=300)

修复后完整代码

for i in range(10):
    # 强制转换为原生float类型
    lat = float(map_data['LATLNG'].iloc[i][0])
    long = float(map_data['LATLNG'].iloc[i][1])
    location=map_data['address'].iloc[i]

    if map_data['ValueCount'].iloc[i] > 150:
        color = 'red'
    elif map_data['ValueCount'].iloc[i] >= 100:
        color = 'orange'
    elif map_data['ValueCount'].iloc[i] < 100:
        color = 'blue'
    
    popup_text = """
                Location: {}<br>
                Peace Disruptions : {}<br>"""
    popup_text = popup_text.format(
                               location, 
                               map_data['ValueCount'].iloc[i]
                             )

    folium.Marker(
        location = [lat, long],
        popup= folium.Popup(popup_text, max_width=300),
        icon = folium.Icon(color= color)
    ).add_to(stl_crime_street)

内容的提问来源于stack exchange,提问作者jacksojo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 11:48:27