gpt4 book ai didi

python - 为什么我在解析数据帧时会收到错误消息,而当它是单行时却不会收到错误消息?

转载 作者:太空宇宙 更新时间:2023-11-03 23:54:27 25 4
gpt4 key购买 nike

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 docs您需要一个字符串来表示您的各个地址组件列中的地址,如下所示:

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

然后您可以遍历 Series of address strings 的值并将精度存储在一个列表中,该列表可以插入到您的 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写入.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')

关于python - 为什么我在解析数据帧时会收到错误消息,而当它是单行时却不会收到错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58356111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com