Does this achieve what you're after? There might be more elegant ways to achieve the same. I've assumed you have a DataFrame
df
with your table.
df_shift = df.shift(1) # shift database with 1 row
same_idx = df['nn_id'] == df_shift['nn_id']
# get column positions for columns of interest
col1_pos = df.columns.get_loc('slice-0010-EDSR_x2_X ')
col2_pos = df.columns.get_loc('slice-0010-EDSR_x2_Y')
nn_idx_pos = df.columns.get_loc('nn_id')
my_dict = {} # define empty dict to store your results.
for i in np.where(same_idx)[0]: # for each row where the nn_idx value is the same
# define the value that you're after
my_value = [(df.iloc[i-1, col1_pos], df.iloc[i-1, col2_pos]),
(df.iloc[i, col1_pos], df.iloc[i, col2_pos])]
# and add element to dictionary
my_dict[df.iloc[i, nn_idx_pos]] = my_value
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…