Drop rows with all zeros in pandas data frame
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping
--
Chapters
00:00 Question
00:38 Accepted answer (Score 145)
00:55 Answer 2 (Score 204)
01:13 Answer 3 (Score 49)
01:26 Answer 4 (Score 32)
01:44 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
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping
--
Chapters
00:00 Question
00:38 Accepted answer (Score 145)
00:55 Answer 2 (Score 204)
01:13 Answer 3 (Score 49)
01:26 Answer 4 (Score 32)
01:44 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)