How to filter in NaN (pandas)?

78

I have a pandas dataframe (df), and I want to do something like:

newdf = df[(df.var1 == 'a') & (df.var2 == NaN)]

I've tried replacing NaN with np.NaN, or 'NaN' or 'nan' etc, but nothing evaluates to True. There's no pd.NaN.

I can use df.fillna(np.nan) before evaluating the above expression but that feels hackish and I wonder if it will interfere with other pandas operations that rely on being able to identify pandas-format NaN's later.

I get the feeling there should be an easy answer to this question, but somehow it has eluded me. Any advice is appreciated. Thank you.

This question is tagged with python pandas nan

~ Asked on 2014-07-31 02:57:26

The Best Answer is


84

This doesn't work because NaN isn't equal to anything, including NaN. Use pd.isnull(df.var2) instead.

~ Answered on 2014-07-31 03:02:10


81

Simplest of all solutions:

filtered_df = df[df['var2'].isnull()]

This filters and gives you rows which has only NaN values in 'var2' column.

~ Answered on 2017-12-04 09:18:21


Most Viewed Questions: