Home > Blockchain >  how eliminate all columns whose have 0 values in one of their row?
how eliminate all columns whose have 0 values in one of their row?

Time:05-01

I wanna remove all columns whose have 0 in one of their rows:

Sk   col1       col2       col3
A      4         5           0
B       0        2           9
l       0        6           6

output:

Sk          col2       
A              5           
B              2           
l              6           

CodePudding user response:

Use boolean indexing with eq(0) any and invert the match with ~:

df.loc[:, ~df.eq(0).any()]

Alternatively with ne and all:

df.loc[:, df.ne(0).all()]

output:

  Sk  col2
0  A     5
1  B     2
2  l     6
  • Related