【问题标题】:Getting Error "list indices must be integers or slices, not float" using folium to draw cluster points on a map使用 folium 在地图上绘制聚类点时出现错误“列表索引必须是整数或切片,而不是浮点数”
【发布时间】:2026-01-10 08:25:03
【问题描述】:

我正在尝试使用在地图上创建聚类点。这是我使用的代码,但我收到错误“列表索引必须是整数或切片,而不是浮点数”

# Create map
map_clusters = folium.Map(location=[kol_lat, kol_lng], zoom_start=11)

# Set color scheme for the clusters
x = np.arange(kclusters)
ys = [i + x + (i*x)**2 for i in range(kclusters)]
colors_array = cm.rainbow(np.linspace(0, 1, len(ys)))
rainbow = [colors.rgb2hex(i) for i in colors_array]

# Add markers to the map
markers_colors = []
for lat, lon, poi, cluster in zip(kolkata_merged['Latitude'], kolkata_merged['Longitude'], kolkata_merged['Neighbourhood'], kolkata_merged['Cluster Labels']):
    label = folium.Popup(str(poi) + ' (Cluster ' + str(cluster + 1) + ')', parse_html=True)
    map_clusters.add_child(
        folium.features.CircleMarker(
        [lat, lon],
        radius=5,
        popup=label,
        color=rainbow[cluster-1],
        fill=True,
        fill_color=rainbow[cluster-1],
        fill_opacity=0.7))

map_clusters

【问题讨论】:

    标签: jupyter-notebook folium


    【解决方案1】:

    确保您的数据框中没有 NaN 值。 然后尝试使用color=rainbow[int(cluster)-1],

    【讨论】:

      最近更新 更多