Reputation: 19
I have a df of 500 rows with columns id, name, latitude1, longitude1, latitude2, longitude2. The table is populated with coordinates of values of work place(lat1 and long1) and values of home address (lat2 and long2). I created a new column ("dist") and want to calculate the distance between work place and home of every person in the table.
I've tried geopy.distance.distance()
and geopy.distance.geodesic()
- which gave me an error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
When I add .all()
it doesn't help the code.
I also tried using hs.haversine
but it gave back another error: TypeError: cannot convert the series to <class 'float'>
What am I missing?
Code:
home_lat = vv.user_home_lat
home_long = vv.user_home_long
home_add = (home_lat, home_long)
vanue_lat = vv.visit_lat
vanue_long = vv.visit_long
vanue_add = (vanue_lat, vanue_long)
# hs.haversine(home_add, vanue_add)
# vv['distance_home_vanue'] = geopy.distance.distance((home_lat, home_long), (vanue_lat, vanue_long)).miles
distance_home_vanue = geodesic(home_add, vanue_add).miles
Full error stack traceback:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-187-072ccb994de5> in <module>
11 # vv['distance_home_vanue'] = geopy.distance.distance((home_lat, home_long), (vanue_lat, vanue_long)).miles
12
---> 13 distance_home_vanue = geodesic(home_add, vanue_add).miles
~\anaconda3\lib\site-packages\geopy\distance.py in __init__(self,
*args, **kwargs)
414 self.set_ellipsoid(kwargs.pop('ellipsoid', 'WGS-84'))
415 major, minor, f = self.ELLIPSOID
--> 416 super().__init__(*args, **kwargs)
417
418 def set_ellipsoid(self, ellipsoid):
~\anaconda3\lib\site-packages\geopy\distance.py in __init__(self,
*args, **kwargs)
198 elif len(args) > 1:
199 for a, b in util.pairwise(args):
--> 200 kilometers += self.measure(a, b)
201
202 kilometers += units.kilometers(**kwargs)
~\anaconda3\lib\site-packages\geopy\distance.py in measure(self, a, b)
434 # Call geographiclib routines for measure and destination
435 def measure(self, a, b):
--> 436 a, b = Point(a), Point(b)
437 _ensure_same_altitude(a, b)
438 lat1, lon1 = a.latitude, a.longitude
~\anaconda3\lib\site-packages\geopy\point.py in __new__(cls, latitude, longitude, altitude)
173 )
174 else:
--> 175 return cls.from_sequence(seq)
176
177 if single_arg:
~\anaconda3\lib\site-packages\geopy\point.py in from_sequence(cls, seq)
470 raise ValueError('When creating a Point from sequence, it '
471 'must not have more than 3 items.')
--> 472 return cls(*args)
473
474 @classmethod
~\anaconda3\lib\site-packages\geopy\point.py in __new__(cls, latitude, longitude, altitude)
186
187 latitude, longitude, altitude = \
--> 188 _normalize_coordinates(latitude, longitude, altitude)
189
190 self = super().__new__(cls)
~\anaconda3\lib\site-packages\geopy\point.py in
_normalize_coordinates(latitude, longitude, altitude)
55
56 def _normalize_coordinates(latitude, longitude, altitude):
---> 57 latitude = float(latitude or 0.0)
58 longitude = float(longitude or 0.0)
59 altitude = float(altitude or 0.0)
~\anaconda3\lib\site-packages\pandas\core\generic.py in
__nonzero__(self) 1325 def __nonzero__(self): 1326 raise ValueError(
-> 1327 f"The truth value of a {type(self).__name__} is ambiguous. " 1328 "Use a.empty, a.bool(), a.item(), a.any() or a.all()." 1329 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Upvotes: 0
Views: 749
Reputation: 21812
Try using the Dataframes apply method:
from geopy import distance
distances = vv.apply(
lambda row: distance.distance(
(row['user_home_lat'], row['user_home_long']), (row['visit_lat'], row['visit_long'])
), axis=1
)
Upvotes: 1