0

蟒蛇新手。我在 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

为什么我会收到此错误?

4

2 回答 2

1

查看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

然后将结果写入dfa .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')
于 2019-10-12T17:03:00.760 回答
0

我会使用apply特定的异常等,但现在我想新的只是关注什么有效和错误。但是,当您熟悉 pandas 和 python 时,一定会研究这些主题。

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html https://geek-university.com/python/catch-specific-exceptions/

errors, address_list, accuracy_list, accuracy_type_list = [], [], [], []
for index, row in customers.iterrows():
    try:
        addresses = client.geocode(row.values[0])
        accuracy = addresses.best_match.get("accuracy")
        accuracy_type = addresses.best_match.get("accuracy_type")

        address_list.append(addresses)
        accuracy_list.append(accuracy)
        accuracy_type_list.append(accuracy_type)
    except Exception as e:
        address_list.append(None)
        accuracy_list.append(None)
        accuracy_type_list.append(None)
        errors.append(f"failure {e.args[0]} at index {index}")

我在做什么?iterrows提供索引和行的元组。所以我对每个行项目进行地理编码。如果它有效,我将结果添加到 address_list。与准确性相同。但是当它失败时,我会在错误列表中添加一条消息,以指示错误在数据帧中发生的位置;即索引。但我还需要地址列表中的占位符,所以我只添加无。所以现在我可以做

customers['addresses'] = address_list
customers['accuracy'] = accuracy_list
customers['accuracy_type'] = accuracy_type_list

如果需要,保存我的数据框。https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html

于 2019-10-12T16:39:17.540 回答