1

I have a df like this

        x  y1  y2  y3  y4
0   -20.0 NaN NaN NaN NaN
1   -19.9 NaN NaN  20 NaN
2   -19.8 NaN NaN NaN NaN
3   -19.7 NaN NaN NaN NaN
4   -19.6 NaN  10 NaN NaN

I want the program to drop all rows with only NaN values in columns y1 to y4.

The output would be this:

        x  y1  y2  y3  y4
1   -19.9 NaN NaN  20 NaN
4   -19.6 NaN  10 NaN NaN

I tried df.dropna(axis = 0, how = "all", inplace = True) but this counts column x aswell.

1 Answer 1

1

You need to specify your columns as subset:

df.dropna(subset=['y1', 'y2', 'y3', 'y4'], how='all', inplace=True)

Output:

      x  y1    y2    y3  y4
1 -19.9 NaN   NaN  20.0 NaN
4 -19.6 NaN  10.0   NaN NaN
Sign up to request clarification or add additional context in comments.

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.