将数据框中的值与数据框中的所有其他值进行比较

时间:2019-03-20 17:00:52

标签: python pandas loops dataframe

x1 = 37.1589
y1 = -98.2656
x2 = 37.2017
y2 = -98.2601

def haversine(y1, x1, y2, x2):
    R = 6372.8
    dlat = math.radians(x2 – x1)
    dlon = math.radians(y2 – y1)
    lat1 = math.radians(x1)
    lat2 = math.radians(x2)

    a = math.sin(dlat2/2)**2 + math.cos(lat1)*math.cos(lat2)*math.sin(dlon/2)**2
    c = 2*math.asin(math.sqrt(a))

    return R * c

def midpoint(x1, y1, x2, y2):
    lat1 = math.radians(x1)
    lon1 = math.radians(y1)
    lat2 = math.radians(x2)
    lon2 = math.radians(y2)

    bx = math.cos(lat2) * math.cos(lon2 – lon1)
    by = math.cos(lat2) * math.sin(lon2 – lon1)
    lat3 = math.atan2(math.sin(lat1) + math.sin(lat2).math.sqrt((math.com(lat1) + bx)**2) + by**2)
    lon3 = lon1 + math.atan2(by, math.com(lat1) + bx)

    return [round(math.degrees(lat3), 4], round(math.degrees(lon3), 4)]

if haversine(y1, x1, y2, x2) < 5:
    mid = midpoint(x1, y1, x2, y2)
    print(mid)
else:
    print(‘Points are greater than 5km apart’)

样本数据帧-lat是'x'值,lon是'y'值。

df = pd.DataFrame()
df[‘lat’] = [37.1589, 37.2017, 37.2254, 37.1127]
df[‘lon’] = [-98.2656, -98.2601, -98.2597, -98.2701]

我想将每对坐标与所有其他坐标进行比较: 37.1589 -98.2656对抗另外三对; 37.2017 -98.2601对其余两对比赛; 和37.2254 -98.2597对着最后一对

预期的输出-当它们之间的距离小于5公里时,请运行中点功能并将该坐标返回到df ['midpoint']列。

我已经检查了函数中公式的计算,但是我不知道如何遍历这样的数据框。任何帮助将不胜感激。

0 个答案:

没有答案