使用pandas在folium中绘制标记时float64类型转换报错问题
问题描述
首先提供使用的原始JSON文件内容如下:
{ "entries": [ { "awy_id": "", "distance": 0.0, "fl_at_wpt": 1, "lat": -34.821666, "lng": -58.536666, "max_fl": 410, "min_fl": 0, "ndic": "SA", "ndid": "SAEZ", "ndid_ext": "SAEZ", "ndtyp": "PA", "ndtyp_ext": "PA", "special_route": "", "through_fra_edge": false, "used_fl": 400 }, { "awy_id": "GBE6", "distance": 84661.59842290805, "fl_at_wpt": 322, "lat": -35.751666, "lng": -58.465, "max_fl": 1000, "min_fl": 0, "ndic": "SA", "ndid": "GBE", "ndid_ext": "GBE", "ndtyp": "D", "ndtyp_ext": "D", "special_route": "GBE6", "through_fra_edge": false, "used_fl": 400 }, { "awy_id": "DCT", "distance": 146604.93487805585, "fl_at_wpt": 400, "lat": -37.0, "lng": -59.0, "max_fl": 660, "min_fl": 95, "ndic": "", "ndid": "3700S05900W", "ndid_ext": "3700S05900W", "ndtyp": "$C", "ndtyp_ext": "$C", "special_route": "", "through_fra_edge": true, "used_fl": 400 }, { "awy_id": "DCT", "distance": 156860.1442784719, "fl_at_wpt": 400, "lat": -38.313333, "lng": -59.656666, "max_fl": 660, "min_fl": 95, "ndic": "SA", "ndid": "SIGUL", "ndid_ext": "SIGUL", "ndtyp": "EA", "ndtyp_ext": "EA", "special_route": "", "through_fra_edge": true, "used_fl": 400 }, { "awy_id": "DCT", "distance": 253654.31159714353, "fl_at_wpt": 400, "lat": -40.49, "lng": -60.551666, "max_fl": 660, "min_fl": 95, "ndic": "SA", "ndid": "PUGLI", "ndid_ext": "PUGLI", "ndtyp": "EA", "ndtyp_ext": "EA", "special_route": "", "through_fra_edge": true, "used_fl": 400 }, { "awy_id": "UT662", "distance": 455742.1357395189, "fl_at_wpt": 400, "lat": -44.38, "lng": -62.313333, "max_fl": 450, "min_fl": 250, "ndic": "SA", "ndid": "OGRAX", "ndid_ext": "OGRAX", "ndtyp": "EA", "ndtyp_ext": "EA", "special_route": "", "through_fra_edge": false, "used_fl": 400 }, { "awy_id": "DCT", "distance": 172690.29386362626, "fl_at_wpt": 400, "lat": -45.846666, "lng": -63.038333, "max_fl": 660, "min_fl": 95, "ndic": "SA", "ndid": "ESNAS", "ndid_ext": "ESNAS", "ndtyp": "EA", "ndtyp_ext": "EA", "special_route": "", "through_fra_edge": true, "used_fl": 400 }, { "awy_id": "DCT", "distance": 268007.5486955521, "fl_at_wpt": 400, "lat": -48.113333, "lng": -64.238333, "max_fl": 660, "min_fl": 95, "ndic": "SA", "ndid": "IRAVA", "ndid_ext": "IRAVA", "ndtyp": "EA", "ndtyp_ext": "EA", "special_route": "", "through_fra_edge": true, "used_fl": 400 }, { "awy_id": "DCT", "distance": 201521.2136195075, "fl_at_wpt": 400, "lat": -49.81, "lng": -65.205, "max_fl": 660, "min_fl": 95, "ndic": "SA", "ndid": "LOBOS", "ndid_ext": "LOBOS", "ndtyp": "EA", "ndtyp_ext": "EA", "special_route": "", "through_fra_edge": true, "used_fl": 400 }, { "awy_id": "UT662", "distance": 355978.6689666387, "fl_at_wpt": 400, "lat": -52.788333, "lng": -67.071666, "max_fl": 450, "min_fl": 250, "ndic": "SA", "ndid": "ERUPO", "ndid_ext": "ERUPO", "ndtyp": "EA", "ndtyp_ext": "EA", "special_route": "", "through_fra_edge": false, "used_fl": 400 }, { "awy_id": "DCT", "distance": 120043.42891043477, "fl_at_wpt": 210, "lat": -53.785, "lng": -67.76, "max_fl": 660, "min_fl": 95, "ndic": "SA", "ndid": "GRA", "ndid_ext": "GRA", "ndtyp": "DB", "ndtyp_ext": "DB", "special_route": "", "through_fra_edge": true, "used_fl": 400 }, { "awy_id": "DCT", "distance": 122840.72073640439, "fl_at_wpt": 2, "lat": -54.843333, "lng": -68.295, "max_fl": 660, "min_fl": 95, "ndic": "SA", "ndid": "SAWH", "ndid_ext": "SAWH", "ndtyp": "PA", "ndtyp_ext": "PA", "special_route": "DCT", "through_fra_edge": false, "used_fl": 400 } ] }
尝试使用pandas DataFrame中lat、lng列的经纬度数据,通过folium绘制地图标记,两列的数据类型均为float64。
实现代码如下:
import pandas as pd import numpy as np import folium import json with open('response.json') as json_data: data = json.load(json_data) df = pd.DataFrame(data['entries']) af = pd.DataFrame(data['profile']) difference = af.diff(axis=0) difference.drop([0],inplace=False) difference #df[df.select_dtypes(np.float64).columns] = df.select_dtypes(np.float64).astype(np.float32) flight = folium.Map( location=[38.40, -30.40], zoom_start=2 ) for _, city in df.iterrows(): folium.Marker( location=[df['lng'], df['lat']] ).add_to(flight) flight
运行代码后出现如下报错:
Name: lng, dtype: float64 of type <class 'pandas.core.series.Series'> is not convertible to float.
已检索相关解决方案,尝试将lat、lng列强制转换为float类型,执行如下转换代码后问题仍未解决:
df[df.select_dtypes(np.float64).columns] = df.select_dtypes(np.float64).astype(np.float)
报错原因
这个报错和列的数据类型没有关系,之前做的float类型转换完全没有命中问题根源,核心问题有两个:
- 遍历DataFrame行的时候,没有取当前行的坐标值,反而把整个
lng、lat列(pandas Series对象)传给了location参数。folium要求location接收两个单独的浮点数值组成的列表,传入整列对象自然无法转换为单个float值,触发报错。 - 坐标顺序写反了:folium的
location参数固定要求顺序为*[纬度, 经度]*,原代码把经度lng放在了第一位,就算不报错标记位置也会完全错误。
另外代码里difference.drop([0],inplace=False)这行没有实际作用,inplace=False会返回删除后的新对象,没有赋值给变量的话原对象不会发生任何修改,不过这部分和当前报错无关。
修复方法
修改循环内的坐标取值逻辑,取当前遍历行的单个经纬度值,同时调整经纬度顺序即可,修复后的循环代码如下:
for _, city in df.iterrows(): folium.Marker( # 取当前行的lat、lng值,纬度在前经度在后 location=[city['lat'], city['lng']] ).add_to(flight)
不需要额外做float类型转换,原始数据里lat和lng本身就是浮点类型,直接取值即可正常运行。
内容的提问来源于stack exchange,提问作者setan0
相关产品推荐
相关产品推荐

