如何将数据帧中的数据调用到 Haversine 函数中

How to call data from a dataframe into Haversine function

我有一个名为 lat_long 的数据框,其中包含一些位置的纬度和经度。我想找出以下每个位置之间的区别。当我使用示例 haversine 函数时,出现错误。 KeyError: ('1', u'occurred at index 0').

    1         2
0  -6.081689  145.391881
1  -5.207083  145.788700
2  -5.826789  144.295861
3  -6.569828  146.726242
4  -9.443383  147.220050

def haversine(row):
    lon1 = lat_long['1']
    lat1 = lat_long['2']
    lon2 = row['1']
    lat2 = row['2']
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * arcsin(sqrt(a)) 
    km = 6367 * c
    return km

lat_long['distance'] = lat_long.apply(lambda row: haversine(row), axis=1)
lat_long

尝试 this solution:

def haversine_np(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)

    All args must be of equal length.    

    """
    lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = np.sin(dlat/2.0)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2.0)**2

    c = 2 * np.arcsin(np.sqrt(a))
    km = 6367 * c
    return km

演示:

In [17]: df
Out[17]:
        lat         lon
0 -6.081689  145.391881
1 -5.207083  145.788700
2 -5.826789  144.295861
3 -6.569828  146.726242
4 -9.443383  147.220050

In [18]: df['dist'] = \
    ...:     haversine_np(df.lon.shift(), df.lat.shift(), df.ix[1:, 'lon'], df.ix[1:, 'lat'])

In [19]: df
Out[19]:
        lat         lon        dist
0 -6.081689  145.391881         NaN
1 -5.207083  145.788700  106.638117
2 -5.826789  144.295861  178.907364
3 -6.569828  146.726242  280.904983
4 -9.443383  147.220050  323.913612