How to find the rows for dataframe mask

Question:

For example:

df = pd.DataFrame([[False, True],[False, False]], columns=['a', 'b'])

How to show only the rows (it will be only the first row in this case) instead of showing all rows?

Tried the code below but it shows all rows:

df == True
Asked By: weiming

||

Answers:

IIUC you are looking for rows where column a or b is True:

df[df['a'] | df['b']]

Output:

a       b
False   True
Answered By: Mattravel

You can do it like this:

df[df.any(axis=1)]
Answered By: Leonid Astrin

Do you want the rows with at least one True?

Then use any and boolean indexing:

df[df.any(axis=1)]

Output:

       a     b
0  False  True
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.