如何在数据框架列上使用geopy vicenty distance?[英] How to use geopy vicenty distance over dataframe columns?

本文是小编为大家收集整理的关于如何在数据框架列上使用geopy vicenty distance?的处理方法,想解了如何在数据框架列上使用geopy vicenty distance?的问题怎么解决?如何在数据框架列上使用geopy vicenty distance?问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我有一个带有位置列的数据框

 deviceid                             location        
1102ADb75        [12.9404578177, 77.5548244743]

如何使用地质的Vicenty函数获得连续行之间的距离? 我尝试遵循代码

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

它返回以下错误 - typeError:__new __()最多进行4个参数(5给定)

编辑 - df是包含DeviceID和位置列的Pandas DataFrame,如上图所示 也

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

推荐答案

基于https://github.com/geopy/geopy/geopy/blob/ba/ba/ba509142dea4ee1ce45deb9ef226751faefc3b/geopy/geopy/distance.py.py#l301-301-301-301-301-301-304" rel= nonnorpollowellow <>您应该将两个元组传递到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

本文地址:https://www.itbaoku.cn/post/1728088.html