6

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!

0

3 Answers 3

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')
Sign up to request clarification or add additional context in comments.

3 Comments

@DanceParty2 was just about to update. Done!
Perfect! Thanks so much. Also, what does .all(1) do?
that's shorthand for all(axis=1), i.e. checking row-wise, default is axis=0, checking column-wise.
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

Comments

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

1 Comment

The question was updated.. I'll update the answer accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.