Nested loop over dataframe rows

Question:

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.

Asked By: mahmood

||

Answers:

For your specific problem i guess df.iterrows() may not be optimal…
You should consider just iterating using indicies and df.iloc.

for o_index in range(len(df)):
    o_row = df.iloc[o_index]
    L1 = o_row['Home']
    L2 = o_row['Block']
    for i_index in range(o_index + 1, len(df)):
        i_row = df.iloc[i_index]
        L3 = i_row['Home']
        L4 = i_row['Block']

Otherwise if you really want to use df.iterrows() this solution should work:

for o_index, o_row in df.iterrows():
    L1 = o_row['Home']
    L2 = o_row['Block']
    for i_index, i_row in df.iloc[o_index+1:].iterrows():
        L3 = i_row['Home']
        L4 = i_row['Block']
Answered By: Matteo Zanoni

No need for iteration with testing, what you want to do is to compute the combinations of Block for the same Home. So just do that:

from itertools import combinations

out = [abs(L2-L4) for _, g in df.groupby('Home')
       for L2, L4 in combinations(g['Block'], r=2)]

Output:

[100, 100, 200]
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.