为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

守着星空守着你 2023-05-16 15:10:52
我不确定我是否可以使用 operators 列来返回一个 pandas 系列,它将根据它的及格分数、运算符和实际值来确定某一行的活动是通过还是失败。数据集示例: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    Actual1   Quiz        80              >=          791   Attendance  2               <=          02   Quiz        80              >=          822   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)
查看完整描述

1 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

你可以做:

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

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

  • 实际测验

  • 实际出勤率

然后你可以这样做:

data['Actual_quiz'] =< 80

等等。


查看完整回答
反对 回复 2023-05-16
  • 1 回答
  • 0 关注
  • 88 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信