I have a 500,000 list of latitudes and longitudes coordinates like below:
Latitude Longitude
42.022506 -88.168156
41.877445 -87.723846
29.986801 -90.166314
I am looking to use python to get the city, state, and country for each coordinate in a new column like below:
Latitude Longitude City State Country
42.022506 -88.168156 Streamwood IL United States
41.877445 -87.723846 Chicago IL United States
29.986801 -90.166314 Metairie LA United States
With this large of a dataset, how can this be achieved in python? I have heard of Google's API, Nominatim's API, and Geopy package.
How do I get to run through all of the rows into this code? Right now I have to manually input the latitude and longitude into the last line.
import csv
import pandas as pd
import numpy as np
import math
from geopy.geocoders import Nominatim
input_file = "Lat-Log.csv" # file contains ID, Latitude, Longitude
output_file = "output.csv"
df = pd.read_csv(input_file)
geolocator = Nominatim(user_agent="geoapiExercises")
def city_state_country(coord):
location = geolocator.reverse(coord, exactly_one=True)
address = location.raw['address']
city = address.get('city', '')
state = address.get('state', '')
country = address.get('country', '')
return city, state, country
print(city_state_country("47.470706, -99.704723"))
The output gives me ('Bowdon', 'North Dakota', 'USA'). I am looking to replace the coordinates with my columns (latitude and longitude) to run through my list. How do I input my columns into the code to run through the whole document?