如果任意两列满足条件,Pandas 就会选择行

问题描述 投票:0回答:1

请帮助我过滤数据帧中的条件,该条件应在任意两列中满足。 想象一下在不同运动项目中取得成绩的学生列表。我想过滤该列表,以便新列表 Passed_students 仅显示那些在至少两项不同运动中获得 4 或更高分数的学生。

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],
}

代码应返回以下内容:

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

如果4级以上就足够了,我就可以让它发挥作用:

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)

返回:

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

但是我怎样才能使任何两个 4 或以上的成绩符合 Passed_students 的条件,从而只返回这个?

passed_Students = {
  "Names": ["Tom", "Sally"],
  "Football": [4,2],
  "Basketball": [4,4],
  "Volleyball": [6,6],
  "Foosball": [4,4],
}
filter masking
1个回答
0
投票

您可以使用自定义函数创建一行来记录该学生是否通过。

import pandas as pd

# Your Data
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],
}

# Creating a dataframe from your data
df = pd.DataFrame(Students)

# Custom function to generate "Passed" Column
def passColumnGenerator(row):
    passedCounter = 0
    allSports =  ["Football", "Basketball", "Volleyball", "Football"]
    for eachSport in allSports:
        if row[eachSport] >= 4:
            passedCounter += 1
    return  "Yes" if passedCounter >= 2 else "No"
df["Passed"] = df.apply(passColumnGenerator, axis=1)

现在会有一个“通过”栏,如果学生至少在 2 项运动中通过,则该栏将为“是”。之后,您可以简单地过滤行。

passed_df = df[df["Passed"] == "Yes"]

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.