How to use geopy vicenty distance over dataframe columns?

2.3k views Asked by At

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

There are 1 answers

1
Dušan Maďar On

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