python - 为什么我在解析数据帧时会收到错误,但当它是单行时却没有?
问题描述
蟒蛇新手。我在 python 中使用 pygeocodio 库
API_KEY = "myapikey"
from geocodio import GeocodioClient
client = GeocodioClient(API_KEY)
addresses = client.geocode("21236 Birchwood Loop, 99567, AK")
addresses.best_match.get("accuracy")
Out[61]: 1
addresses.best_match.get("accuracy_type")
Out[62]: 'rooftop'
但是,如果我想遍历数据框(example.csv):
import pandas as pd
customers = pd.read_csv("example.csv")
for row in customers.iterrows():
addresses = client.geocode(row)
addresses.best_match.get("accuracy")
我收到一个错误:
File "C:\Users\jtharian\AppData\Local\Continuum\anaconda3\lib\site-packages\geocodio\client.py", line 58, in error_response
raise exceptions.GeocodioDataError(response.json()["error"])
GeocodioDataError: Could not geocode address. Postal code or city required.
example.csv 的代表:
21236 Birchwood Loop, 99567, AK
1731 Bragaw St, 99508, AK
300 E Fireweed Ln, 99503, AK
4360 Snider Dr, 99654, AK
1921 W Dimond Blvd 108, 99515, AK
2702 Peger Rd, 99709, AK
1651 College Rd, 99709, AK
898 Ballaine Rd, 99709, AK
23819 Immelman Circle, 99567, AK
9750 W Parks Hwy, 99652, AK
7205 Shorewood Dr, 99645, AK
为什么我会收到此错误?
解决方案
查看api 文档,您需要一个字符串来表示各个地址组件列中的地址,如下所示:
location = client.geocode("1109 N Highland St, Arlington VA")
因此,要在您的中获得这样的列,df
您可以将每个向量映射到一个字符串,然后使用简单的字符串连接来生成一个字符串,然后将其插入到您的新系列中df
:
import pandas as pd
customers = pd.read_csv("example.csv", header=None)
customers['address_string'] = customers[0].map(str) + ' ' + customers[1].map(str) + customers[2].map(str)
生产:
# >>> customers['address_string']
# 0 21236 Birchwood Loop 99567 AK
# 1 1731 Bragaw St 99508 AK
# 2 300 E Fireweed Ln 99503 AK
# 3 4360 Snider Dr 99654 AK
# 4 1921 W Dimond Blvd 108 99515 AK
然后您可以遍历地址字符串系列的值并将准确性存储在可以插入到您的列表中df
:
geocoded_acuracy = []
geocoded_acuracy_type = []
for address in customers['address_string'].values:
geocoded_address = client.geocode(address)
accuracy = geocoded_address.best_match.get("accuracy")
accuracy_type = geocoded_address.best_match.get("accuracy_type")
geocoded_acuracy.append(accuracy)
geocoded_acuracy_type.append(accuracy_type)
customers['accuracy'] = geocoded_acuracy
customers['accuracy_type'] = geocoded_acuracy_type
results = customers[['address_string', 'accuracy', 'accuracy_type']]
结果df
将如下所示:
# >>> results
# address_string accuracy accuracy_type
# 0 21236 Birchwood Loop 99567 AK 1.00 rooftop
# 1 1731 Bragaw St 99508 AK 1.00 rooftop
# 2 300 E Fireweed Ln 99503 AK 1.00 rooftop
# 3 4360 Snider Dr 99654 AK 1.00 range_interpolation
# 4 1921 W Dimond Blvd 108 99515 AK 1.00 rooftop
# 5 2702 Peger Rd 99709 AK 1.00 rooftop
# 6 1651 College Rd 99709 AK 1.00 rooftop
# 7 898 Ballaine Rd 99709 AK 1.00 rooftop
# 8 23819 Immelman Circle 99567 AK 1.00 rooftop
# 9 9750 W Parks Hwy 99652 AK 0.33 place
# 10 7205 Shorewood Dr 99645 AK 1.00 range_interpolation
然后将结果写入df
a .csv
:
results.to_csv('results.csv')
将所有这些放在一起会产生以下代码:
import pandas as pd
from geocodio import GeocodioClient
API_KEY = 'insert_your_key_here'
client = GeocodioClient(API_KEY)
customers = pd.read_csv("example.csv", header=None)
customers['address_string'] = customers[0].map(str) + ' ' + customers[1].map(str) + customers[2].map(str)
geocoded_acuracy = []
geocoded_acuracy_type = []
for address in customers['address_string'].values:
geocoded_address = client.geocode(address)
accuracy = geocoded_address.best_match.get("accuracy")
accuracy_type = geocoded_address.best_match.get("accuracy_type")
geocoded_acuracy.append(accuracy)
geocoded_acuracy_type.append(accuracy_type)
customers['accuracy'] = geocoded_acuracy
customers['accuracy_type'] = geocoded_acuracy_type
results = customers[['address_string', 'accuracy', 'accuracy_type']]
results.to_csv('results.csv')
推荐阅读
- c++ - 成为有效伪随机发生器的标准
- wordpress - Wordpress 仪表板提要小部件 - 如何在新窗口中打开提要?
- sql - SQL 内联视图的语法帮助
- c - 32 位 x86 平台 c/c++ 应用程序中的 Tensorflow 2.x 模型推理
- reactjs - 如何从另一个组件中的减速器初始状态中过滤一组对象?
- npm - 如何从任何地方使用安装在 monorepo 的 npm 根目录中的可执行文件?
- html - 在网页中找到文本并单击 - Eclipse Selenium
- selenium - ChromeDriver - 无法登录谷歌帐户
- c++ - std::divides 上的 C++ 等号运算符
- php - 停止 PHP_EOL 将数据附加到上一行