Reputation: 864
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
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
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