PR102012
PR102012

Reputation: 878

Distance calculation in Geopy

I am using Python 2.6.6 on Centos 6.
I have a dataframe that I am bringing in from a pickle file. Then I'd like to calculate the distance between 2 points. I have tried to combine the lat and long for each point into a tuple and then used Geopy.great_circle. However the traceback includes this:

/opt/rh/python27/root/usr/lib/python2.7/site-packages/geopy/point.pyc in __new__(cls, latitude, longitude, altitude)
127                     )
128                 else:
--> 129                     return cls.from_sequence(seq)
130 
131         latitude = float(latitude or 0.0)

/opt/rh/python27/root/usr/lib/python2.7/site-packages/geopy/point.pyc in from_sequence(cls, seq)
351         """
352         args = tuple(islice(seq, 4))
--> 353         return cls(*args)
354 
355     @classmethod

TypeError: __new__() takes at most 4 arguments (5 given)

My input is from a Pandas DataFrame that should be of the same length (if that matters?)

import numpy as np
from geopy.distance import vincenty
import geopy
import pandas as pd

distances_frame = pickle.load(open("distances.p", "rb"))
samp = distances_frame.sample(n=50)
samp = samp.dropna()
point1 = tuple(zip(samp['biz_lat'],samp['biz_lon']))
point2 = tuple(zip(samp['id_lat'],samp['id_lon']))
dist= (vincenty(point1,point2).miles)

Upvotes: 0

Views: 2982

Answers (1)

PR102012
PR102012

Reputation: 878

EDIT 'EdChum' has the correct answer in comments above..

samp.apply(lambda x: vincenty((x['biz_lat'],x['biz_lon']), (x['id_lat'],   x['id_lon'])).miles, axis=1)

Upvotes: 2

Related Questions