I would like to perform a nested loop over a dataframe rows, considering the fact the inner loop starts from outer_row + 1
. If I use
for o_index, o_row in df.iterrows():
L1 = o_row['Home']
L2 = o_row['Block']
for i_index, i_row in df.iterrows():
L3 = i_row['Home']
L4 = i_row['Block']
As you can see, in the first iteration, i_index is the same as o_index. However, I want o_index to be 0 and i_index to be 1. How can I do that?
Example: Assume a dataframe like this:
Cycle Home Block
0 100 1 400
1 130 1 500
2 200 2 200
3 300 1 300
4 350 3 100
The iterations should be in this order:
0 -> 1, 2, 3, 4
1 -> 2, 3, 4
2 -> 3, 4
3 -> 4
4 -> nothing
In each inner iteration, I will then compare L1 and L3 and if they are equal, then abs(L2-L4) is calculated and pushed in a list.