
Get full address from geo coordinates using Python for free

Piotr
Founder
•
•3 min read
analytics
Recently, I have been tasked to retrieve full addresses from geo coordinates for over 4k records. Nominatim is a great tool for this task, but it has a limit of 1 request per second.
You can read this on Medium here.
IMPORT LIBRARIES
python
1import pandas as pd
2from geopy.geocoders import Nominatim
3from geopy.extra.rate_limiter import RateLimiterIMPORT DATA
python
1df = pd.read_csv("france_ria_locations.csv")
2
I imported csv file with geo coordinates to pandas DataFrame and I already had coords pair as a string. So, we will need to convert it to a tuple of floats, but first let's initialize Nominatim geocoder.
python
1# Initialize the geocoder
2geolocator = Nominatim(user_agent="myGeocoder")and create a rate limiter:
python
1# Create a rate limiter
2geocode = RateLimiter(geolocator.reverse, min_delay_seconds=1)CONVERT COORDS TO TUPLE OF FLOATS
python
1## convert 'coords' from string to a tuple of floats
2df['coords'] = df['coords'].apply(lambda x: tuple(map(float, x.strip('()').split(','))))The next step is to add 'location' column to DataFrame by applying Nominatim geocoder to 'coords' column.
python
1# Add 'location' column to dataframe by applying geocode to 'coords' column
2df['location'] = df['coords'].apply(geocode)SHAPING THE DATAFRAME
python
1# Add 'address', 'city' and 'zip' columns
2df['address'] = df['location'].apply(lambda loc: loc.raw['address']['road'] if 'road' in loc.raw['address'] else None)
3df['city'] = df['location'].apply(lambda loc: loc.raw['address']['town'] if 'town' in loc.raw['address'] else None)
4df['zip'] = df['location'].apply(lambda loc: loc.raw['address']['postcode'] if 'postcode' in loc.raw['address'] else None)
5
SAVE TO CSV
python
1# Save to csv
2df.to_csv('france_ria_locations_with_address.csv', index=False)Happy coding!
Piotr