How to select cells greater than a value in a multi-index Pandas dataframe? How to select cells greater than a value in a multi-index Pandas dataframe? python python

How to select cells greater than a value in a multi-index Pandas dataframe?


If what you are trying to do is to select only rows where any one column meets the condition , you can use DataFrame.any() along with axis=1 (to do row-wise grouping) . Example -

In [3]: dfOut[3]:   A  B  C0  1  2  31  3  4  52  3  1  4In [6]: df[(df <= 2).any(axis=1)]Out[6]:   A  B  C0  1  2  32  3  1  4

Alternatively, if you are trying for filtering rows where all columns meet the condition , use .all() inplace of .any() . Example of all -

In [8]: df = pd.DataFrame([[1,2,3],[3,4,5],[3,1,4],[1,2,1]],columns=['A','B','C'])In [9]: dfOut[9]:   A  B  C0  1  2  31  3  4  52  3  1  43  1  2  1In [11]: df[(df <= 2).all(axis=1)]Out[11]:   A  B  C3  1  2  1