使用列运算符检查是否通过

Using the column operator to check if pass or fail

我不确定如何使用运算符列来 return 一个 pandas 系列,它将确定某一行的 activity 是否通过或失败基于它的及格分数、运算符和实际。

数据集示例:

data={"ID": [1,1,2,2],
      "Activity": ["Quiz", "Attendance", "Quiz", "Attendance"],
      "Passing Score": [80, 2, 80, 2],
      "Operator": [">=", "<=", ">=", "<="],
      "Actual": [79, 0, 82, 3]
     }
data = pd.DataFrame(data)

外观:

ID  Activity    Passing Score   Operator    Actual
1   Quiz        80              >=          79
1   Attendance  2               <=          0
2   Quiz        80              >=          82
2   Attendance  2               <=          3

我的解决方案:

def score(pass_score, operator, actual):
    """
    pass_score: pandas Series, passing Score
    operator: pandas Series, operator
    actual: pandas Series, actual Score
    """
    
    the_list=[]
    
    for a,b,c in zip(pass_score, operator, actual):
        if b == ">=":
            the_list.append(c >= a)
        elif b == "<=":
            the_list.append(c <= a)
    
    mapper={True: "Pass",
            False: "Fail"
           }
    
    return pd.Series(the_list).map(mapper)

data["Peformance Tag"] = score(data["Passing Score"], data["Operator"], data["Actual"])

我想要实现的(如果可能的话,通过使用字典来缩短我的代码):

operator_map = {">=": >=,
                "<=": <=,
               }

data["Peformance Tag"] =  data[["Passing Score", "Operator", "Actual"]].apply(lambda x: x[0] operator_map[x[1]]  x[2], axis=1)

你可以这样做:

data[['Passing Score', 'Operator', 'Actual']].astype(str).sum(axis=1).apply(eval)

但说实话我不会太相信这种编程。我觉得你的数据框可以通过 2 列以更有意义的方式重塑:

  • Actual_quiz
  • Actual_Attendance

那么你可以这样做:

data['Actual_quiz'] =< 80

等等。