vishwajeet Mane
vishwajeet Mane

Reputation: 344

How to find column with specific row is zero in pandas

I have pandas dataframe like below.

    index col_A    col_B    col_C   col_D   col_E
      a     12       15      28       34     23
      b     23       37      46       34     92
      c     34       32      24       93     12 
      d     12       0        1       0      0

I want output like below.

    index     col_B   col_D   col_E
      a        15      34     23
      b        37      34     92
      c        32      93     12 
      d        0        0      0

The output frame condition is like if there is 0 in index d row. It should be in output dataframe.

Upvotes: 1

Views: 682

Answers (1)

jezrael
jezrael

Reputation: 862611

Use DataFrame.loc for select index d and then filter by another DataFrame.loc with : for all rows and condition for filter columns:

df = df.loc[:, df.loc['d'].eq(0)]
print (df)
       col_B  col_D  col_E
index                     
a         15     34     23
b         37     34     92
c         32     93     12
d          0      0      0

Upvotes: 2

Related Questions