Cactus Philosopher
Cactus Philosopher

Reputation: 864

Filtering rows in DataFrame with a numeric value OR NaN in column

I want to remove all rows with a numeric value of less than 15 in a column, but I want to retain those rows if the value is NaN. How do I this?

This line removes all rows with values less than 15, but it also removes all NaN rows:

df2 = df[(df['columnA'] >= 15)] 

Upvotes: 1

Views: 529

Answers (2)

Clade
Clade

Reputation: 986

I believe what you are looking for is pandas.isnull:

import pandas as pd

df2 = df[(df['columnA'] >= 15) | pd.isnull(df['columnA'])]

Upvotes: 6

ndclt
ndclt

Reputation: 3108

This should work:

df[(df['columnA'] >=15) | (df['columnA'].isnull())]

But you should better use loc instead of just the condition:

df.loc[(df['columnA'] >=15) | (df['columnA'].isnull()), :]

Warning: don't forget the inner parenthesis, it won't work without.

isnull detects missing values (NaN, None or NaT).

Upvotes: 4

Related Questions