【问题标题】:How to calculate the distance between latitudes and longitudes of two stations in a pandas dataframe如何计算熊猫数据框中两个站点的经纬度之间的距离
【发布时间】: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


【解决方案1】:

您需要对每一行进行计算,一种方法是使用 itterows(不保证距离计算本身):

def get_distance(row, R = 6371): #km
    dlon = row[1]['end_lng'] - row[1]['start_lng']
    dlat = row[1]['end_lat'] - row[1]['start_lat']

    a = ((sin(dlat/2))**2 + cos(row[1]['start_lat']) * cos(row[1]['end_lat']) * (sin(dlon/2))**2)
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    return R * c

data['distance'] = [get_distance(row) for row in data.iterrows()] 

【讨论】:

    猜你喜欢
    • 2022-11-03
    • 1970-01-01
    • 2012-10-13
    • 2019-08-23
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 2018-06-11
    • 2011-04-11
    相关资源
    最近更新 更多