How to find longitude and latitude for a list of Regions or Country using Python Last Updated : 16 Jul, 2020 Improve Improve Like Article Like Save Share Report Geopy is a Python 2 and 3 client for several popular geocoding web services. Geocoding is the process to identify geographic coordinates like latitude and longitude of a given city/country/address. This is very useful during marking position on the map in data visualization. We use geopy.exec, geocodertimedout, geolocators, geopy.geocoder in the below code to fetch the results Installation This module does not come built-in with Python. To install it type the below command in the terminal. pip install geopy Example: Let’s create a pandas dataframe containing a list of regions or countries. # Import pandas package import pandas as pd import numpy as np # Define a dictionary containing data data = {'City':['Bangalore', 'Mumbai', 'Chennai', 'Delhi']} # Convert the dictionary into DataFrame df = pd.DataFrame(data) # Observe the result df Output: Now Let’s find the latitude and longitude of the following regions or countries. from geopy.exc import GeocoderTimedOut from geopy.geocoders import Nominatim # declare an empty list to store # latitude and longitude of values # of city column longitude = [] latitude = [] # function to find the coordinate # of a given city def findGeocode(city): # try and catch is used to overcome # the exception thrown by geolocator # using geocodertimedout try: # Specify the user_agent as your # app name it should not be none geolocator = Nominatim(user_agent="your_app_name") return geolocator.geocode(city) except GeocoderTimedOut: return findGeocode(city) # each value from city column # will be fetched and sent to # function find_geocode for i in (df["City"]): if findGeocode(i) != None: loc = findGeocode(i) # coordinates returned from # function is stored into # two separate list latitude.append(loc.latitude) longitude.append(loc.longitude) # if coordinate for a city not # found, insert "NaN" indicating # missing value else: latitude.append(np.nan) longitude.append(np.nan) Showing the output produced as dataframe. # now add this column to dataframe df["Longitude"] = longitude df["Latitude"] = latitude df Output: Like Article Suggest improvement Previous Creating a Receipt Calculator using Python Next Port scanner using 'python-nmap' Share your thoughts in the comments Add Your Comment Please Login to comment... Similar Reads Get the city, state, and country names from latitude and longitude using Python Find the location with specified latitude and longitude using Python Web scraper for extracting emails based on keywords and regions GUI application to search a country name from a given state or city name using Python Python IMDbPY – Getting the country codes of the series Get Country From Phone Number - Python Python Program to Get Country Information Python | Convert list of string to list of list Python | Convert list of tuples to list of list Python | Convert List of String List to String List Like D deepak_k Follow Article Tags : python-modules Python-projects python-utility Machine Learning Python Python Programs Write From Home Practice Tags : Machine Learningpython