Try 1:
df[ df > 1.0 ]
: this returned all cells in NAN
.
Try2:
df.loc[ df > 1.0 ]
: this returned KeyError: 0
df[df['A']> 1.0]
: this works - But I want to apply the filter condition to all columns.
To select Pandas rows with column values greater than or smaller than specific value, we use operators like > , <= , >= while creating masks or queries. This results in DataFrame with values of Sales greater than or equal to 300 .
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]: df
Out[3]:
A B C
0 1 2 3
1 3 4 5
2 3 1 4
In [6]: df[(df <= 2).any(axis=1)]
Out[6]:
A B C
0 1 2 3
2 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]: df
Out[9]:
A B C
0 1 2 3
1 3 4 5
2 3 1 4
3 1 2 1
In [11]: df[(df <= 2).all(axis=1)]
Out[11]:
A B C
3 1 2 1
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