循环数据框列以隐蔽县以进行坐标

时间:2018-07-18 08:57:33

标签: python geopy

我想将县名转换为坐标。

县:

fips    state_fips  county_fips state   county
1000    1   0   Alabama Alabama
1005    1   5   Alabama Barbour County
1007    1   7   Alabama Bibb County
1009    1   9   Alabama Blount County
1011    1   11  Alabama Bullock County
6085    6   85  California  Santa Clara County
6089    6   89  California  Shasta County
6091    6   91  California  Sierra County
32021   32  21  Nevada  Mineral County
32023   32  23  Nevada  Nye County
32027   32  27  Nevada  Pershing County
32029   32  29  Nevada  Storey County

我想使用python geopy软件包。

from geopy.geocoders import Nominatim
import pandas as pd
import numpy as np
import time

geolocator = Nominatim(timeout=None)
fobj_out = open('county_coordinate.txt', 'a')
i=0
for row in county.itertuples(index=True, name='Pandas'):
    location = geolocator.geocode(getattr(row, "county"))
    #print(location.address)
    #print((location.latitude, location.longitude))
    cty=getattr(row, "county")
    #lat = location.latitude
    #long = location.longitude
    fobj_out.write(str(i))
    i=i+1
    fobj_out.write(",")
    fobj_out.write(cty)
    fobj_out.write(",")
    fobj_out.write(str(location.latitude))
    fobj_out.write(",")
    fobj_out.write(str(location.longitude))
    fobj_out.write("\n")
    time.sleep(0.5) # delay 5 milli-seconds between each request
fobj_out.close()

我得到了错误:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
geopy.exc.GeocoderServiceError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

首先,我希望time.sleep可以帮助我在一秒钟内解决许多请求的问题。但是,该程序在执行90个请求后失败。如何使该程序运行到我的4000个观测数据集的末尾?

第二,当我关闭输出文件以将兑现的结果放入其中时,我看到按顺序该四元组不起作用。我希望程序应该一个接一个地执行,因此,即使程序停止运行,我也可以从i + 1开始继续执行未完成的部分。但是,迭代元组跳到我的数据集的中间,而索引i完全没有意义。如何使迭代元组遵循数据帧索引的顺序?

谢谢。

0 个答案:

没有答案