Skip to content
Advertisement

Select all row where all columns are equal to value

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

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

Once the test function is defined apply it to the dataframe along axis 1 (row by row):

JavaScript

Which returns a boolean series, showing for each row, whether the test passed or failed.

JavaScript

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
index A B C
7 0 0 0
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement