python在使用坐标计算两个位置之间的距离时获取ValueError
我有一个500行的df,其中包含id、name、latitude1、longitude1、latitude2、longitude2列。 表中填充了工作地点(lat1和long1)值和家庭地址(lat2和long2)值的坐标。我创建了一个新列(“dist”),希望计算表中每个人的工作地点和家之间的距离 我尝试了python在使用坐标计算两个位置之间的距离时获取ValueError,python,typeerror,valueerror,geopy,haversine,Python,Typeerror,Valueerror,Geopy,Haversine,我有一个500行的df,其中包含id、name、latitude1、longitude1、latitude2、longitude2列。 表中填充了工作地点(lat1和long1)值和家庭地址(lat2和long2)值的坐标。我创建了一个新列(“dist”),希望计算表中每个人的工作地点和家之间的距离 我尝试了geopy.distance.distance()和geopy.distance.geodesic()-这给了我一个错误: ValueError: The truth value of a
geopy.distance.distance()
和geopy.distance.geodesic()
-这给了我一个错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
当我添加.all()
时,它对代码没有帮助
我还尝试使用hs.haversine
,但它返回了另一个错误:TypeError:无法将序列转换为
我错过了什么
代码:
完整错误堆栈回溯:
-------------------------------------------------------------中的ValueError回溯(最近一次调用)
11#vv['distance_home_vanue']=地理距离距离((home_lat,home_long),(vanue_lat,vanue_long))英里
12
--->13距离\u home\u vanue=测地线(home\u add,vanue\u add)。英里
~\anaconda3\lib\site packages\geopy\distance.py in\uuuuu init\uuuuu(self,
*args,**kwargs)
414自置椭球体(kwargs.pop(“椭球体”、“WGS-84”))
415大调、小调,f=自椭球体
-->416 super()
417
418 def set_椭球体(自身,椭球体):
~\anaconda3\lib\site packages\geopy\distance.py in\uuuuu init\uuuuu(self,
*args,**kwargs)
198 elif len(args)>1:
199对于a,b,成对使用(参数):
-->200公里+=自我测量(a,b)
201
202公里+=单位公里(**kwargs)
测量中的~\anaconda3\lib\site packages\geopy\distance.py(self,a,b)
434#调用GeographicalIB例程进行度量和目标
435 def测量(自身、a、b):
-->436 a,b=点(a),点(b)
437确保相同高度(a、b)
438纬度1,经度1=a.纬度,a.经度
~\anaconda3\lib\site packages\geopy\point.py in\uuuuu new\uuuu(cls、纬度、经度、海拔)
173 )
174.其他:
-->175从_序列返回cls(序列)
176
177如果是单参数:
~\anaconda3\lib\site packages\geopy\point.py在from_序列中(cls,seq)
470 raise VALUERROR('从序列创建点时,它'
471“不得有超过3项”。)
-->472返回cls(*args)
473
474@classmethod
~\anaconda3\lib\site packages\geopy\point.py in\uuuuu new\uuuu(cls、纬度、经度、海拔)
186
187纬度、经度、海拔=\
-->188_标准化_坐标(纬度、经度、高度)
189
190 self=super()
中的~\anaconda3\lib\site packages\geopy\point.py
_标准化坐标(纬度、经度、高度)
55
56 def_标准化坐标(纬度、经度、高度):
--->57纬度=浮动(纬度或0.0)
58经度=浮动(经度或0.0)
59高度=浮动(高度或0.0)
中的~\anaconda3\lib\site packages\pandas\core\generic.py
__非零(自)1325定义非零(自):1326提升值错误(
->1327 f“一个{type(self).\uuuu name\uuuuuu}的真值是不明确的。”1328“使用a.empty、a.bool()、a.item()、a.any()或a.all()
ValueError:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。
尝试使用数据帧应用方法:
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
)
感谢您提供错误消息。请在错误消息之前包含完整的堆栈跟踪(“文件中的行…”内容),以及您尝试的实际代码,而不仅仅是您调用的函数。数据帧的摘录也有助于做到这一点。@CrazyChucky我编辑了一篇博文,其中有一个错误--KeyError:'user\u home\u lat'@xRay Updated,请检查
--------------------------------------------------------------------------- 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().
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
)