Skip to content

Instantly share code, notes, and snippets.

@vb100
Created July 11, 2017 20:30
Show Gist options
  • Save vb100/228ce299a842ee66f349cc7a8b4bb7cd to your computer and use it in GitHub Desktop.
Save vb100/228ce299a842ee66f349cc7a8b4bb7cd to your computer and use it in GitHub Desktop.
This Python application correctly generate Longitude and Latitude of an any spatial object that is defined by it's address features in Pandas (Python module) dataframe by using OpenStreetMap API that is very friendly with Python environment and add those Longitudes and Latitudes to an existing data table.
import pandas, os
os.listdir()
df1=pandas.read_csv('supermarkets.csv')
import geopy
dir(geopy)
from geopy.geocoders import Nominatim
nom = Nominatim()
n=nom.geocode("London tower")
print(n.latitude, n.longitude)
#add a new Adresss column
df1["Address"]=df1["Address"]+", "+df1["City"]+", "+df1["State"]+", "+df1["Country"]
#send this string to Geocode method
df1["Coordinates"]=df1["Address"].apply(nom.geocode)
#Set values to Longitudes and Latitudes
df1["Latitude"]=df1["Coordinates"].apply(lambda x: x.latitude if x != None else None)
df1["Longitude"]=df1["Coordinates"].apply(lambda x: x.longitude if x != None else None)
ID Address City State Country Name Employees
1 3666 21st St San Francisco CA 94114 USA Madeira 8
2 735 Dolores St San Francisco CA 94119 USA Bready Shop 15
3 332 Hill St San Francisco California 94114 USA Super River 25
4 3995 23rd St San Francisco CA 94114 USA Ben's Shop 10
5 1056 Sanchez St San Francisco California USA Sanchez 12
6 551 Alvarado St San Francisco CA 94114 USA Richvalley 20
@isaco
Copy link

isaco commented Aug 29, 2017

Hi,
this code BELOW can produce error because of TIME....

#send this string to Geocode method
df1["Coordinates"]=df1["Address"].apply(nom.geocode)

Code with time delay will be useful example !

Best regards,
Isaco

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment