Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested loop over dataframe rows

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     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.

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading