Pandas DataFrame select the specific columns with NaN values
Asked Answered
C

4

11

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

Carnay answered 14/7, 2016 at 7:52 Comment(1)
shouldn't the title be changed here? 'rows' where it says 'columns'Chetnik
S
13

You need isnull for finding NaN values:

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

Docs.

Stutsman answered 14/7, 2016 at 7:53 Comment(0)
V
1

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

Varia answered 14/7, 2016 at 7:57 Comment(0)
A
1

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

df[df.isna().any(axis=1)]
Andesine answered 14/12, 2023 at 13:49 Comment(0)
M
0

You can use isna() as well:

df[df['merged_key_words'].isna() | df['key_words'].isna()]
Merrythought answered 6/3, 2023 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.