pythonpandaslambdageolocationnominatim

How to create new pandas column while checking for NoneType


I am working on creating a dataframe of location data using Nominatim to pull out longitude and latitude from city names. Here is my code so far:

from geopy.geocoders import Nominatim
import pandas as pd

geolocator = Nominatim(user_agent="measurements", timeout=3)
locations = ['Portland, Oregon', 'Seattle, Washington','New York, New York','Columbia, South Carolina']
df = pd.DataFrame(locations, columns=['locations'])

df['latlon'] = df['locations'].apply(lambda x: geolocator.geocode(x, addressdetails=True, language='en'))

I want to pull out the latitude and longitude from the 2nd column but have been having issues parsing the Location(...) data properly. So, I've edited my code above to parse it more directly:

df['lat'] = df['locations'].apply(lambda x: geolocator.geocode(x, addressdetails=True, language='en').latitude)
df['lon'] = df['locations'].apply(lambda x: geolocator.geocode(x, addressdetails=True, language='en').longitude)

This all works for my reproducible example above, but I am running into a problem where I get the following error:

AttributeError: 'NoneType' object has no attribute 'latitude'

How can I write the second chunk of code above to have a check for the data type "NoneType" and then continue on evaluating the lambda expression if NoneType is found?


Solution

  • When geolocator.geocode result is None can cause this problem.Pre emptiness judgment.

    def extract_coordinates(location):
    geocode_result = geolocator.geocode(location, addressdetails=True, language='en')
    if geocode_result is not None:
        latitude = geocode_result.latitude
        longitude = geocode_result.longitude
        return latitude, longitude
    else:
        return None, None
    
    
    df[['latitude', 'longitude']] = 
    df['locations'].apply(extract_coordinates).apply(pd.Series)
    
    print(df)