Python 如何查找距离b/w地理位置w/测地线w/分为4列的坐标以创建距离列--ValueError
我创建了一个较短且虚假的数据集。我已经将我的Location_1和Location_2分为两列,每列产生四列。现在我需要在上面使用测地线。在进行测试运行时,我可以通过一次观察手动完成。但我似乎无法使它适用于整列数据,也无法为距离创建新列 下面的代码将一直运行到最后一行,在那里它抛出了一个错误,并反映了我对原始日期集的处理,我无法共享,这是数千次观察。敲出的线也抛出了一个错误,但一个不同的错误Python 如何查找距离b/w地理位置w/测地线w/分为4列的坐标以创建距离列--ValueError,python,pandas,geolocation,distance,Python,Pandas,Geolocation,Distance,我创建了一个较短且虚假的数据集。我已经将我的Location_1和Location_2分为两列,每列产生四列。现在我需要在上面使用测地线。在进行测试运行时,我可以通过一次观察手动完成。但我似乎无法使它适用于整列数据,也无法为距离创建新列 下面的代码将一直运行到最后一行,在那里它抛出了一个错误,并反映了我对原始日期集的处理,我无法共享,这是数千次观察。敲出的线也抛出了一个错误,但一个不同的错误 places_data = pd.DataFrame( {"Place_1&quo
places_data = pd.DataFrame(
{"Place_1": ["Disneyland Park", "Empire State Building", "Yosemite Park", "Disney World Park", "Rockefeller Tower", "Grand Canyon"],
"Places": ["Peaches", "Apples", "Peaches", "Peaches", "Apples", "Peaches"]}
)
other_places = places_data.copy()
other_places.loc[(other_places["Places"] == "Peaches"), "Sites"] = "Georgia Aquarium"
other_places.loc[(other_places["Places"] == "Apples"), "Sites"] = "World of Coca-Cola"
other_places["Loc_1"] = other_places["Place_1"].apply(geolocator.geocode).apply(lambda loc: tuple(loc.point) if loc else None)
other_places["Loc_2"] = other_places["Sites"].apply(geolocator.geocode).apply(lambda loc: tuple(loc.point) if loc else None)
places_data['Loc_1'] = places_data.Place_1.map(dict(other_places[['Place_1','Loc_1']].to_numpy()))
places_data['Loc_2'] = places_data.Places.map(dict(other_places[['Places','Loc_2']].to_numpy()))
places_data[['Lat_1', 'Long_1', 'Alt_1']] = pd.DataFrame(places_data['Loc_1'].tolist(), index = places_data.index)
places_data[['Lat_2', 'Long_2', 'Alt_2']] = pd.DataFrame(places_data['Loc_2'].tolist(), index = places_data.index)
#places_data["Distance"] = geodesic(places_data["Loc_1"], places_data["Loc_2"]).miles
places_data["Distance"] = geodesic(
(places_data["Lat_1"], places_data["Long_1"]),
(places_data["Lat_2"], places_data["Long_2"])
).miles
这是我的测试代码,它确实有效
geodesic(
(geolocator.geocode("Disneyland Park").latitude, geolocator.geocode("Disneyland Park").longitude),
(geolocator.geocode("World of Coca-Cola").latitude, geolocator.geocode("Disneyland Park").longitude)
)
返回值:距离(5.629067391427556)
错误摘要:
ValueError:序列的真值不明确。使用a.empty,
a、 bool()、a.item()、a.any()或a.all()
下面是错误:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-772-f5a592d7d527> in <module>()
22 places_data["Distance"] = geodesic(
23 (places_data["Lat_1"], places_data["Long_1"]),
---> 24 (places_data["Lat_2"], places_data["Long_2"])
25 ).miles
7 frames /usr/local/lib/python3.6/dist-packages/geopy/distance.py in
__init__(self, *args, **kwargs)
387 kwargs.pop('iterations', 0)
388 major, minor, f = self.ELLIPSOID
--> 389 super(geodesic, self).__init__(*args, **kwargs)
390
391 def set_ellipsoid(self, ellipsoid):
/usr/local/lib/python3.6/dist-packages/geopy/distance.py in
__init__(self, *args, **kwargs)
162 elif len(args) > 1:
163 for a, b in util.pairwise(args):
--> 164 kilometers += self.measure(a, b)
165
166 kilometers += units.kilometers(**kwargs)
/usr/local/lib/python3.6/dist-packages/geopy/distance.py in measure(self, a, b)
408 # Call geographiclib routines for measure and destination
409 def measure(self, a, b):
--> 410 a, b = Point(a), Point(b)
411 lat1, lon1 = a.latitude, a.longitude
412 lat2, lon2 = b.latitude, b.longitude
/usr/local/lib/python3.6/dist-packages/geopy/point.py in __new__(cls, latitude, longitude, altitude)
169 )
170 else:
--> 171 return cls.from_sequence(seq)
172
173 if single_arg:
/usr/local/lib/python3.6/dist-packages/geopy/point.py in from_sequence(cls, seq)
408 raise ValueError('When creating a Point from sequence, it '
409 'must not have more than 3 items.')
--> 410 return cls(*args)
411
412 @classmethod
/usr/local/lib/python3.6/dist-packages/geopy/point.py in __new__(cls, latitude, longitude, altitude)
181
182 latitude, longitude, altitude = \
--> 183 _normalize_coordinates(latitude, longitude, altitude)
184
185 self = super(Point, cls).__new__(cls)
/usr/local/lib/python3.6/dist-packages/geopy/point.py in
_normalize_coordinates(latitude, longitude, altitude)
63
64 def _normalize_coordinates(latitude, longitude, altitude):
---> 65 latitude = float(latitude or 0.0)
66 longitude = float(longitude or 0.0)
67 altitude = float(altitude or 0.0)
/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in
__nonzero__(self) 1477 def __nonzero__(self): 1478 raise ValueError(
-> 1479 f"The truth value of a {type(self).__name__} is ambiguous. " 1480 "Use a.empty, a.bool(), a.item(), a.any() or a.all()." 1481 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
--------------------------------------------------------------------------------------在()中的ValueError回溯(最近一次调用)
22个位置_数据[“距离”]=测地线(
23(位置数据[“横向1”]、位置数据[“纵向1”]),
--->24(位置数据[“Lat_2”]、位置数据[“Long_2”])
25)英里
7帧/usr/local/lib/python3.6/dist-packages/geopy/distance.py in
__初始化(self,*args,**kwargs)
387 kwargs.pop('iterations',0)
388大调,小调,f=自椭球体
-->389 super(测地线,自)
390
391 def set_椭球体(自身,椭球体):
/usr/local/lib/python3.6/dist-packages/geopy/distance.py in
__初始化(self,*args,**kwargs)
162 elif len(args)>1:
163对于a,b,使用成对(args):
-->164公里+=自我测量(a,b)
165
166公里+=单位公里(**kwargs)
/usr/local/lib/python3.6/dist-packages/geopy/distance.py in measure(self,a,b)
408#调用GeographicalIB例程进行度量和目标
409 def测量(自身、a、b):
-->410 a,b=点(a),点(b)
411lat1,lon1=a.纬度,a.经度
412lat2,lon2=b.纬度,b.经度
/usr/local/lib/python3.6/dist-packages/geopy/point.py in\uuuuu new\uuuuu(cls、纬度、经度、海拔)
169 )
170其他:
-->171从_序列返回cls(序列)
172
173如果为单参数:
/usr/local/lib/python3.6/dist-packages/geopy/point.py from_序列(cls,seq)
408 raise VALUERROR('从序列创建点时,它'
409“不得有超过3项”。)
-->410返回cls(*args)
411
412@classmethod
/usr/local/lib/python3.6/dist-packages/geopy/point.py in\uuuuu new\uuuuu(cls、纬度、经度、海拔)
181
182纬度、经度、海拔=\
-->183标准化坐标(纬度、经度、高度)
184
185 self=super(点,cls)。\uuuu新的\uuuuu(cls)
/usr/local/lib/python3.6/dist-packages/geopy/point.py in
_标准化坐标(纬度、经度、高度)
63
64 def_标准化_坐标(纬度、经度、高度):
--->65纬度=浮动(纬度或0.0)
66经度=浮动(经度或0.0)
67高度=浮动(高度或0.0)
/中的usr/local/lib/python3.6/dist-packages/pandas/core/generic.py
__非零(自身)1477定义非零(自身):1478提升值错误(
->1479 f“一个{type(self).{name}的真值是不明确的。”1480“使用a.empty、a.bool()、a.item()、a.any()或a.all()。”1481)
ValueError:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。
在列表中理解zip
位置1和位置2的列,并计算每对位置1和位置2的测地
距离:
places_data['Distance'] = [geodesic(x, y).miles for x, y in zip(places_data['Loc_1'], places_data['Loc_2'])]
在列表中,对Loc_1
和Loc_2
的列进行理解zip
并计算每对Loc_1
和Loc_2
的测地距离:
places_data['Distance'] = [geodesic(x, y).miles for x, y in zip(places_data['Loc_1'], places_data['Loc_2'])]
@这是否回答了你的问题?如果答案是肯定的,你可以接受并投票表决。看看@EX_Tenn这是否回答了你的问题?如果答案是肯定的,你可以接受并投票表决。看看