Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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           

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading