我在Python中从geocoder提取的一些坐标没有保存在我创建的变量中

2024-04-23 08:40:21 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我得到这个错误: 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]

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


Tags: 代码importjsonpandasherecodenom地理
1条回答
网友
1楼 · 发布于 2024-04-23 08:40:21

如果地址找不到,^{} [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'))

我们首先查询所有位置,然后检查成功的位置,并检索该位置的latitudelatitude。你知道吗

相关问题 更多 >