【问题标题】:Python function or nested loop that does a distance calculation for each unique item in column对列中的每个唯一项进行距离计算的 Python 函数或嵌套循环
【发布时间】:2020-10-13 01:23:35
【问题描述】:

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

车号 ___ x 坐标 ___ y 坐标
1 _____________ 54 _____ 25
1 _____________ 57 _____ 26
1 _____________ 54 _____ 29
2 _____________ 52 _____ 24
2 _____________ 56 _____ 28
2 _____________ 57 _____ 29
3 _____________ 51 _____ 25
3 _____________ 54 _____ 26
3 _____________ 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新手,请原谅我的错误,我真的很困惑。

【问题讨论】:

  • 您是否使用数据框来存储这些值?
  • @Ozzy08 是的!我应该提到这一点,但是我使用的是数据框。
  • 我做到了。

标签: python loops nested distance nested-loops


【解决方案1】:
from pandas import DataFrame

# create data
data = DataFrame([
    (1, 54, 25),
    (1, 57, 26),
    (1, 54, 29),
    (2, 52, 24),
    (2, 56, 28),
    (2, 57, 29),
    (3, 51, 25),
    (3, 54, 26),
    (3, 59, 29),
], columns=['car_number', 'x_coord', 'y_coord'])


# calculate distances
data['distance'] = (
    (data['x_coord'] - data['x_coord'].shift()) ** 2 +
    (data['y_coord'] - data['y_coord'].shift()) ** 2
) ** 0.5

# ignore distances between points for different cars
data['same_car'] = data['car_number'] == data['car_number'].shift()
data['distance'] = data['distance'] * data['same_car']

# group distances by car and sum
distances = data.groupby('car_number')['distance'].sum().reset_index()

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      相关资源
      最近更新 更多