1

I have a data frame and I want do a null check and store the null value rows in separate data frame. But, one constrain is, I don't want to do null check for one particular column. How can I achieve this?

For example, In below Data Frame,

   Name  Age  Class
0   tom   10      NaN
1  nick   15     10
2  juli   14      9

If I apply, df[df.isnull().any(axis=1)], It gives me

   Name  Age  Class
0  tom   10    NaN

But, I want don't want to do null check for Class column and I'm expecting empty data frame for this case. I have searched in SO but couldn't find the solution.

3 Answers 3

2

Try:

cols_to_excl = ['Class']

df.loc[df[df.columns ^ cols_to_excl].isnull().any(axis=1)]

In essence: df.columns ^ cols_to_excl will return all columns, besides all the columns from the list cols_to_excl.

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

Comments

1

You can try the following :

df[df.iloc[:, :2].isnull().any(axis=1)]

This can work if you know that your function will only work on this dataframe otherwise you can try generalized approach as stated by @Grzegorz Skibinski

Comments

0

xor operation will be deprecated in newer versions. Pandas suggests using symmetric_difference instead.

cols_to_excl = ['Class']

df.loc[df[df.columns.symmetric_difference(cols_to_excl)].isnull().any(axis=1)]

Comments

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.