Drop rows with all zeros in pandas data frame
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Mysterious Puzzle
--
Chapters
00:00 Drop Rows With All Zeros In Pandas Data Frame
00:29 Accepted Answer Score 147
00:42 Answer 2 Score 226
00:56 Answer 3 Score 30
01:12 Answer 4 Score 34
01:23 Thank you
--
Full question
https://stackoverflow.com/questions/2264...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Mysterious Puzzle
--
Chapters
00:00 Drop Rows With All Zeros In Pandas Data Frame
00:29 Accepted Answer Score 147
00:42 Answer 2 Score 226
00:56 Answer 3 Score 30
01:12 Answer 4 Score 34
01:23 Thank you
--
Full question
https://stackoverflow.com/questions/2264...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas
#avk47
ANSWER 1
Score 226
One-liner. No transpose needed:
df.loc[~(df==0).all(axis=1)]
And for those who like symmetry, this also works...
df.loc[(df!=0).any(axis=1)]
ACCEPTED ANSWER
Score 147
It turns out this can be nicely expressed in a vectorized fashion:
> df = pd.DataFrame({'a':[0,0,1,1], 'b':[0,1,0,1]})
> df = df[(df.T != 0).any()]
> df
   a  b
1  0  1
2  1  0
3  1  1
ANSWER 3
Score 34
I look up this question about once a month and always have to dig out the best answer from the comments:
df.loc[(df!=0).any(1)]
Thanks Dan Allan!
ANSWER 4
Score 30
Replace the zeros with nan and then drop the rows with all entries as nan.
After that replace nan with zeros.
import numpy as np
df = df.replace(0, np.nan)
df = df.dropna(how='all', axis=0)
df = df.replace(np.nan, 0)