如何在数据帧列上使用geopy vicenty distance?

时间:2015-06-21 20:59:43

标签: python pandas dataframe geopy

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

 deviceid                             location        
1102ADb75        [12.9404578177, 77.5548244743]

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

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

它返回以下错误 - TypeError:__ new __()最多需要4个参数(给定5个)

编辑 - 其中df是包含deviceId&amp ;;的Pandas数据帧。位置列如上所示 还

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

1 个答案:

答案 0 :(得分:2)

基于geopy's 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