pythonpandasdataframeboolean-logiclines-of-code

Selecting dataframe columns with boolen, rest have to be false


I try to filter a dataframe with a specific condition, but don't now how to get safe that all other columns have to be false.

A | B | C | D | E | F
True True False False False False
True False True False False True
True True True False False False

given this df i want to select every row where A is tru and B or C is True.

df.loc[(df.A == True) & ((df.B == True) or (df.C == True))]

or

df.query('A and (b or C) ')

and my resut would be

A | B | C | D | E | F
True True False False False False
True False True False False True
True False True False False False

but how can I get safe that all other columns that are not to mentioned (D,E,F) have to be False so that the result is

A | B | C | D | E | F
True True False False False False
True False True False False False

Solution

  • You can use another mask with columns.difference and any:

    m1 = df['A'] & (df['B']  | df['C'])
    m2 = ~df[df.columns.difference(['A', 'B', 'C'])].any(axis=1)
    df.loc[m1 & m2]
    

    Output:

          A     B      C      D      E      F
    0  True  True  False  False  False  False
    2  True  True   True  False  False  False