Fan
Fan

Reputation: 199

Pandas DataFrame select the specific columns with NaN values

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

Upvotes: 11

Views: 15197

Answers (4)

amrita yadav
amrita yadav

Reputation: 21

You can also use isna() method with axis 1 (i.e., along the columns):

df[df.isna().any(axis=1)]

Upvotes: 1

Nikita Malviya
Nikita Malviya

Reputation: 1399

You can use isna() as well:

df[df['merged_key_words'].isna() | df['key_words'].isna()]

Upvotes: 1

piRSquared
piRSquared

Reputation: 294318

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...

Upvotes: 1

jezrael
jezrael

Reputation: 862751

You need isnull for finding NaN values:

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

Docs.

Upvotes: 13

Related Questions