+1 vote
in Programming Languages by (56.8k points)
I want to drop all columns from a DataFrame that contains only 0s for all rows. What functions of Pandas should I use for it?

1 Answer

+1 vote
by (71.8k points)
selected by
 
Best answer

To drop columns with only 0s in all rows, you need to first identify those columns. Then you can use loc to unselect those columns. To identify columns with only 0s in all rows, you can use either all() or any() function of the DataFrame.

In the following example, I am using all() function to find all columns with only 0s. Then I negate it to get all columns with non-zero values; all such columns are used with loc to drop columns with only 0s.

>>> import pandas as pd

>>> df = pd.DataFrame({'A':[1,2,3,4], 'B':[0,0,0,0], 'C':[10,20,30,40], 'D':[0,0,0,0]})

>>> df

   A  B   C  D

0  1  0  10  0

1  2  0  20  0

2  3  0  30  0

3  4  0  40  0

>>> (df==0).all(axis=0)

A    False

B     True

C    False

D     True

dtype: bool

>>> df1=df.loc[:, ~(df==0).all(axis=0)]

>>> df1

   A   C

0  1  10

1  2  20

2  3  30

3  4  40


...