Home > database >  How to remove columns if the value of one specific row is 0
How to remove columns if the value of one specific row is 0

Time:05-19

I have a fairly straight forward question but I could not find the answer on stack. I have a pd.df

Index   A  B  C
0       1  1  0
1       0  0  0 
2       1  1  1
3       0  0  1

I simply wish to remove all columns where the fourth row (3) is 0. So only column C would remain. Cheers.

CodePudding user response:

Assuming "Index" the index, you can use boolean indexing:

df2 = df.loc[:, df.iloc[3].ne(0)]

output:

   C
0  0
1  0
2  1
3  1

output of df.iloc[3].ne(0):

A    False
B    False
C     True
Name: 3, dtype: bool
  • Related