Home > Mobile >  How to write multiple conditional statements for loc dataframe with operators
How to write multiple conditional statements for loc dataframe with operators

Time:08-18

I'm trying to create a loc dataframe with multiple conditional statements. The problem I am having is that when I use and 'and' operator for my second conditional statement, I get the error below.

As a side problem, is there any better way to seperate the conditional statements than with |.

My code:

df = df2.loc[(df2['Position'] == 0) | (df2['TABNo'] == 0 and df2['IsBarrierTrial'] == 'FALSE')]

Error with and operator:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

CodePudding user response:

Make sure they are correctly separated by parenthesis to group the logics and consider using bitwise operator:

df = df2.loc[(df2['Position'] == 0) | ((df2['TABNo'] == 0) & (df2['IsBarrierTrial'] == 'FALSE'))]
  • Related