GeoCode超时。错误是由数据帧表引起的吗?

问题描述 投票:0回答:1

这是我的数据d = pd.read_html('https://en.wikipedia.org/wiki/List_of_Lagos_State_local_government_areas_by_population')

dfr = d [2]

dfr ['Locale'] = dfr.LGA +',尼日利亚拉各斯'

我打算使用此代码

from geopy.exc import GeocoderTimedOut 

for address in (dfr['Locale']):
    geolocator = Nominatim(user_agent="lagos_explorer")
    location = geolocator.geocode(address, timeout=20000)    
    if location:
        latitude = location.latitude
        longitude = location.longitude
    else:
        latitude= "Not Available"
        longitude= "Not Available"

    dfr['Latitude']=pd.Series([latitude])
    dfr['Longitude']=pd.Series([longitude])

dfr

要遍历表格,然后分配各自的坐标。问题是,插入第一行后超时,然后为其他行填充NaN。

我是编码新手,请帮忙。

python pandas loops geocode
1个回答
0
投票

为索引创建了一个计数器。

from geopy.exc import GeocoderTimedOut
for i in (dfr.index):
    geolocator = Nominatim(user_agent="lag_explore")
    location = geolocator.geocode(dfr['Locale'][i], timeout=20000)

    if location:
        latitude = location.latitude
        longitude = location.longitude
    else:
        latitude= None
        longitude= None

    dfr['Latitude'][i]=latitude
    dfr['Longitude'][i]=longitude


dfr
© www.soinside.com 2019 - 2024. All rights reserved.