I'm trying to right a code using the Geopy library to get lat long for some adresses.
So I wrote this function:
def latlong(street, city):
try:
location = geolocator.geocode(str(street) + ', ' + str(city))
if location is None:
return 0, 0
return location.latitude, location.longitude
except:
return 0, 0
df['LAT'], df['LONG'] = df.apply(lambda x: latlong(x['STREET'], x['CITY']), axis = 1)
The code runs for some time and then I get this error:
ValueError: too many values to unpack (expected 2)
However if I change the code to have two function, one for lat and one for long it works just fine, but it takes twice the time to run and I'll do twice as many requests to the API.
def lat(street, cidade):
location = geolocator.geocode(str(street) + ', ' + str(city))
if location is None:
return 0
return location.latitude
def long(street, cidade):
location = geolocator.geocode(str(street) + ', ' + str(city))
if location is None:
return 0
return location.longitude
df['LAT'] = df.apply(lambda x: lat(x['STREET'], x['CITY']), axis = 1)
df['LONG'] = df.apply(lambda x: long(x['STREET'], x['CITY']), axis = 1)
Is there a way two call a function using apply and lambda to fill two columns of a dataframe at the same time? How can I fix this 'too many values to unpack' error?
Thanks in advance for the help!