Python:如何将给定距离内的点组合在一起?

时间:2016-04-29 10:05:37

标签: python pandas group-by distance

我有一个数据框,其中包含不同点(纬度/经度)之间用户的起始目的地旅行。我们有\\\

Origin_X, Origin_Y

我想将原点和目的地半径均为Destination_X, Destination_Y的所有df: Trip Origin_X Origin_Y Destination_X Destination_Y 1 -33.55682 -70.78614 -33.44007 -70.6552 2 -33.49097 -70.77741 -33.48908 -70.76263 3 -33.37108 -70.6711 -33.73425 -70.76278 组合在一起。如果两个行程与原点的距离和目的地的距离为Trip,则可以对两个行程进行分组。为了计算两个坐标之间的距离,我正在使用1km函数。

d<=1km

2 个答案:

答案 0 :(得分:2)

以下是如何做到这一点:

import pandas as pd
from math import *

def haversine(row):
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians 
    lon1 = row[1]
    lat1 = row[2]
    lon2 = row[3]
    lat2 = row[4]
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles
    return c * r

#Copy the trip details provided by in this question

df = pd.read_clipboard()
df['dist'] = df.apply(haversine, axis=1)

print df

   Trip  Origin_X  Origin_Y  Destination_X  Destination_Y       dist
0     1 -33.55682 -70.78614      -33.44007      -70.65520  15.177680
1     2 -33.49097 -70.77741      -33.48908      -70.76263   1.644918
2     3 -33.37108 -70.67110      -33.73425      -70.76278  16.785898
#To group
dfg = df.groupby(df['dist'] < 1)

#Just to select all the trips that are less than 2 radius
df[df['dist']<2]
   Trip  Origin_X  Origin_Y  Destination_X  Destination_Y      dist
1     2 -33.49097 -70.77741      -33.48908      -70.76263  1.644918

答案 1 :(得分:0)

您可以迭代每个点,计算到所有其他点的距离,然后检查距离是否小于或等于1km并将其添加到字典中,其中键是原点,值是所有其他点的数组关闭点...