「カリフォルニア州ロサンゼルス」のような位置データを含むデータフレームがあります。
目標は、列のすべてのエントリを反復処理し、longとlatを新しい列に保存することです。どんな入力やヒントも大歓迎です!
私はそれを単一の値で試しましたが、うまくいきました。
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="xxx")
location=geolocator.geocode(df['Location'][1])
print(location.longitude)
print(location.latitude)
-117.8704931
33.7500378
さて、初心者として、forループをやってみましょうと思いました:
df['lat']=0
print(df['Location'][1])
for x in range(1,len(df)+1):
location = geolocator.geocode(df['Location'][x])
df['loc'][x]=location.latitude
次の警告が表示されます。
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
df['loc'][x]=location.latitude
約2分後、次のエラーが発生します。
socket.timeout: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\skpok\Anaconda3\lib\site-packages\geopy\geocoders\base.py", line 344, in _call_geocoder
page = requester(req, timeout=timeout, **kwargs)
File "C:\Users\skpok\Anaconda3\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "C:\Users\skpok\Anaconda3\lib\urllib\request.py", line 543, in _open
'_open', req)
File "C:\Users\skpok\Anaconda3\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\skpok\Anaconda3\lib\urllib\request.py", line 1360, in https_open
context=self._context, check_hostname=self._check_hostname)
File "C:\Users\skpok\Anaconda3\lib\urllib\request.py", line 1319, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error timed out>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\skpok\Downloads\test123.py", line 12, in <module>
location = geolocator.geocode(df['Location'][x])
File "C:\Users\skpok\Anaconda3\lib\site-packages\geopy\geocoders\osm.py", line 309, in geocode
self._call_geocoder(url, timeout=timeout), exactly_one
File "C:\Users\skpok\Anaconda3\lib\site-packages\geopy\geocoders\base.py", line 367, in _call_geocoder
raise GeocoderTimedOut('Service timed out')
geopy.exc.GeocoderTimedOut: Service timed out
Nominatimには、1秒あたりの最大リクエストルール(1秒あたり1)があります。スクリプトがループ間でスリープする句を挿入するようにしてください。
あなたが試すことができます
import time
df['lat']=0
print(df['Location'][1])
for x in range(1,len(df)+1):
location = geolocator.geocode(df['Location'][x])
time.sleep(2)
df.at[x, 'lat']=location.latitude
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加