首页 > 解决方案 > 对列中的每个唯一项进行距离计算的 Python 函数或嵌套循环

问题描述

基本上可以说我有 3 辆车和一堆 x/y 坐标,如下所示:

车号 ___ x coor ___ y coor
1 _____________ 54 _____ 25
1 _____________ 57 _____ 26
1 _____________ 54 _____ 29
2 _____________ 52 _____ 24
2 _____________ 56 _____ 28
2 _____________ 57 _____ 29
3 _____________ 51 _____ 25
3 _____________ 3 _____________ 54 _____
2 59 _____ 29

我需要我的代码做的是从坐标计算每辆车的位移或行驶距离,输出显示类似

汽车 __ 排量
1 ________ 9
2 ________ 5
3 ________ 7

我目前拥有的内容如下,绝对行不通

displacement  = 0
for (car number, x coor, y coor) in coorset:
    for i in car number:
        displacement(i) = displacement  + (df[coor x] **2 + df[coor y] **2)**.5
        print (displacement)
        print(car number)

我是python的新手,所以请原谅我的错误,我真的很困惑。

标签: pythonloopsnesteddistancenested-loops

解决方案


这应该有效。我取了与当前车号对应的数据帧的一部分,对其进行了修改以包含位移,然后将其替换为原始数据帧。

data["displacement"] = 0

def distance_x(df, i):
        return (df.iloc[i, 1] - df.iloc[i + 1, 1]) ** 2

def distance_y(df, i):
    return (df.iloc[i, 2] - df.iloc[i + 1, 2]) ** 2

def total_displacement(df):
    cars = df["car_number"].unique()
    for car_num in cars:
        df_sel = df[df["car_number"] == car_num].copy()
        for i in range(len(df_sel) - 1):
            distance = (distance_x(df_sel, i) + distance_y(df_sel, i)) ** (1/2)
            df_sel.iloc[i + 1, 3] = distance + df_sel.iloc[i, 3]
        df[df["car_number"] == df_sel.iloc[0,0]] = df_sel    
    return df
    
total_displacement(data)
print(data)

 car_number  x_coord  y_coord  displacement
0         1.0     54.0     25.0      0.000000
1         1.0     57.0     26.0      3.162278
2         1.0     54.0     29.0      7.404918
3         2.0     52.0     24.0      0.000000
4         2.0     56.0     28.0      5.656854
5         2.0     57.0     29.0      7.071068
6         3.0     51.0     25.0      0.000000
7         3.0     54.0     26.0      3.162278
8         3.0     59.0     29.0      8.993230

推荐阅读