Created
November 12, 2018 00:24
-
-
Save erikgregorywebb/82a73244744c0cf5e34291c1e8188d2d to your computer and use it in GitHub Desktop.
This file contains 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 libraries | |
import pandas as pd | |
import googlemaps | |
import time | |
# read in the data | |
scarsdale_properties = pd.read_csv('/Users/erikgregorywebb/Documents/Python/scarsdale/properties-clean.csv') | |
# prepare list of addresses | |
scarsdale_properties['FullAddress'] = scarsdale_properties['Address'] + ' Scarsdale, NY 10583' | |
addresses = list(scarsdale_properties['FullAddress']) | |
# prepare the API Call | |
gmaps = googlemaps.Client(key='YOUR-KEY-HERE') | |
# geocode addresses to obtain geographic coordinates | |
lats = [] | |
lngs = [] | |
exceptions = [] | |
for address in addresses: | |
time.sleep(1) | |
try: | |
geo = gmaps.geocode(address) | |
lat = geo[0]['geometry']['location']['lat'] | |
lng = geo[0]['geometry']['location']['lng'] | |
except: | |
print(address) | |
exceptions.append(address) | |
lat = "" | |
lng = "" | |
time.sleep(2) | |
lats.append(lat) | |
lngs.append(lng) | |
# format results | |
geo_data = pd.DataFrame({'FullAddress': addresses, 'LAT': lats, 'LNG': lngs}) | |
scarsdale_properties = pd.merge(scarsdale_properties, geo_data, on='FullAddress', how='outer') | |
scarsdale_properties['Coordinates'] = scarsdale_properties['LAT'].map(str) + ', ' + scarsdale_properties['LNG'].map(str) | |
# export | |
scarsdale_properties.to_csv('/Users/erikgregorywebb/Documents/Python/scarsdale/properties-clean-coordinates.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment