【问题标题】:Google Geocode ValueError谷歌地理编码值错误
【发布时间】:2021-05-01 18:16:05
【问题描述】:

我目前正在尝试从一系列当前存储在 pandas 数据框中的地址中提取纬度和经度值。我目前有以下代码:

import pandas as pd
from googlemaps import Client as GoogleMaps

GoogleAPIkey = "myAPIkey"
gmaps = GoogleMaps(GoogleAPIkey)

df = pd.read_csv("addresslist")

df输出如下:

 |Location                            |
0|Robert Pitt Drive, Spring Valley, NY|
1|Bryant Avenue, Harrison, NY         |

理想情况下,我希望得到以下输出:

 |Location                            | Lat | Lon |
0|Robert Pitt Drive, Spring Valley, NY|     |     | 
1|Bryant Avenue, Harrison, NY         |     |     | 

我正在使用以下代码在 df 中创建一个新列:

df["geocode_result"] = gmaps.geocode(df["Location"])

但是,我得到一个 ValueError:一个系列的真值是不明确的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

我是否需要将我的字符串系列转换为另一种格式才能获得正确的值?

谢谢

【问题讨论】:

    标签: python pandas valueerror geocode


    【解决方案1】:

    尝试将您的代码更改为:

    df["geocode_result"] = df['Location'].map(lambda x: gmaps.geocode(x))
    

    方法.map() 或.apply() 允许您将gmaps.geocode() 函数应用于存储在df['Location'] 列中的每个值。

    【讨论】:

    • 不幸的是我在使用这行代码时仍然遇到同样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多