Checking only when it is .tolist()

80 Views Asked by At

I have to put a column in a list to be able to find matches.

I am trying to create a table "mytable" that gets data from going through each row of another table "table_main" and if it meets the condition, add rows to mytable. I have other tables as well, e.g. "table3" all DataFrame.

I wrote it like:

mytable = {'column1' :[] , 'column2':[] , 'column3': [] , 'column4':[]}

for index, row in table_main.itterrows():
   if row['apple'] not in table3['codes'] and row['apple']!= "07":
       mytable['column1'].append("2024")
       mytable['column2'].append(str(row['number']))
       mytable['column3'].append(str(row['apple']))
       mytable['column4'].append("first condition")
 

but it can't understand row['apple'] values exist in table3['codes']. I tried to put str( before them still not working. Only works when I put table3['codes'].tolist() but I don't want to do it. Does anyone know why?

2

There are 2 best solutions below

7
TheHungryCub On BEST ANSWER

Try this :

mytable = {'column1': [], 'column2': [], 'column3': [], 'column4': []}

for index, row in table_main.iterrows():
    if (row['apple'] not in table3['codes'].values) and (row['apple'] != "07"):
        mytable['column1'].append("2024")
        mytable['column2'].append(str(row['number']))
        mytable['column3'].append(str(row['apple']))
        mytable['column4'].append("first condition")

Updated code For Matching row:

mytable = {'column1': [], 'column2': [], 'column3': [], 'column4': []}

for index, row in table_main.iterrows():
    # Your condition to find matching rows
    if row['apple'] in table3['codes'].values and row['apple'] != "07":
        mytable['column1'].append("2024")
        mytable['column2'].append(str(row['number']))
        mytable['column3'].append(str(row['apple']))
        mytable['column4'].append("another condition")
0
sahasrara62 On

table3['codes'] is a column data type, so in order to find element in this data type so you need to use method which look of element which this data type support or transform the values to another datatype where you can use in to check values

So make the changes

if row['apple'] not in table3['codes'] and row['apple']!= "07":

to

if row['apple'] not in table3['codes'].values and row['apple']!= "07":