Home > Software engineering >  How to filter a Pandas dataframe to keep entire rows/colums if a criterium is fullfilled?
How to filter a Pandas dataframe to keep entire rows/colums if a criterium is fullfilled?

Time:04-25

I am learning Python Pandas and I am having some trouble with data filtering. I have gone through multiple examples and I cannot seem to find an approach that fits my particular need:

In a dataframe with numerical values, I would like to filter rows and columns by the following criterium:

"If ANY value in a row is above a threshold, include the WHOLE row (including values that are below the threshold). Else, discard the row."

This should apply to all rows. Subsequently, I would repeat for columns.

Any help is highly appreciated.

CodePudding user response:

Use:

value = 123
df[df.gt(value).any(axis=1)]

For columns, this would be:

value = 123
df.loc[:, df.gt(value).any(axis=0)]
  • Related