Geocoding using Geopandas

Filed Under: Python
Geocoding With Geopandas

Each point on earth can be located given its coordinates, i.e. Latitude and Longitude. Most of the address we retrieve from the real world is in textual form and due to which to access the perfect location of the given address on a map is difficult.

Geocoding is a method where we represent the string of addresses into its corresponding Latitude and Longitude value, which makes it convenient to locate and plot the given address on a map.

Geopandas extends its functionality for geocoding through GeoPy, which is an optional dependency of Geopandas.

In this article, we will use Geopandas to retrieve coordinates of given addresses in text form.

Geocoding Services and Geopy

Chances are that you have used a geocoder before without even realizing i.e. searching a location using text description on google maps. services like Bing Maps, Google Maps, etc to name a few are the popular geocoding services.

GeoPy is not a Geocoding service but simply a python client for several popular geocoding web services. It uses third-party geocoders and other data sources to find the geocode of an address. Geocoding is provided by a number of different services. These services provide APIs, GeoPy library provides an implementation of these APIs in a single package.

You can get the description for all the geocoding services included inside GeoPy here.

Geopandas uses GeoPy library to implement geocoding.

Geocoding Premium Services

There are many Geocoding services available in the market. I have tried GeocodeAPI and they work really well for business applications. They have multiple endpoints to get lat-long from address, reverse geocoding, auto-complete address, etc.

They provide 10,000 free requests per day, which is great if you are just starting to build your application. You can get more details from their pricing page.

Implementation with Geopandas

Geopandas contains geocode class that offers functionality for geocoding.

geocode class needs the following input arguments:

  • the address as a Python string.
  • The geocoding provider we intend to use.

However Geocoding services are either paid or free so prior to selecting a service do go through their Terms of Use, quotas, pricing, geodatabase, and so on. we’ll use the Nominatim geocoder as it doesn’t require any API key to use.

1. Using the geocode Class

#import requires modules
from import geocode

#address we need to locate
loc = 'Machu Picchu'

#finding the location
location = geocode(loc, provider="nominatim" , user_agent = 'my_request')


The geocode class returns GeoDataFrame, which has two columns.

  • geometry – containing latitude and longitude of the given address string.
  • address – complete address as retrieved from the database.

As it is similar to pandas dataframe we can easily manipulate it using pandas indexing functions.

Accessing latitude and longitude values:

#import requires modules
from import geocode

#address we need to locate
loc = 'Machu Picchu'

#finding the location
location = geocode(loc, provider="nominatim" , user_agent = 'my_request')

point = location.geometry.iloc[0]
print('Name: '+ loc )
print('complete address: '+ location.address.iloc[0])
print('longitude: {} '.format(point.x))
print('latitude: {} '.format(point.y))
Machu Picchu Coordinates
Machu Picchu Coordinates

2. Using Geocode class with Pandas DataFrame

Let’s make a dataframe having names of seven wonders of the world and we’ll geocode the information to acquire their respective latitude and longitude.

#Importing the requires modules
import folium
import pandas as pd
from import geocode

#Creating a DataFrame
wonders = ['Taj Mahal', 'Colosseum','Machu Picchu','Christ the Redeemer','Chichen Itza','petra']

df = pd.DataFrame({'wonders' : wonders})

#function to unzip latitude and longitude from GeoDataFrame
def custom_geocoder(address):
    dataframe = geocode(address , provider="nominatim" , user_agent = 'my_request')
    point = dataframe.geometry.iloc[0]
    return pd.Series({'Latitude': point.y, 'Longitude': point.x})

#Applying function to the dataframe
df[['latitude' , 'longitude']]= df.wonders.apply( lambda x: custom_geocoder(x))

DataFrame With Latitude And Longitude
DataFrame With Latitude And Longitude

here we defined a custom function named custom_geocoder which accepts a string of address and geocodes it returning the latitude and longitude values as pandas Series.

Using pandas .apply method we applied our custom function to the column containing names of the seven wonders adding corresponding latitude and longitude values to the DataFrame.

Plotting the Results

# Make sure you run the above code first to plot the results
mapit = folium.Map( location=[0, 0], zoom_start=1 )

for lat , lon in zip(df.latitude , df.longitude):
    folium.Marker( location=[ lat,lon ], fill_color='#43d9de', radius=8 ).add_to( mapit )

Locations On Map
Locations On Map


This article was all about using Geopandas for Geocoding a string of addresses. we also applied geocoding to a pandas Dataframe having names of the seven wonders of the world and visualized the results on a map using Folium.

Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages