Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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
You can’t perform that action at this time.