Home > OS >  How to simplify a pandas dataframe based on treshold value
How to simplify a pandas dataframe based on treshold value

Time:04-07

Here's my dataframe

      A       B        C        D
1     0    0.41     0.35     0.61
2     0    0.41     0.35        0
3     0    0.21        0        0
4  0.11     0.4     0.53        0

I want to only display columns or rows that contains value more than 0.5, like this

       C        D
1    0.35     0.61
4    0.53        0

How suppose I should do that

CodePudding user response:

Use DataFrame.gt for test greater values with DataFrame.any for test if match at least one value and filter in DataFrame.loc:

m = df.gt(0.5)
df1 = df.loc[m.any(axis=1), m.any()]
print (df1)
     C     D
1  0.35  0.61
4  0.53  0.00

CodePudding user response:

You can use a double boolean indexing. Once on each axis and selection using loc

m = df.gt(0.5)
df.loc[m.any(1), m.any(0)]

output:

      C     D
1  0.35  0.61
4  0.53  0.00
  • Related