Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop all columns where all values are zero

I have a simple question which relates to similar questions here, and here.

I am trying to drop all columns from a pandas dataframe, which have only zeroes (vertically, axis=1). Let me give you an example:

df = pd.DataFrame({'a':[0,0,0,0], 'b':[0,-1,0,1]})

    a   b
0   0   0
1   0  -1
2   0   0
3   0   1

I'd like to drop column asince it has only zeroes.

However, I'd like to do it in a nice and vectorized fashion if possible. My data set is huge - so I don't want to loop. Hence I tried

df = df.loc[(df).any(1), (df!=0).any(0)]

    b
1  -1
3   1

Which allows me to drop both columns and rows. But if I just try to drop the columns, locseems to fail. Any ideas?

like image 567
Rachel Avatar asked Dec 01 '22 15:12

Rachel


1 Answers

You are really close, use any - 0 are casted to Falses:

df = df.loc[:, df.any()]
print (df)

   b
0  0
1  1
2  0
3  1
like image 69
jezrael Avatar answered Dec 04 '22 01:12

jezrael