有没有一种方法可以调用一次API,而不是每个生成的输出调用两次? [英] Is there a way to call an API once instead of twice per output generated?
问题描述
我正在尝试使用Python更有效地编写代码.目前,下面的应用程序获取了我想要的输出,但是,每个地址进行了2次调用
Im trying to write code more efficiently in Python. Currently the app below fetches my desired output.However, it makes 2 calls per address
代表:
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')
尽管代码已运行,但我收到了所需的输出.该代码对每个地址的api进行了2次调用.由于每天有2500个限制,因此很快就会消耗掉.有没有一种方法可以在一次调用中调用准确性和准确性类型,因为根据文档,这些内容仅显示在原始正向地理编码函数本身的输出字典中.我的目标是每个地址只能拨打1个电话.
Although the code ran and I received my desired output. The code makes 2 calls to the api per address. Since there is a 2500/day limit it gets consumed fast. Is there a way to make the call for accuracy and accuracy type within a single call because acc to the documentation these just show up within the output dictionary of the original forward geocoding function itself. My objective is to make only 1 call per address.
数据集的代表(其中地址字符串是我在以上代码中的唯一输入):
Reprex of dataset(where address string is my only input in the above code):
customers['address_string']
Out[5]:
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
5 2702 Peger Rd, 99709, AK
6 1651 College Rd, 99709, AK
7 898 Ballaine Rd, 99709, AK
8 23819 Immelman Circle, 99567, AK
9 9750 W Parks Hwy, 99652, AK
10 7205 Shorewood Dr, 99645, AK
Name: address_string, dtype: object
推荐答案
API返回以下格式的JSON:
The API returns a JSON in the following format:
from geocodio import GeocodioClient
client = GeocodioClient(YOUR_API_KEY)
location = client.geocode("1109 N Highland St, Arlington VA")
返回:
{
"input": {
"address_components": {
"number": "1109",
"predirectional": "N",
"street": "Highland",
"suffix": "St",
"formatted_street": "N Highland St",
"city": "Arlington",
"state": "VA",
"zip": "22201",
"country": "US"
},
"formatted_address": "1109 N Highland St, Arlington, VA 22201"
},
"results": [
{
"address_components": {
"number": "1109",
"predirectional": "N",
"street": "Highland",
"suffix": "St",
"formatted_street": "N Highland St",
"city": "Arlington",
"county": "Arlington County",
"state": "VA",
"zip": "22201",
"country": "US"
},
"formatted_address": "1109 N Highland St, Arlington, VA 22201",
"location": {
"lat": 38.886665,
"lng": -77.094733
},
"accuracy": 1,
"accuracy_type": "rooftop",
"source": "Virginia GIS Clearinghouse"
}
]
}
您只需要进行一次查询,然后从JSON中检索所有信息.
You should only need to make the query once and then retrieve all information from the JSON.
- Batch geocoding
- Single geocoding
location['results'][0]['accuracy']
location['results'][0]['accuracy_type']
- 这是一个很棒的资源 Python词典
- Here's a great resource Dictionaries in Python
- 可能类似以下的内容
- 我只更新了
for-loop
for address in customers['address_string'].values:
geocoded_address = client.geocode(address)
accuracy = geocoded_address['results'][0]['accuracy']
accuracy_type = geocoded_address['results'][0]['accuracy_type']
geocoded_acuracy.append(accuracy)
geocoded_acuracy_type.append(accuracy_type)
customers['accuracy'] = geocoded_acuracy
customers['accuracy_type'] = geocoded_acuracy_type
用于批处理的新代码:
- 在列表中提供您的地址:
- 使用熊猫
- Given your addresses in a list:
- Using pandas
New code for batch processing:
from geocodio import GeocodioClient
import pandas as pd
from pandas.io.json import json_normalize
addresses = ['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']
client = GeocodioClient('api key')
locations = client.geocode(addresses)
df = json_normalize(locations, 'results')
formatted_address accuracy accuracy_type source address_components.number address_components.street address_components.suffix address_components.formatted_street address_components.city address_components.county address_components.state address_components.zip address_components.country location.lat location.lng address_components.predirectional
21236 Birchwood Loop Rd, Chugiak, AK 99567 1.00 rooftop Municipality of Anchorage 21236 Birchwood Loop Rd Birchwood Loop Rd Chugiak Anchorage Municipality AK 99567 US 61.408788 -149.486656 NaN
1731 Bragaw St, Anchorage, AK 99508 1.00 rooftop Municipality of Anchorage 1731 Bragaw St Bragaw St Anchorage Anchorage Municipality AK 99508 US 61.204899 -149.808038 NaN
300 E Fireweed Ln, Anchorage, AK 99503 1.00 rooftop Municipality of Anchorage 300 Fireweed Ln E Fireweed Ln Anchorage Anchorage Municipality AK 99503 US 61.197860 -149.878311 E
300 W Fireweed Ln, Anchorage, AK 99503 0.80 rooftop Municipality of Anchorage 300 Fireweed Ln W Fireweed Ln Anchorage Anchorage Municipality AK 99503 US 61.198304 -149.887737 W
4360 Snider Dr, Wasilla, AK 99654 1.00 range_interpolation TIGER/Line® dataset from the US Census Bureau 4360 Snider Dr Snider Dr Wasilla Matanuska-Susitna Borough AK 99654 US 61.584604 -149.339148 NaN
4360 E Snider Dr, Wasilla, AK 99654 0.90 rooftop Matanuska-Susitna Borough 4360 Snider Dr E Snider Dr Wasilla Matanuska-Susitna Borough AK 99654 US 61.584226 -149.340742 E
4360 Snider Dr, Wasilla, AK 99654 0.90 range_interpolation TIGER/Line® dataset from the US Census Bureau 4360 Snider Dr Snider Dr Wasilla Matanuska-Susitna Borough AK 99654 US 61.584903 -149.338583 NaN
1921 W Dimond Blvd, Anchorage, AK 99515 1.00 rooftop Municipality of Anchorage 1921 Dimond Blvd W Dimond Blvd Anchorage Anchorage Municipality AK 99515 US 61.139078 -149.915706 W
2702 Peger Rd, Fairbanks, AK 99709 1.00 rooftop Fairbanks North Star Borough 2702 Peger Rd Peger Rd Fairbanks Fairbanks North Star Borough AK 99709 US 64.822680 -147.779801 NaN
1651 College Rd, Fairbanks, AK 99709 1.00 rooftop Fairbanks North Star Borough 1651 College Rd College Rd Fairbanks Fairbanks North Star Borough AK 99709 US 64.862441 -147.754823 NaN
898 Ballaine Rd, Fairbanks, AK 99709 1.00 rooftop Fairbanks North Star Borough 898 Ballaine Rd Ballaine Rd Fairbanks Fairbanks North Star Borough AK 99709 US 64.899323 -147.828632 NaN
23819 Immelman Cir, Chugiak, AK 99567 1.00 rooftop Municipality of Anchorage 23819 Immelman Cir Immelman Cir Chugiak Anchorage Municipality AK 99567 US 61.417786 -149.438269 NaN
Big Lake, AK 99652 0.33 place TIGER/Line® dataset from the US Census Bureau NaN NaN NaN NaN Big Lake Matanuska-Susitna Borough AK 99652 US 61.517340 -149.953740 NaN
7205 Shorewood Dr, Palmer, AK 99645 1.00 range_interpolation TIGER/Line® dataset from the US Census Bureau 7205 Shorewood Dr Shorewood Dr Palmer Matanuska-Susitna Borough AK 99645 US 61.625942 -149.266667 NaN
7205 E Shorewood Dr, Palmer, AK 99645 0.90 rooftop Matanuska-Susitna Borough 7205 Shorewood Dr E Shorewood Dr Palmer Matanuska-Susitna Borough AK 99645 US 61.626537 -149.268727 E
7205 Shorewood Dr, Palmer, AK 99645 0.90 range_interpolation TIGER/Line® dataset from the US Census Bureau 7205 Shorewood Dr Shorewood Dr Palmer Matanuska-Susitna Borough AK 99645 US 61.625909 -149.266960 NaN
这篇关于有没有一种方法可以调用一次API,而不是每个生成的输出调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!