Test "if" with two conditions not working

Hello,
I am using a code which aims to identify in a database of manoeuvres those where two parameters each respect a condition simultaneously at at least one time during the manoeuvre.
The way it is written at the moment, it returns the manoeuvres which respect the first condition, but does not take into account the second condition.
Do you know how to modify the code so that both conditions are checked?
Thanks in advance!

The code is the following :

            if ((criteria1 < 10) & (criteria2 > 20)
            ).any():  
                man_list.loc[index, "Condition OK"] = True

Python uses the keyword and and not & for the AND operation

Edit: @steven.rumbalski pointed out that Pandas is used here and & is used.

I apologize for my mistake.

What database?

What are criteria1 and criteria2 before this code? Why should the combination have a .any method? What is man_list, and why does it have a .loc?

How do you know that the second condition is not being taken into account? Given a clear, specific input that you can show us: exactly what should happen when the code is given that input? What does happen instead, and how is that different?

Please take a step back and try to present the necessary context for the code, step by step. If you are using third-party libraries, make sure to mention which ones. Then try to create a minimal reproducible example.

Hello Erik,
Thanks for your reply. I don’t know exactly which variant of Python I am using, but if I use “and” instead of “&”, I get an error message :“The truth value of a Series is ambiguous”

“variants” of Python are not relevant here (there are different versions of the reference implementation, but the syntax changes are very minor, and other implementations implement the same syntax).

The result indicates that you are using the Pandas third-party library. (I guessed that it would be either Numpy or Pandas.) If you understood this, you should have said so, and given an appropriate explanation like what I described. (If you didn’t, then please try to find a different source to learn Python; whatever is teaching you, is clearly not properly explaining even basic concepts.) But we still need more information in order to have any chance of debugging the problem.

1 Like

The method calls indicate the OP is using a pandas.DataFrame or similar structure from another library. & in pandas is used to apply boolean & element-wise across two Series. It is used for combining the results of multiple conditions.

1 Like

Hello Steven,
Thanks for your reply.
I had help from a colleague who advised to introduce the variable “boolean_serie” to test the two criterias. And it works!
Have a good day

            boolean_serie = (criteria1 < 10) & (criteria2 > 20)            
                    
            if boolean_serie.any():
                man_list.loc[index, "Condition OK"] = True

This code

        boolean_serie = (criteria1 < 10) & (criteria2 > 20)            
                
        if boolean_serie.any():
            man_list.loc[index, "Condition OK"] = True

is not substantively different than your original code

        if ((criteria1 < 10) & (criteria2 > 20)).any():  
            man_list.loc[index, "Condition OK"] = True

If the first one fails, the second should also fail. There’s nothing magical about naming the result of (criteria1 < 10) & (criteria2 > 20) .

1 Like