‘lat’的类型为不能转换为float。

huangapple go评论67阅读模式
英文:

Beginner's Error in python, How can I fix this 'lat' of type <class 'str'> is not convertible to float

问题

我最近一直在使用Python,正在使用Foursquare和从Wikipedia获取的数据做一项工作。我正在尝试使用以下代码制作地图:

venues_map = folium.Map(location=[latitude, longitude], zoom_start=13) # 生成以Ciutat Vella为中心的地图

# 添加一个红色圆形标记来表示社区的中心
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)

# 将商店添加为蓝色圆形标记
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)

# 显示地图
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列,它确实是一个浮点数,但我不知道如何修复它,无法继续前进。我将不胜感激任何帮助。

<details>
<summary>英文:</summary>

I have recently been using python and I am doing a job using Foursquare and data obtained from wikipedia. I am trying to make a map with the following code:

        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(
        [&#39;lat&#39;,&#39;lng&#39;],
        radius=10,
        color=&#39;red&#39;,
        popup=&#39;Eixample&#39;,
        fill = True,
        fill_color = &#39;red&#39;,
        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=&#39;blue&#39;,
            popup=label,
            fill = True,
            fill_color=&#39;blue&#39;,
            fill_opacity=0.6
        ).add_to(venues_map)
    
    # display map
    venues_map

When executing the line I get the following error:

        ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    /opt/conda/envs/Python36/lib/python3.6/site-packages/folium/utilities.py in validate_location(location)
         58         try:
    ---&gt; 59             float(coord)
         60         except (TypeError, ValueError):
    
    ValueError: could not convert string to float: &#39;lat&#39;
    
    During handling of the above exception, another exception occurred:
    
    ValueError                                Traceback (most recent call last)
    &lt;ipython-input-27-7a61c4e1c46b&gt; in &lt;module&gt;
         11     fill = True,
         12     fill_color = &#39;red&#39;,
    ---&gt; 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,
    --&gt; 305                                            tooltip=tooltip)
        306         self._name = &#39;CircleMarker&#39;
        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 = &#39;Marker&#39;
    --&gt; 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(&#39;Location should consist of two numerical values, &#39;
         62                              &#39;but {!r} of type {} is not convertible to float.&#39;
    ---&gt; 63                              .format(coord, type(coord)))
         64         if math.isnan(float(coord)):
         65             raise ValueError(&#39;Location values cannot contain NaNs.&#39;)
    
    ValueError: Location should consist of two numerical values, but &#39;lat&#39; of type &lt;class &#39;str&#39;&gt; is not convertible to float.

I have looked and the LAT column if it is a float, I don&#39;t know how to fix it and I can&#39;t move forward. I would appreciate any help.


The value of Lat comes from the following table, in which it shows the name of the neighborhoods of Barcelona and shows its latitude and longitude to be able to take from there the values to form the map:

[enter image description here][1]


  [1]: https://i.stack.imgur.com/AeMsu.png

</details>


# 答案1
**得分**: 0

在你的代码中:

```python
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

location (tuple[float, float]) – 纬度和经度对(北纬,东经)

位置元组应该包含两个浮点数。尝试移除 'lat' 和 'lng' 的引号,就应该可以了。

英文:

In your code:

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

You are trying to pass in a tuple of strings.

Reading the documentation : https://python-visualization.github.io/folium/modules.html

> location (tuple[float, float]) – Latitude and Longitude pair (Northing, Easting)

The location tuple takes two floats. Try to remove the "" from lat and lng and you should be good.

huangapple
  • 本文由 发表于 2020年1月6日 22:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定