【发布时间】:2021-11-23 06:47:09
【问题描述】:
我有一个包含经度和纬度的电台信息的数据框,如下所示:
start_lat start_lng end_lat end_lng
41.877726 -87.654787 41.888716 -87.644448
41.930000 -87.700000 41.910000 -87.700000
41.910000 -87.690000 41.930000 -87.700000
同样明智。
我想根据这些信息创建一个距离列,其中距离可以以公里或英里为单位表示这些起点和终点之间的距离。
(正如下面的亲属所分享的,当我尝试实现 SO 答案时,我遇到了错误。)
from math import sin, cos, sqrt, atan2
dlon = data.end_lng - data.start_lng
dlat = data.end_lat - data.start_lat
a = ((sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2)
c = 2 * atan2(sqrt(a), sqrt(1-a))
data['distance'] = R * c
TypeError Traceback (most recent call last)
<ipython-input-8-a8f8b698a81b> in <module>()
2 dlon = data.end_lng - data.start_lng
3 dlat = data.end_lat - data.start_lat
----> 4 a = ((sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2).apply(lambda x: float(x))
5 c = 2 * atan2(sqrt(a), sqrt(1-a))
6 data['distance'] = R * c
/usr/local/lib/python3.7/dist-packages/pandas/core/series.py in wrapper(self)
127 if len(self) == 1:
128 return converter(self.iloc[0])
--> 129 raise TypeError(f"cannot convert the series to {converter}")
130
131 wrapper.__name__ = f"__{converter.__name__}__"
TypeError: cannot convert the series to <class 'float'>
如何解决?
【问题讨论】:
-
感谢您的建议!我试过了,但我认为我无法正确使用它:因为我获得了熊猫系列。你能帮我解决一下吗?
标签: python-3.x pandas dataframe distance latitude-longitude