-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipcode-radius-search.py
More file actions
36 lines (27 loc) · 1.03 KB
/
zipcode-radius-search.py
File metadata and controls
36 lines (27 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import csv
import requests
import pandas as pd
from pandas import json_normalize
import yaml
with open("config.yml", 'r') as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.BaseLoader)
API_KEY = cfg['API_KEY']
# API key and declares final as panda dataframe
final = pd.DataFrame()
# pull source.csv into a panda dataframe
df = pd.read_csv('testzips.csv')
# opens output.csv and sets to 'f'
f = open('output.csv')
csv_file = csv.writer(f)
# loop used to iterate over
for index, row in df.iterrows():
# API request URL that replaces %s with zipcode value from the dataframe and sets to 'r'
r = requests.get(
'https://api.zip-codes.com/ZipCodesAPI.svc/1.0/FindZipCodesInRadius?zipcode=%s&minimumradius=0&maximumradius=30&key=%s' % (
row['zip'], API_KEY))
df_normalize = r.json()
# iterates over each item in the normalized response data
for item in df_normalize['DataList']:
df2 = json_normalize(item)
final = final.append(df2)
final.to_csv('output.csv', index=False, encoding='utf-8')