Created
April 12, 2016 17:36
-
-
Save JimmyMow/f798b13015c17f0cd972d1adcfcbdaf2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import json | |
import time | |
import urllib2 | |
# Step1 | |
addrs = pd.read_csv('addresses.csv', dtype = {'Zip': str}) | |
addrs['Full Addr'] = addrs['Address'] + ', ' + addrs['City'] + ', ' + addrs['State']+ ', ' + addrs['Zip'] | |
#Step2 | |
address = '1309 E 10th St, 47405' | |
def make_req(url): | |
rslt = urllib2.urlopen(url).read() | |
rslt = json.loads(rslt) | |
return rslt | |
def get_lat_lng(address, num_retries=2): | |
url_base = 'http://maps.googleapis.com/maps/api/geocode/json' + \ | |
'?sensor=false&address=' | |
url = url_base + address.replace(' ', '%20') | |
rslt = make_req(url) | |
print rslt['status'] | |
while rslt['status'] == "OVER_QUERY_LIMIT": | |
num_retries = num_retries - 1 | |
time.sleep(1) | |
rslt = make_req(url) | |
print("Out of while loop") | |
if rslt['status'] == 'OK': | |
lat = rslt['results'][0]['geometry']['location']['lat'] | |
lng = rslt['results'][0]['geometry']['location']['lng'] | |
print(lat, lng) | |
return (lat, lng) | |
else: | |
return "There seems to be an issue with your address" | |
# Testing the Lat/Lng function | |
latlng = get_lat_lng('1309 E 10th St, 47405') | |
print(latlng) | |
# Testing the retry if we hit the query limit | |
for i in range (15): | |
print i, get_lat_lng('1309 E 10th St, 47405') | |
# Step 3 | |
addrs['latlong'] = addrs['Full Addr'].apply(get_lat_lng) | |
print(addrs['latlong']) | |
#Step 4 | |
addrs['key'] = 1 | |
pairs = pd.merge(addrs, addrs, on='key', suffixes=['1', '2']) | |
remove_dup = pairs[pairs.Address1 != pairs.Address2] | |
d = pairs.loc[pairs['Address1'] == pairs['Address2']] | |
# print(pairs) | |
# print(d) | |
print(remove_dup) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment