How do I drop rows from a Pandas dataframe based on the maximum value of the whole row?

Question:

I would like to drop rows based on a conditional of the maximum of all the values in the row, not on a column by column basis like most of the solutions on this site demonstrate.

Let’s say I have the following dataframe:

AA BB
A 5 8
B 11 55

I want to drop all the rows where the maximum value of the row is <= 50. So row A should be dropped since the max value in the row is only 8.
How do I go about doing this?

Asked By: Sanjiv Prasad

||

Answers:

Instead of dropping rows, you can keep right ones:

>>> df[df.max(axis=1).gt(50)]
    AA   BB
B    11  55

# Or suggested by @BigBen
>>> df[df.gt(50).any(axis=1)]
    AA   BB
B    11  55
Answered By: Corralien

With drop Pandas function for instance :

import pandas as pd

df = pd.DataFrame({'AA':[5, 11],
                   'BB':[8, 55],
                   })


df = df.drop(df[df.max(axis=1) < 50].index)

# >>> df
#    AA  BB
# 1  11  55
Answered By: Laurent B.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.