Python Folium读取CSV绘制地图出现语法错误求助排查
代码错误点汇总
- 提取列错误:
df_locations = df_bedarf[["Latitude", "Longitude"]]仅筛选了经纬度两列,后续代码尝试从df_locations中获取suburb/Sort/t/a字段时会抛出KeyError,需要把用到的字段都加入筛选列表。 - 列名大小写不匹配:CSV表头字段为首字母大写的Suburb,代码中写的是小写
suburb,字段名不对应会报错。 - 方法名拼写错误:Folium绘制圆形标记的方法是大写开头的
Circle,小写circle不存在,调用会抛出属性错误。 - 语法错误1:
radius = str(df_locations["t/a"][point]*100)行末尾缺少逗号,会触发语法解析错误。 - 语法错误2:
fill_color="#17cbef字符串缺少右侧闭合双引号,会触发语法解析错误。 - 参数类型错误:
radius参数要求传入数值类型,代码中将其转为字符串会导致渲染失败,需要删除str()转换。
修正后完整代码
import folium import pandas as pd df_bedarf = pd.read_csv('C:/Users/xxxxx/Desktop/xxxxxx/xxxxxxx.csv', sep = ";") df_bedarf.head() # 筛选所有需要用到的字段 df_locations = df_bedarf[["Latitude", "Longitude", "Suburb", "Sort", "t/a"]] location_list = df_locations[["Latitude", "Longitude"]].values.tolist() location_list_size = len(location_list) map_points = folium.Map(location = [47.57087, 13.43828], zoom_start = 7) for point in range(0, location_list_size): folium.Circle( location = location_list[point], popup = df_locations["Suburb"][point] + ": " + df_locations["Sort"][point], radius = df_locations["t/a"][point] * 100, color = "#17cbef", fill = True, opacity = 0.8, fill_color = "#17cbef", stroke = True, weight = 1.0, ).add_to(map_points) map_points
内容的提问来源于stack exchange,提问作者wischi11
相关产品推荐
相关产品推荐

