Obtain column based on row value

Viewed 27

I have the following table:

       A  B  C  D  E  F  G  H
A
B
C
D
E
F
G
H
Value  1  3  4  1  4  3  3  4 

How could I filter columns where Value is greater than 3?

1 Answers

You can use boolean indexing:

df.loc[:, df.loc['Value'].astype(int).gt(3)]

NB. Converting to integer/numeric as a safety. If non numeric values are present in the columns, this would indeed force an object type.

output:

       C  E  H
A      x  x  x
B      x  x  x
C      x  x  x
D      x  x  x
E      x  x  x
F      x  x  x
G      x  x  x
H      x  x  x
Value  4  4  4
Related