How to Geocode with Python and pandas

Peter Haas
3 min readJan 27, 2020

Are you running a data analysis on a dataset that has physical addresses? Would you like to plot those on a map? You’ll need to geocode them to generate their latitude and longitude co-ordinates.

Photo by Capturing the human heart. on Unsplash

How to look up the geocode for a single address

You’ll be using geopy, a Python client for several popular geocoding webservices. Start by installing geopy.

pip install geopy

Import the library you just installed. You’ll be using the Nominatim geocoding service. There are many geocoding services available, but this one does not require an API key to access.

from geopy.geocoders import Nominatim

Create a geolocator object using the Openstreet Nominatim API. It’s a good idea to increase the default timeout setting from 1s to 10s so that you don’t get a TimedOut exception. You’ll also need to enter a name (any name) for the ‘user_agent’ attribute.

To test out the geolocator, pass it an address. Print out the location object. Then print the latitude and longitude from the object.

geolocator = Nominatim(timeout=10, user_agent = "myGeolocator")
location = geolocator.geocode('4550 Kester Mill Rd,Winston-Salem,NC')
print(location)
print((location.latitude, location.longitude))

--

--

Peter Haas
Peter Haas

Written by Peter Haas

Python Data Analyst. Writing about Python programming, data analysis, data visualization, and whatever I’m interested in at the moment.

No responses yet