Search code examples
pythonpandasgeopy

Pandas: Passing format options into functions


I want to extract coordinates from postcodes as a new df column.

The functionality from the geopy module is:

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode('%s tn6 3rn')

print((location.latitude, location.longitude))
(51.0459837, 0.2192646)

My function to apply to this to a single value works:

def pcodeToCoor(x):
    geolocator = Nominatim()
    location = geolocator.geocode(x)
    return ((location.latitude, location.longitude))

pcodeToCoor('%s tn6 3rn')
(51.0459837, 0.2192646)

But when passing the function to a test df:

    name    postcode
0   jd      tn6 3rn
1   hf      en6 1dg
2   ss      sw17 0ju

df['coordinate'] = df['postcode'].map(pcodeToCoor)

I get AttributeError: 'NoneType' object has no attribute 'latitude. Note I can recreate this error by removing %s from the basic api functionality.

The question is, how do I use %s in my function? I imagine the answer's very simple but nothing I've tried works!


Solution

  • You could do something like this if you are going to use this function consistently this way, or you code code a check to see if your string starts with "%s".

    def pcodeToCoor(x):
        geolocator = Nominatim()
        location = geolocator.geocode('%s '+x)
        return ((location.latitude, location.longitude))
    

    Edit:

    def pcodeToCoor(x):
        if x[0:2] != '%s':
            x = '%s ' + x 
        geolocator = Nominatim()
        location = geolocator.geocode(x)
        return ((location.latitude, location.longitude))
    

    Test:

    pcodeToCoor('%s tn6 3rn')
    

    Output:

    (51.0459837, 0.2192646)
    

    Test 2:

    df['postcode'].map(pcodeToCoor)
    

    Output:

    0     (51.0459837, 0.2192646)
    1    (51.7206134, -0.2042041)
    2    (51.3866947, -0.1800573)
    Name: postcode, dtype: object