我不确定我是否可以使用 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 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)
POPMUISE
相关分类