1

From the data frame below, how to delete all the rows where columns [B, C, D] all are having null values?

Input data frame:

    A    B    C    D
0  20  NaN  2.0  NaN
1   3  1.0  NaN  2.0
2  44  4.0  NaN  NaN
3  67  NaN  NaN  NaN
4  52  3.0  2.0  NaN
5   8  NaN  NaN  NaN

Desired output:

    A    B    C    D
0  20  NaN  2.0  NaN
1   3  1.0  NaN  2.0
2  44  4.0  NaN  NaN
4  52  3.0  2.0  NaN

3 Answers 3

3

Let us try dropna

df=df.dropna(thresh=1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you . It does the job for above question.
1

Personally, I would use pandas.DataFrame.dropna() method with its parameters how and subset. The code is following.

>>> df = df.dropna(how='all', subset=['B', 'C', 'D'])
>>> print(df)
    A    B    C    D
0  20  NaN  2.0  NaN
1   3  1.0  NaN  2.0
2  44  4.0  NaN  NaN
4  52  3.0  2.0  NaN

2 Comments

Thank you.That did the trick.I find this method most comfortable.
@jjnair, I am glad I could have helped. However, If answer is correct and helpful, also consider accepting it.
1

You can count the (non NaN) value across a subset of columns:

df[df[['B', 'C', 'D']].count(axis=1) > 0]

It gives as expected:

    A    B    C    D
0  20  NaN  2.0  NaN
1   3  1.0  NaN  2.0
2  44  4.0  NaN  NaN
4  52  3.0  2.0  NaN

and only focuses on the required columns

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.