5

Given the following:

import pandas as pd
import numpy as np

df = pd.DataFrame({'a':[np.nan,1,2],'b':[np.nan,np.nan,4]})

     a    b
0  NaN  NaN
1  1.0  NaN
2  2.0  4.0

How do I return rows where both columns 'a' and 'b' are null without having to use pd.isnull for each column?

Desired result:

     a    b
0  NaN  NaN

I know this works (but it's not how I want to do it):

df.loc[(pd.isnull(df['a']) & (pd.isnull(df['b'])]

I tried this:

df.loc[pd.isnull(df[['a', 'b']])]

...but got the following error:

ValueError: Cannot index with multidimensional key

Thanks in advance!

Dance Party2
  • 7,214
  • 17
  • 59
  • 106

3 Answers3

7

You are close:

df[~pd.isnull(df[['a', 'b']]).all(1)]

Or

df[df[['a','b']].isna().all(1)]

How about:

df.dropna(subset=['a','b'], how='all')
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
4

With your shown samples, please try following. Using isnull function here.

mask1 = df['a'].isnull()
mask2 = df['b'].isnull()
df[mask1 & mask2]


Above answer is with creating 2 variables for better understanding. In case you want to use conditions inside df itself and don't want to create condition variables(mask1 and mask2 in this case) then try following.

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

Output will be as follows.

    a   b
0   NaN NaN
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
2

You can use dropna() with parameter as how=all

df.dropna(how='all')

Output:

   a    b
1  1.0  NaN
2  2.0  4.0

Since the question was updated, you can then create masking either using df.isnull() or using df.isna() and filter accordingly.

df[df.isna().all(axis=1)]
   a   b
0 NaN NaN
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45