-4
 pathway=(list(set(genes.iloc[0]).intersection(genes.iloc[i+1]))),(list(set(genes.iloc1).intersection(genes.iloc[i+2]))),(list(set(genes.iloc[2]).intersection(genes.iloc[i+3]))),(list(set(genes.iloc[3]).intersection(genes.iloc[i+4])

Instead of writing 0,1,2 to specify indeces, Is there any way to loop through the index?

I have 90 columns in genes dataframe, so kindly help me to make it simple.

Example: genes =

enter image description here

I want to compare 1st row with 2,3,4,5,6,7,8. Then 2nd row with 3,4,5,6,7,8 and so on till 7th row to 8.

I have tried this code,

for i in range(len(keys)):
    for k in range (0,8):
        for h in range (1,9):
            pathway1=(list(set(genes.iloc[k]).intersection(genes.iloc[i+h])))
            print(pathway1)

But its give me only 1st row with 2,3,4,5,6,7,8 comparison not others

Timus
  • 10,974
  • 5
  • 14
  • 28
Rutuja
  • 1
  • 2
  • The first code block isn't valid Python: There are not enough closing brackets. Should that be a tuple/list? Please clarify. – Timus Oct 28 '22 at 11:21
  • Please [do not post images of data](https://stackoverflow.com/help/how-to-ask), add the information as text (within code fences etc.) instead. Add a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) (also look [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) that includes the expected output. – Timus Oct 28 '22 at 11:22
  • It should be list – Rutuja Oct 28 '22 at 11:38
  • I want these comparison for finding common genes: 1 vs 2 1 vs 3 1 vs 4 1 vs 5 1 vs 6 1 vs 7 1 vs 8 2 vs 3 2 vs 4 2 vs 5 2 vs 6 2 vs 7 2 vs 8 3 vs 4 3 vs 5 3 vs 6 3 vs 7 3 vs 8 4 vs 5 4 vs 6 4 vs 7 4 vs 8 5 vs 6 5 vs 7 5 vs 8 6 vs 7 6 vs 8 7 vs 8 – Rutuja Oct 29 '22 at 08:59
  • Again: Provide a MRE (see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/14311263)). It's not that hard. – Timus Oct 29 '22 at 10:53
  • Also: Why is your question mirrored [here](https://stackoverflow.com/questions/74232998/for-loop-in-intersection-python), word by word? – Timus Oct 29 '22 at 10:53

1 Answers1

2
for j in [0,1,2,3]:
    pathway=(list(set(genes.iloc[j]).intersection(genes.iloc[i+j+1])))
    #do something with pathway

Take a look here for more info on loops

  • for i in range(len(keys)): for j in [0,8]: pathway[j]=(list(set(genes.iloc[j]).intersection(genes.iloc[i+j+1]))) print(pathway[j]) This code giving me only last comparison – Rutuja Oct 28 '22 at 11:15