Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep rows where at least one column satisfy a condition in Pandas

Tags:

python

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 to keep rows if at least one of the column is > 2. The final output looks like this:

       0     1    2
qux  0.3  4.10  4.0
foo  0.0  0.30  5.0

What's the way to do it in Pandas?

like image 224
pdubois Avatar asked Jan 13 '15 01:01

pdubois


People also ask

How do I select rows from a DataFrame based on multiple column values?

You can select the Rows from Pandas DataFrame based on column values or based on multiple conditions either using DataFrame. loc[] attribute, DataFrame. query() or DataFrame. apply() method to use lambda function.


1 Answers

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
like image 76
unutbu Avatar answered Sep 20 '22 22:09

unutbu