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.
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
?
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?
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
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"):
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)
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.