回答編集履歴
1
追記
answer
CHANGED
@@ -6,4 +6,42 @@
|
|
6
6
|
for d in address_components:
|
7
7
|
if "country" in d["types"]:
|
8
8
|
return d["long_name"]
|
9
|
+
```
|
10
|
+
|
11
|
+
----
|
12
|
+
追記
|
13
|
+
|
14
|
+
ちょっと試したところ、applyではクエリを連送してエラーになってしまうようですので、forで一つずつ処理したほうが無難なようです。エラー処理ののためにtryキャッチを入れていますが、回数の上限を設けて置いたおうが安全かもしれません。
|
15
|
+
|
16
|
+
```
|
17
|
+
import time
|
18
|
+
|
19
|
+
import pandas as pd
|
20
|
+
from pygeocoder import Geocoder, GeocoderError
|
21
|
+
|
22
|
+
def find_country(name):
|
23
|
+
geocode = Geocoder.geocode(name)
|
24
|
+
address_components = geocode.current_data["address_components"]
|
25
|
+
for d in address_components:
|
26
|
+
if "country" in d["types"]:
|
27
|
+
return d["long_name"]
|
28
|
+
|
29
|
+
City = pd.DataFrame({"City":["Saitama","Kabul","Sochi","Giza","Kampala"]})
|
30
|
+
|
31
|
+
countries = []
|
32
|
+
for city in City["City"]:
|
33
|
+
while True:
|
34
|
+
try:
|
35
|
+
country = find_country(city)
|
36
|
+
print("Success")
|
37
|
+
print(country)
|
38
|
+
countries.append(country)
|
39
|
+
time.sleep(1)
|
40
|
+
break
|
41
|
+
except GeocoderError:
|
42
|
+
print("Failure")
|
43
|
+
time.sleep(1)
|
44
|
+
continue
|
45
|
+
|
46
|
+
Country = pd.DataFrame({"Country":countries})
|
9
47
|
```
|