简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM