回答編集履歴

1

追記

2018/02/23 12:59

投稿

退会済みユーザー
test CHANGED
@@ -15,3 +15,79 @@
15
15
  return d["long_name"]
16
16
 
17
17
  ```
18
+
19
+
20
+
21
+ ----
22
+
23
+ 追記
24
+
25
+
26
+
27
+ ちょっと試したところ、applyではクエリを連送してエラーになってしまうようですので、forで一つずつ処理したほうが無難なようです。エラー処理ののためにtryキャッチを入れていますが、回数の上限を設けて置いたおうが安全かもしれません。
28
+
29
+
30
+
31
+ ```
32
+
33
+ import time
34
+
35
+
36
+
37
+ import pandas as pd
38
+
39
+ from pygeocoder import Geocoder, GeocoderError
40
+
41
+
42
+
43
+ def find_country(name):
44
+
45
+ geocode = Geocoder.geocode(name)
46
+
47
+ address_components = geocode.current_data["address_components"]
48
+
49
+ for d in address_components:
50
+
51
+ if "country" in d["types"]:
52
+
53
+ return d["long_name"]
54
+
55
+
56
+
57
+ City = pd.DataFrame({"City":["Saitama","Kabul","Sochi","Giza","Kampala"]})
58
+
59
+
60
+
61
+ countries = []
62
+
63
+ for city in City["City"]:
64
+
65
+ while True:
66
+
67
+ try:
68
+
69
+ country = find_country(city)
70
+
71
+ print("Success")
72
+
73
+ print(country)
74
+
75
+ countries.append(country)
76
+
77
+ time.sleep(1)
78
+
79
+ break
80
+
81
+ except GeocoderError:
82
+
83
+ print("Failure")
84
+
85
+ time.sleep(1)
86
+
87
+ continue
88
+
89
+
90
+
91
+ Country = pd.DataFrame({"Country":countries})
92
+
93
+ ```