1

How can I select the rows with null values in respect of columns name?

What I have:

ID A B
1 a b
2 v
3 y
4 w j
5 w

What I want:

Select rows with null in respect with e.g. column B:

ID B
3 y
5 w
3
  • why 2 not selected?
    – Epsi95
    Commented Feb 7, 2021 at 16:01
  • Because I want to select the null values only in column B!
    – Jsmoka
    Commented Feb 7, 2021 at 16:03
  • 2
    df[df['B'].isna()]
    – Epsi95
    Commented Feb 7, 2021 at 16:05

2 Answers 2

1

Use pandas.DataFrame.pop and pandas.Series.isna:

>>> df
   ID    A    B
0   1    a    b
1   2  NaN    v
2   3    y  NaN
3   4    w    j
4   5    w  NaN

>>> df[df.pop('B').isna()]

   ID  A
2   3  y
4   5  w

Use pop if you do not need column 'B' in the original dataframe afterwards. Otherwise, use pandas.DataFrame.drop:

>>> df[df['B'].isna()].drop('B', axis=1)
1

I guess you can use isna() or isnull() functions.

df[df['column name'].isna()]

(or)

df[df['column name'].isnull()]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.