-1

I am trying to find a combination where it will go through the data to find matched variables for any value from the list First_row is found, any value from the list Second_row and any value from the list Third_row is found.

import pandas as pd
import numpy as np
import csv
df= pd.read_csv ("data.csv")

    
b2=1 #assigning variables
c2=2
d2=3
b3=4
c3=5
d3=6
b4=7
c4=8
d4=9

First_row = [b2, c2, d2] #list for first row
Second_row = [b3, c3, d3] #list for second row
Third_row = [b4, c4, d4] #list for third row

for n in range(len(df)): # data
    for i in range(len(First_row)):
        if df.loc[n].isin(First_row[i]).any(): # Found a value from First_row
            for i in range(len(Second_row)):
                if df.loc[n+1].isin(Second_row[i]).any(): ## Found a value from Second_row
                    for i in range(len(Third_row)):
                        if df.loc[n+2].isin(Third_row[i]).any(): # Found a value from Third_row
                            print(First_row[i],Second_row[i],Third_row[i]) # print the variables

If the first row has any one of [b2, c2, d2], second row has any one of [b3, c3, d3] and third row has any one of [b4, c4, d4] then it will print the variables, not the assigned value.

It will search as b2,b3,b4, -> b2,b3,c4, -> b2,b3,d4, -> b2,c3,b4, etc. and print the variables (not the assigned value) when found and continue. I am trying to run it but it's showing "only list-like objects are allowed to be passed to isin()"

1 Answer 1

2

It looks like you're trying to pass a specific value from your row into the isin() method.

If you try using

if df.loc[n] in First_row 

that should get you past the first step.

In any case the issue is definitely that you're giving isin() a value and not a list. Rather than embedding these loops super deep, could you turn it into separate sections?

I expect you'll run into trouble with i being reused like this as well, so iterating through each row separately will work better and you can just save i to a unique index variable.

As it is, your final printout will use the final value of i and print out every value with whatever index df.loc[n] is present in the third_row.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.