Dance Party2
Dance Party2

Reputation: 7536

Pandas DataFrame Isnull Multiple Columns at Once

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!

Upvotes: 6

Views: 10813

Answers (3)

ThePyGuy
ThePyGuy

Reputation: 18426

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

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133600

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

Upvotes: 4

Quang Hoang
Quang Hoang

Reputation: 150785

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')

Upvotes: 7

Related Questions