gpt4 book ai didi

python - 我从 Python 中的地理编码器中提取的一些坐标没有保存在我创建的变量中

转载 作者:行者123 更新时间:2023-12-01 07:24:10 25 4
gpt4 key购买 nike

在此处输入代码嗨,我想保存一些通过地理编码提取的坐标(纬度和经度),我遇到的问题是这些坐标没有保存,我似乎无法将它们作为列添加到使用 pandas 生成的表中

我收到此错误:AttributeError:“NoneType”对象没有属性“纬度”

    import pandas
from geopy.geocoders import Nominatim
df1= pandas.read_json("supermarkets.json")
nom= Nominatim(scheme= 'http')
lis= list(df1.index)
for i in lis:

l= nom.geocode(list(df1.loc[i,"Address":"Country"]))
j=[]+ [l.latitude]
k=[]+ [l.longitude]

我希望有一种方法来保存坐标并将它们包含在我的表格中。谢谢

最佳答案

nom.geocode(..) [geopy-doc]如果找不到地址,或者未在足够的时间内回答查询,则可能会导致 None。这是在文档中指定的:

Return type:

None, geopy.location.Location or a list of them, if exactly_one=False.

from operator import attrgetter

locations = df['Address':'Country'].apply(
lambda r: nom.geocode(list(r)), axis=1
)
nonnull = locations.notnull()
df.loc[nonnull, 'longitude'] = locations[nonnull].apply(attrgetter('longitude'))
df.loc[nonnull, 'latitude'] = locations[nonnull].apply(attrgetter('latitude'))

我们首先查询所有位置,然后检查已成功的位置,并检索该位置的纬度纬度

关于python - 我从 Python 中的地理编码器中提取的一些坐标没有保存在我创建的变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57537591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com