Pandas DataFrame select the specific columns with NaN values

Question:

I have a two-column DataFrame, I want to select the rows with NaN in either column.

I used this method df[ (df['a'] == np.NaN) | (df['b'] == np.NaN) ]
However it returns an empty answer. I don’t know what’s the problem

Asked By: Fan

||

Answers:

You need isnull for finding NaN values:

df[ (df['a'].isnull()) | (df['b'].isnull()) ]

Docs.

Answered By: jezrael

You could apply isnull() to the whole dataframe then check if the rows have any nulls with any(1)

df[df.isnull().any(1)]

Timing

df = pd.DataFrame(np.random.choice((np.nan, 1), (1000000, 2), p=(.2, .8)), columns=['A', 'B'])

enter image description here

I did not expect these results…

Answered By: piRSquared

You can use isna() as well:

df[df['merged_key_words'].isna() | df['key_words'].isna()]
Answered By: Nikita Malviya
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.