5

我有一个带有位置列的数据框,其中包含经纬度位置,如下所示

 deviceid                             location        
1102ADb75        [12.9404578177, 77.5548244743]

如何使用 geopy 的 vicenty 函数获取连续行之间的距离?我尝试了以下代码

from geopy.distance import vincenty 
vincenty(df['location'].shift(-1), df['location']).miles

它返回以下错误 - TypeError: __new__() 最多接受 4 个参数(给定 5 个)

编辑 - 其中 df 是包含 deviceId 和 Location 列的 Pandas 数据框,如上所示

print type(df)
class 'pandas.core.frame.DataFrame'
4

1 回答 1

3

基于geopy的github,您应该将两个元组传递给vincenty函数:

    >>> from geopy.distance import vincenty
    >>> point_a = (41.49008, -71.312796)
    >>> point_b = (41.499498, -81.695391)
    >>> print(vincenty(point_a, point_b).miles)
    538.3904451566326

编辑

import pandas as pd
from geopy.distance import vincenty

data = [[101, [41.49008, -71.312796]],
        [202, [41.499498, -81.695391]]]
df = pd.DataFrame(data=data, columns=['deviceid', 'location'])

print df
>>>    deviceid                 location
>>> 0       101   [41.49008, -71.312796]
>>> 1       202  [41.499498, -81.695391]

print vincenty(df['location'][0], df['location'][1]).miles
>>> 538.390445157
于 2015-06-21T21:11:18.513 回答