pandas - Could I use python to retrieve a number of zip code within a radius -
picture: retrieve number of zip code within radius in excel
as can see on picture above, use excel expand target zip code (a list of zip code randomly type in). , can list of expanded zip code within 35 miles radius (it can 10 mils, 50 miles, depending on want at) of target zip code.
however, process takes long time, if have different groups of target zip code expand. thinking whether there more efficient way in python instead.
i know geopy can me distance between 2 zip code, cannot give list of zip within location.
for usa zip codes, there pyzipcode package.
single zip code query:
from pyzipcode import zipcodedatabase zcdb = zipcodedatabase() in_radius = [z.zip z in zcdb.get_zipcodes_around_radius('55001', 8)] # ('zip', radius in miles) radius_utf = [x.encode('utf-8') x in in_radius] # unicode list utf list output:
>>> in_radius [u'54016', u'54021', u'55001', u'55003', u'55042', u'55043', u'55082', u'55129'] >>> radius_utf ['54016', '54021', '55001', '55003', '55042', '55043', '55082', '55129'] multiple zip codes @ once:
from pyzipcode import zipcodedatabase zcdb = zipcodedatabase() input_zips = [] input_zips = raw_input("enter zip codes (separated comma): ").replace(" ", "").split(",") # remove whitespace , split comma list input_radius = raw_input("enter radius entered zip code (in miles): ") # in python 3.x change both raw_input() input() y in range(0, len(input_zips)): # loop find , print radius lists in_radius = [z.zip z in zcdb.get_zipcodes_around_radius(input_zips[y], input_radius)] # ('zip', radius in miles) radius_utf = map(int, [x.encode('utf-8') x in in_radius]) # unicode list utf list , map convert str list int list print("target zip: %s, radius %s miles, list of zip codes in radius:\n%s\n" %(input_zips[y], input_radius, radius_utf)) output:
enter zip codes (separated comma): 54021, 55001 enter radius entered zip code (in miles): 10 target zip: 54021, radius 10 miles, list of zip codes in radius: [54021, 55001, 55033, 55043, 55089, 55129] target zip: 55001, radius 10 miles, list of zip codes in radius: [54016, 54021, 54082, 55001, 55003, 55016, 55033, 55042, 55043, 55082, 55090, 55115, 55125, 55129]
Comments
Post a Comment