โค้ดเร่งความเร็วสำหรับแบ่งหนึ่งขอบออกเป็นหลายขอบผ่านแพนด้า

import pandas as pd

cols = ["PRODUCT_ID", "X", "Z", "AISLE", "SEQUENCE", "PATH_ID", "DIRECTION", "WAY", 
        "X_START_PATH", "Z_START_PATH", "X_END_PATH", "Z_END_PATH", "OTHER1","OTHER2", "OTHER3"]

test_df = pd.DataFrame([[100762, 30, 20, "A", 335, "PATH_1", "V", "DOWN", 30, 1000, 30, 1, "N", "N", True],
 [986520, 30, 15, "A", 338, "PATH_1", "V", "DOWN", 30, 1000, 30, 1, "N", "N", True],
 [647300, 135, 87, "B", 100, "PATH_2", "V", "UP", 135, 1200, 135, 2, "N", "N", True],
 [649310, 135, 90, "B", 110, "PATH_2", "V", "UP", 135, 1200, 135, 2, "N", "N", True],
 [117340, 135, 99, "B", 112, "PATH_2", "V", "UP", 135, 1200, 135, 2, "N", "N", True]], 
            columns=cols)

test_df.head()
>>
   PRODUCT_ID    X   Z AISLE  SEQUENCE PATH_ID DIRECTION   WAY  X_START_PATH  Z_START_PATH  X_END_PATH  Z_END_PATH OTHER1 OTHER2  OTHER3
0      100762   30  20     A       335  PATH_1         V  DOWN            30          1000          30           1      N      N    True
1      986520   30  15     A       338  PATH_1         V  DOWN            30          1000          30           1      N      N    True
2      647300  135  87     B       100  PATH_2         V    UP           135          1200         135           2      N      N    True
3      649310  135  90     B       110  PATH_2         V    UP           135          1200         135           2      N      N    True
4      117340  135  99     B       112  PATH_2         V    UP           135          1200         135           2      N      N    True
def SLOW_create_path_edges(row):
    xs = row["X"].values
    zs = row["Z"].values
    ss = row["SEQUENCE"].values
    
    # columns of the output dataframe
    output_columns = ['PRODUCT_ID', 'X1', 'Z1', 'X2', 'Z2', 'SEQ_1', 'SEQ_2', 'OTHER1', 'OTHER2', 'OTHER3', 'DIRECTION', 'WAY']

    # x and z of the starting point of the path
    start = row[['X_START_PATH', 'Z_START_PATH'] + ['DIRECTION', 'WAY', 'OTHER1', 'OTHER2', 'OTHER3']].values[0]
    # x and z of the final point of the path
    end = row[['X_END_PATH', 'Z_END_PATH'] + ['DIRECTION', 'WAY', 'OTHER1', 'OTHER2', 'OTHER3']].values[0]
    
    # (the first and last point of the path have sequence = NAN)
    
    # from the start of the path to the first article
    first = [
        [row.name, start[0], start[1], xs[0], zs[0], np.NaN, ss[0], start[2], start[3], start[4], start[5], start[6]]]
    # from the first article to the second one, from the second to the third, ... 
    intermediate = [
        [row.name, xs[i], zs[i], xs[i + 1], zs[i + 1], ss[i], ss[i + 1], start[2], start[3], start[4], start[5],
         start[6]] for i in range(len(xs) - 1)]
    # from the last article to the end of the path
    last = [[row.name, xs[-1], zs[-1], end[0], end[1], ss[-1], np.NaN, end[2], end[3], end[4], end[5], end[6]]]

    return pd.DataFrame(first + intermediate + last,
                        columns=output_columns)