0
latitudes=[]
longitudes=[]
addresses = final_data['Address'].tolist()
for address in addresses:
    location = geolocator.geocode(address)
    print(type(location)) #the location type is none 
    latitudes.append(location.latitude)
    longitudes.append(location.longitude)
 #Output : class 'NoneType'> 
 #at line 7:AttributeError: 'NoneType' object has no attribute 'latitude'

I am trying to get the coordinates of various locations which were stored in a column "Address" in the data frame. I am getting the location for single location but while iterating a loop, i am unable to go forward.

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="dfb568960cd8fda99a9cec76f8b97191")
location = geolocator.geocode("Andhra Pradesh, India")
print(type(location))
# Output : <class 'geopy.location.Location'>

1 Answers1

0

As per geopy's doc, None is returned when the address could not be resolved. So this case should be explicitly handled:

latitudes=[]
longitudes=[]
addresses = final_data['Address'].tolist()
for address in addresses:
    location = geolocator.geocode(address)
    if location is not None:
        latitudes.append(location.latitude)
        longitudes.append(location.longitude)
    else:
        print(f"Could not find Location for {address!r}")
KostyaEsmukov
  • 848
  • 6
  • 11