[Python] Select rows if a condition is met in any two columns

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    Please help me filter my dataframe for a condition, that should be fulfilled in any two columns. Imagine a list of students with grades in different sports. I want to filter the list so the new list passed_students shows only those who have scored a 4 or greater in at least two different sports.

    Students = {
    "Names": ["Tom", "Rick", "Sally","Sarah"],
    "Football": [4, 5, 2,1],
    "Basketball": [4, 2, 4,2],
    "Volleyball": [6, 1, 6,1],
    "Foosball": [4, 3, 4,3],
    }


    The Code should return this:

    passed_Students = {
    "Names": ["Tom", "Sally"],
    "Football": [4,2],
    "Basketball": [4,4],
    "Volleyball": [6,6],
    "Foosball": [4,4],
    }


    I can make it work if one grade above 4 is sufficient:

    import numpy as np
    import pandas as pd


    Students = {
    "Names": ["Tom", "Rick", "Sally","Sarah"],
    "Football": [4, 5, 2,1],
    "Basketball": [4, 2, 4,2],
    "Volleyball": [6, 1, 6,1],
    "Foosball": [4, 3, 4,3],
    }
    Students = pd.DataFrame(Students)


    passed_Students= Students[(Students["Football"]>3) |(Students["Basketball"]>3)|(Students["Volleyball"]>3)|(Students["Foosball"]>3) ]

    print(passed_Students)



    This returns:

    Students = {
    "Names": ["Tom", "Rick", "Sally"],
    "Football": [4, 5, 2],
    "Basketball": [4, 2, 4],
    "Volleyball": [6, 1, 6],
    "Foosball": [4, 3, 4],
    }


    But how can I make it such, that any two grades of 4 or above qualify for passed_students, thereby returning only this?

    passed_Students = {
    "Names": ["Tom", "Sally"],
    "Football": [4,2],
    "Basketball": [4,4],
    "Volleyball": [6,6],
    "Foosball": [4,4],
    }

    Continue reading...

Compartilhe esta Página