Exclude a specific column from pandas dropna

Viewed 2376

I have dataframe where I want to remove the value NA in all but a specific column (in this case "V1")

df=

 V1   V2   V3   V4  
  A    10   20   NA   
  B    NA   NA   NA   
  C     5   20   3     
  D    15   20   4    
  E    NA   10   5

output=

 V1   V2   V3   V4  
  A    10   20   NA      
  C     5   20   3     
  D    15   20   4    
  E    NA   10   5

can anyone suggest me how can proceed I tried

df.dropna(how='all')

but becuase of first column its not able remove

1 Answers

Use dropna with subset argument:

df.dropna(subset=df.columns.difference(['V1']), how='all')

  V1    V2    V3   V4
0  A  10.0  20.0  NaN
2  C   5.0  20.0  3.0
3  D  15.0  20.0  4.0
4  E   NaN  10.0  5.0

Another option (if V1 values are unique) would be to set V1 as the index first, then your dropna call simplifies:

df.set_index('V1').dropna(how='all')


      V2    V3   V4
V1                 
A   10.0  20.0  NaN
C    5.0  20.0  3.0
D   15.0  20.0  4.0
E    NaN  10.0  5.0

This one makes more sense if V1's values are unique, and it is the only non-numeric column in the dataset.

Related