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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x