2

I have the following dataset df

   UniqueID Col1 Col2
0      1234    5  NaN
1      1235    3    4
2      1233  NaN    3
3      1111    3  NaN

I'd like to know the number of rows where Col1 is not null and Col2 is null.

4 Answers 4

6
(df.Col1.notnull() & df.Col2.isnull()).sum()

2
Sign up to request clarification or add additional context in comments.

Comments

2

I'd obviously go with PiRSquared's.

If you, however, want to go something fancy with query, then use

In [430]: df.query('Col1 == Col1 & Col2 != Col2').shape[0]
Out[430]: 2

Comments

2

By using dropna

In [451]: df.dropna(axis=0,how='any',subset=['Col1']).Col2.isnull().sum()
Out[451]: 2

Comments

1

First off, in your example 'nAn' is not a null. So, let's replace that string with np.nan.

df = df.replace('nAn',pd.np.nan)

df.Col1.notnull().sum()

or

df.Col1.count() # Note: `count` ignores nulls where `size`, `shape`, and `len` do not.
3

And, use isnull to check for nulls explicityly:

df.Col2.isnull().sum()
2

2 Comments

I think, OP needs on both columns together, see PiRSquared's.
Ah.. will modify later

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.