I have a pandas dataframe and I need to select the rows where all the columns are equal to 0.00. How can I do that?
Advertisement
Answer
First, we’ll setup some example data to work on – here there are 3 columns, but it could be more, or fewer in your example.
JavaScript
x
16
16
1
import pandas as pd
2
3
data = [
4
{"A": 1, "B": 1, "C" : 1},
5
{"A": 0, "B": 1, "C" : 1},
6
{"A": 1, "B": 0, "C" : 1},
7
{"A": 0, "B": 0, "C" : 1},
8
{"A": 1, "B": 1, "C" : 0},
9
{"A": 0, "B": 1, "C" : 0},
10
{"A": 1, "B": 0, "C" : 0},
11
{"A": 0, "B": 0, "C" : 0}
12
13
]
14
15
df = pd.DataFrame(data)
16
And define a function that accepts a row from a dataframe, and performs some test on it – in this case, check that all values in the row are equal to zero.
JavaScript
1
6
1
def all_zero(row):
2
if all([v==0 for v in row.values]):
3
return True
4
else:
5
return False
6
Once the test function is defined apply it to the dataframe along axis 1 (row by row):
JavaScript
1
2
1
df.apply(all_zero, axis=1)
2
Which returns a boolean series, showing for each row, whether the test passed or failed.
JavaScript
1
10
10
1
0 False
2
1 False
3
2 False
4
3 False
5
4 False
6
5 False
7
6 False
8
7 True
9
dtype: bool
10
And you can use this boolean series as an index-level selector (or mask, as some people call it), to retrieve only those rows that return True
.
JavaScript
1
2
1
df[df.apply(all_zero, axis=1)]
2
index | A | B | C |
---|---|---|---|
7 | 0 | 0 | 0 |