Select columns with all zero entries in a pandas dataframe

Question:

Given a dataframe df, how to find out all the columns that only have 0 as the values?

   0  1  2  3  4  5  6  7
0  0  0  0  1  0  0  1  0
1  1  1  0  0  0  1  1  1

Expected output

   2  4
0  0  0
1  0  0
Asked By: user511792

||

Answers:

I’d simply compare the values to 0 and use .all():

>>> df = pd.DataFrame(np.random.randint(0, 2, (2, 8)))
>>> df
   0  1  2  3  4  5  6  7
0  0  0  0  1  0  0  1  0
1  1  1  0  0  0  1  1  1
>>> df == 0
       0      1     2      3     4      5      6      7
0   True   True  True  False  True   True  False   True
1  False  False  True   True  True  False  False  False
>>> (df == 0).all()
0    False
1    False
2     True
3    False
4     True
5    False
6    False
7    False
dtype: bool
>>> df.columns[(df == 0).all()]
Int64Index([u'2', u'4'], dtype=int64)
>>> df.loc[:, (df == 0).all()]
   2  4
0  0  0
1  0  0
Answered By: DSM

Another way is to mask the nonzero values and drop columns where all values are masked.

df1 = df.mask(df != 0).dropna(axis=1)

# or filter the entire frame
df1 = df[df.eq(0)].dropna(axis=1)

res

Answered By: cottontail
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.