Geopy 中的距离计算
Distance calculation in Geopy
我在 Centos 6 上使用 Python 2.6.6。
我有一个 dataframe
是我从 pickle 文件中引入的。然后我想计算两点之间的距离。我尝试将每个点的 lat
和 long
组合成一个元组,然后使用 Geopy.great_circle
。然而回溯包括这个:
/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)
我的输入来自 Pandas DataFrame,它应该具有相同的长度(如果重要的话?)
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)
EDIT 'EdChum' 在上面的评论中有正确答案..
samp.apply(lambda x: vincenty((x['biz_lat'],x['biz_lon']), (x['id_lat'], x['id_lon'])).miles, axis=1)
我在 Centos 6 上使用 Python 2.6.6。
我有一个 dataframe
是我从 pickle 文件中引入的。然后我想计算两点之间的距离。我尝试将每个点的 lat
和 long
组合成一个元组,然后使用 Geopy.great_circle
。然而回溯包括这个:
/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)
我的输入来自 Pandas DataFrame,它应该具有相同的长度(如果重要的话?)
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)
EDIT 'EdChum' 在上面的评论中有正确答案..
samp.apply(lambda x: vincenty((x['biz_lat'],x['biz_lon']), (x['id_lat'], x['id_lon'])).miles, axis=1)