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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With