Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
991 views
in Technique[技术] by (71.8m points)

typeerror - python getting ValueError when computing distance between 2 places using coordinates

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

~anaconda3libsite-packagesgeopydistance.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):

~anaconda3libsite-packagesgeopydistance.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)

~anaconda3libsite-packagesgeopydistance.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

~anaconda3libsite-packagesgeopypoint.py in __new__(cls, latitude, longitude, altitude)
    173                     )
    174                 else:
--> 175                     return cls.from_sequence(seq)
    176 
    177         if single_arg:

~anaconda3libsite-packagesgeopypoint.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

~anaconda3libsite-packagesgeopypoint.py in __new__(cls, latitude, longitude, altitude)
    186 
    187         latitude, longitude, altitude = 
--> 188             _normalize_coordinates(latitude, longitude, altitude)
    189 
    190         self = super().__new__(cls)

~anaconda3libsite-packagesgeopypoint.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)

~anaconda3libsite-packagespandascoregeneric.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().
question from:https://stackoverflow.com/questions/65861247/python-getting-valueerror-when-computing-distance-between-2-places-using-coordin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...