Home > Blockchain >  How to get only rows that match some criteria
How to get only rows that match some criteria

Time:06-20

I have a dataframe:

Name Score1 Score2 Score3
Leo 89 12 32
Ann 20 85 10
Joanne 10 20 50
Joaquin 20 30 40

How to get only the rows with Score1, Score2 and Score3 >= 85?

I want a fast method, since there could be more than 3 columns as a score and I need a way to filter all of these columns and get only the rows when any of these scores >= 85.

The output should be:

Name Score1 Score2 Score3
Leo 89 12 32
Ann 20 85 10

I also want to know a better approach when the Score column number is variable (we could have 10 score columns instead of 3, so it will be weird to put 10 filters)

CodePudding user response:

In your case

out = df[df.filter(like = 'Score').ge(85).any(1)]
Out[54]: 
  Name  Score1  Score2  Score3
0  Leo      89      12      32
1  Ann      20      85      10

CodePudding user response:

you can add this code

_filter = df.filter(like='Score').ge(85).any(1)
print(df[_filter])
  • Related