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
Answers:
IIUC you are looking for rows where column a
or b
is True
:
df[df['a'] | df['b']]
Output:
a b
False True
You can do it like this:
df[df.any(axis=1)]
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
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
IIUC you are looking for rows where column a
or b
is True
:
df[df['a'] | df['b']]
Output:
a b
False True
You can do it like this:
df[df.any(axis=1)]
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