【问题标题】:pandas and folium not able to convert str to floatpandas 和 folium 无法将 str 转换为 float
【发布时间】:2020-06-13 19:03:45
【问题描述】:

enter image description here我正在使用 python folium 开发显示印度机场的地图,我使用 pandas 从 csv 读取数据并将坐标分配给 folium.Maker 位置,我收到此错误

Traceback (most recent call last):
  File "/Users/user/Downloads/mapping/folium/folium/utilities.py", line 59, in validate_location
    float(coord)
ValueError: could not convert string to float: '#geo +lat'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "map1.py", line 15, in <module>
    fg.add_child(folium.Marker(location=[lt, ln], popup="Hi there", icon=folium.Icon(color="green")))
  File "/Users/user/Downloads/mapping/folium/folium/map.py", line 277, in __init__
    self.location = validate_location(location)
  File "/Users/user/Downloads/mapping/folium/folium/utilities.py", line 63, in validate_location
    .format(coord, type(coord)))
ValueError: Location should consist of two numerical values, but '#geo +lat' of type <class 'str'> is not convertible to float.
import folium
import pandas

data = pandas.read_csv("aiports.csv")
lat = list(data["latitude_deg"])
lon = list(data["longitude_deg"])


map = folium.Map(location = [20.5937 ,78.9629], zoom_start=4, tiles = "Stamen Terrain")

fg = folium.FeatureGroup(name="My Map")

for lt, ln in zip(lat, lon):
    fg.add_child(folium.Marker(location=[lt, ln], popup="Hi there", icon=folium.Icon(color="green")))

map.add_child(fg)

map.save("Map1.html")

【问题讨论】:

  • 请提供 map1.py 中的代码和 csv 文件的摘录。
  • 请提供您的代码
  • 我已经上传了代码

标签: python python-3.x pandas maps folium


【解决方案1】:

查看数据框的内容,您会看到第一行是元数据而不是数据。我刚刚使用loc[] 跳过了第一行。为了更好地衡量,我在标记中包含了机场名称。

df = pd.read_csv("airports.csv")
map = folium.Map(location = [20.5937 ,78.9629], zoom_start=4, tiles = "Stamen Terrain")
fg = folium.FeatureGroup(name="My Map")

for r in df.loc[1:,["name","latitude_deg","longitude_deg"]].to_dict(orient="records"):
    fg.add_child(folium.Marker(location=[r["latitude_deg"], r["longitude_deg"]], popup=r["name"], icon=folium.Icon(color="green")))

map.add_child(fg)    

map

输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多