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!