Python中的初学者错误,我该如何解决类型 无法转换为浮点数的'lat'

问题描述 投票:-1回答:1

我最近一直在使用python,并且正在使用Foursquare和从Wikipedia获得的数据来完成工作。我正在尝试使用以下代码制作地图:

    venues_map = folium.Map(location=[latitude, longitude], zoom_start=13) # generate map centred of Ciutat Vella

# add a red circle marker to represent the center of the neighborhoods 
folium.vector_layers.CircleMarker(
    ['lat','lng'],
    radius=10,
    color='red',
    popup='Eixample',
    fill = True,
    fill_color = 'red',
    fill_opacity = 0.6
).add_to(venues_map)

# add the shops as blue circle markers
for lat, lng, label in zip(new_df.lat, new_df.lng, new_df.categories):
    folium.vector_layers.CircleMarker(
        [lat,lng],
        radius=5,
        color='blue',
        popup=label,
        fill = True,
        fill_color='blue',
        fill_opacity=0.6
    ).add_to(venues_map)

# display map
venues_map

执行该行时出现以下错误:

    ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/opt/conda/envs/Python36/lib/python3.6/site-packages/folium/utilities.py in validate_location(location)
     58         try:
---> 59             float(coord)
     60         except (TypeError, ValueError):

ValueError: could not convert string to float: 'lat'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-27-7a61c4e1c46b> in <module>
     11     fill = True,
     12     fill_color = 'red',
---> 13     fill_opacity = 0.6
     14 ).add_to(venues_map)
     15 

/opt/conda/envs/Python36/lib/python3.6/site-packages/folium/vector_layers.py in __init__(self, location, radius, popup, tooltip, **kwargs)
    303     def __init__(self, location, radius=10, popup=None, tooltip=None, **kwargs):
    304         super(CircleMarker, self).__init__(location, popup=popup,
--> 305                                            tooltip=tooltip)
    306         self._name = 'CircleMarker'
    307         self.options = path_options(line=False, radius=radius, **kwargs)

/opt/conda/envs/Python36/lib/python3.6/site-packages/folium/map.py in __init__(self, location, popup, tooltip, icon, draggable, **kwargs)
    275         super(Marker, self).__init__()
    276         self._name = 'Marker'
--> 277         self.location = validate_location(location)
    278         self.options = parse_options(
    279             draggable=draggable or None,

/opt/conda/envs/Python36/lib/python3.6/site-packages/folium/utilities.py in validate_location(location)
     61             raise ValueError('Location should consist of two numerical values, '
     62                              'but {!r} of type {} is not convertible to float.'
---> 63                              .format(coord, type(coord)))
     64         if math.isnan(float(coord)):
     65             raise ValueError('Location values cannot contain NaNs.')

ValueError: Location should consist of two numerical values, but 'lat' of type <class 'str'> is not convertible to float.

我已经查看了LAT列,如果它是浮点的,我不知道如何解决,也无法前进。我将不胜感激。

Lat的值来自下表,其中显示了巴塞罗那附近的名称,并显示了纬度和经度,以便能够从中获取值以形成地图:

enter image description here

python string maps data-science
1个回答
0
投票

在您的代码中:

folium.vector_layers.CircleMarker(
    ['lat','lng'],
    radius=10,
    color='red',
    popup='Eixample',
    fill = True,
    fill_color = 'red',
    fill_opacity = 0.6
).add_to(venues_map)

您正在尝试传入字符串元组。

阅读文档:https://python-visualization.github.io/folium/modules.html

位置(元组[浮点数,浮点数]-纬度和经度对(北,东)]

位置元组需要两个浮点数。尝试从经纬度中删除“”,您应该会很好。

© www.soinside.com 2019 - 2024. All rights reserved.