Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Or Statements
#1
Hello all

I have trying to write some code that loops through all values in a dataframe and checks if the value is equal to 5 values that i am interested in, the code i have is as follows:-

if (df.iloc(B,2) != "A") or (df.iloc(B,2) != "14-C") or (df.iloc(B,2) != "CAT") or (df.iloc(B,2) != "Tony") or (df.iloc(B,2) != "USE"):    
I keep getting an error which states:-

TypeError: __call__() takes from 1 to 2 positional arguments but 3 were given
I know there maybe a better way of coding this but i just want to code it the way it makes sense in my head.

Can anyone help at all?

Thank you.
Reply
#2
would you please share relevant code as well as full traceback you get.
not to mention that your statement does not make sense - it will be always True
spam = ['foo', 'bar', 'woo']

for eggs in spam:
   if eggs != 'foo' or eggs != 'bar':
      print(eggs)
maybe you want and?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi Buran

The data frame i have is over 4000 items long and i am checking all of the rows in one of the columns of the data frame.

If the row i am looking at is equal to "A", "14-C", "CAT", "Tony" or "USE" then i want to take no action.
If it is not equal to any of these values then i want o drop the row.

This is what i am trying to accomplish.

Can you help?
Reply
#4
you have 2 problems - one is the error and we need the traceback and the code (ideal would be minimal reproducible example that replicate the error).
so show us your code and we can work from there.
My example demonstrate that your condition will always be True
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Even though Buran has shown that the logic doesn't make sense, you could try separating the function call and the logic and see what happens to the error.

iloc_data = df.iloc(B,2)
if (iloc_data != "A") or (iloc_data != "14-C") or (iloc_data != "CAT") or (iloc_data != "Tony") or (iloc_data != "USE"):    
Reply
#6
here is example how to filter df keeping only certain values
spam = ['foo', 'bar', 'woo']

import pandas as pd
df = pd.DataFrame(spam, columns=['spam'])
print(df)
filtered_df = df[df['spam'].isin(['foo', 'bar'])]
print(filtered_df)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Hi Buran

I managed to get it fixed i needed to use [] for the data frame and replace or with and as you suggested.

I know this will sound a little strange but in my head OR would be the correct statement to use instead of AND because i am checking if a single value in my dataframe is equal to A OR Tony OR C etc and if it is then i would print it out for example.

To use AND doesn't seem correct i.e. if i am checking if a single value in my dataframe is equal to A AND Tony AND C - to me that would imply that the value in my data frame was "A Tony C".

So i guess i am trying to ask is what is the difference between And & Or and when would you use them.

Thanks.
Reply
#8
It looks like you are missing basics of boolean algebra
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Maybe that is better (i.e. more practical one) resource https://realpython.com/python-operators-...-operators
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple Loop Statements in a Variable Dexty 1 1,175 May-23-2022, 08:53 AM
Last Post: bowlofred
  SyntaxError: multiple statements found while compiling a single statement Kayode_Odeyinka 1 2,935 Mar-12-2020, 05:50 PM
Last Post: micseydel
  Multiple IF statements kamacaziYakuta 1 1,991 May-04-2019, 05:46 PM
Last Post: MvGulik
  optimize choices in multiple if ---: statements Pedroski55 2 2,856 Dec-25-2018, 05:06 AM
Last Post: Pedroski55
  SyntaxError: multiple statements found while compiling a single statement DragonG 1 5,394 Nov-26-2018, 05:33 AM
Last Post: Larz60+
  How to reliably split string containing multiple sqlite statements? shanepy 2 2,557 Nov-21-2018, 12:07 AM
Last Post: shanepy
  How to execute a string with multiple sqlite statements and print results? shanepy 0 2,247 Nov-20-2018, 10:19 PM
Last Post: shanepy
  Multiple "return" statements or "result" variable? WolfWayfarer 7 7,674 Jul-25-2018, 07:51 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020