pythonpandasoperands

Separate conditional statements with | operand


I am trying to produce a dataframe with these two conditional statements. I am using the | operand or the 'or' operand to separate the conditional statements. There is not an issue with the conditional statements because when I run them separately they work fine. Is there any other way I can separate these conditional statements?

My code:

df = df2[(df2['TABNo'] == 0) & ~df2['IsBarrierTrial']] | df2[(df2['Position'] == 0)]
print(df)

Error:

TypeError: unsupported operand type(s) for |: 'float' and 'bool'

Solution

  • Reindenting your statement:

    df = (
        df2[
            (df2["TABNo"] == 0)
            & ~df2["IsBarrierTrial"]
        ]
        | df2[(df2["Position"] == 0)]
    )
    

    It looks like you're really looking for

    df = df2[
        (df2["TABNo"] == 0)
        & (~df2["IsBarrierTrial"])
        | (df2["Position"] == 0)
    ]
    

    to select a bunch of items instead of having that one nested df2 indexing.