enter code here
嗨,
我想保存通过地理编码提取的一些坐标(经度和纬度),我遇到的问题是这些坐标没有保存,我似乎无法将它们作为列添加到使用熊猫生成的表格中
我收到此错误:
AttributeError:“ NoneType”对象没有属性“ latitude”
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
。这在文档中指定:
返回类型:
None
,geopy.location.Location
或其中的list
(如果是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'))
我们首先查询所有位置,然后检查成功的位置,并检索该位置的
latitude
和latitude
。关于python - 我从Python的Geocoder中提取的某些坐标未保存在我创建的变量中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57537591/