I have an excel file containing two columns. The first column is "City" and the second column is "Country". I want my python code to loop through each row and find the latitude and longitude for each row. The python code creates two new columns "Latitude" and "Longitude" as desired but is returning None for all values. Any help is appreciated.
import pandas as pd
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="Your_Name")
df = pd.read_excel("location.xlsx")
longitude = []
latitude = []
city = df["City"]
country = df["Country"]
for i in df.index:
loc = geolocator.geocode(city + ',' + country)
if loc is None:
latitude.append(None)
longitude.append(None)
else:
latitude.append(loc.latitude)
longitude.append(loc.longitude)
df["Longitude"] = longitude
df["Latitude"] = latitude
print(df)