How to store rows where at least one column satisfies a condition in Pandas

I have the following DF:

In [1]: import pandas as pd

In [2]: mydict = {'foo':[0, 0.3,5], 'bar':[1,0.55,0.1], 'qux': [0.3,4.1,4]}

In [3]: df = pd.DataFrame.from_dict(mydict, orient='index')

In [4]: df
Out[4]:
       0     1    2
qux  0.3  4.10  4.0
foo  0.0  0.30  5.0
bar  1.0  0.55  0.1

      

What I want to do is keep the rows if at least one of the columns is> 2. The end result looks like this:

       0     1    2
qux  0.3  4.10  4.0
foo  0.0  0.30  5.0

      

How do I do this in Pandas?

+3


source to share


1 answer


In [201]: df.loc[(df > 2).any(axis=1)]
Out[201]: 
       0    1  2
qux  0.3  4.1  4
foo  0.0  0.3  5

      



+7


source







All Articles