How to use geopy vicenty distance over dataframe columns?

前提是你 提交于 2019-12-10 19:18:29

问题


I have a dataframe with location column which contains lat,long location as follows

 deviceid                             location        
1102ADb75        [12.9404578177, 77.5548244743]

How to get the distance between consecutive rows using geopy's vicenty function? I tried following code

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

It returns following error - TypeError: __new__() takes at most 4 arguments (5 given)

EDIT - where df is a Pandas dataframe containing deviceId & Location columns as shown above Also

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

回答1:


Based on geopy's github you should pass two tuples to the vincenty function:

    >>> 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

EDIT:

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://stackoverflow.com/questions/30969282/how-to-use-geopy-vicenty-distance-over-dataframe-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!