Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter for rows in pandas dataframe where values in a column are greater than x or NaN

I'm trying to figure out how to filter a pandas dataframe so that that the values in a certain column are either greater than a certain value, or are NaN. Lets say my dataframe looks like this:

df = pd.DataFrame({"col1":[1, 2, 3, 4], "col2": [4, 5, np.nan, 7]})

I've tried:

df = df[df["col2"] >= 5 | df["col2"] == np.nan]

and:

df = df[df["col2"] >= 5 | np.isnan(df["col2"])]

But the first causes an error, and the second excludes rows where the value is NaN. How can I get the result to be this:

pd.DataFrame({"col1":[2, 3, 4], "col2":[5, np.nan, 7]})
like image 264
AJ Bensman Avatar asked Oct 25 '25 17:10

AJ Bensman


1 Answers

Please Try

df[df.col2.isna()|df.col2.gt(4)]



  col1  col2
1     2   5.0
2     3   NaN
3     4   7.0
like image 174
wwnde Avatar answered Oct 28 '25 08:10

wwnde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!