4 回答
TA贡献2021条经验 获得超8个赞
eq搭配使用all:
df['Final'] = df.iloc[:,1:].eq('Pass').all(1)
#If case sensitive you can use
df['Final'] = df.iloc[:,1:].isin(['Pass','pass']).all(1)
#or
df['Final'] = df.iloc[:,1:].apply(lambda x: x.str.lower().eq('pass')).all(1)
#or
df['Final'] = df.iloc[:,1:].applymap(str.lower).eq('pass').all(1)
此外,您可以使用 map 而不是再次映射 True/False np.where:
df['Final'] = np.where(df['Final'], 'Pass', 'Fail')
TA贡献2019条经验 获得超9个赞
cols = ['SW_test', 'HW_test', 'QA_test']
df['Final'] = df[cols].eq('Pass').all(1)
Item SW_test HW_test QA_test Final
0 PC Pass Pass Pass Pass
1 Laptop Fail Fail Pass Fail
2 Mouse Pass Pass Fail Fail
TA贡献1943条经验 获得超7个赞
您可以应用 lambda 函数来检查条件成立的位置,然后可以用您想要的任何值替换 true/false 值。例如:
#create a dataframe
df = pd.DataFrame({'a':['Pass','Pass'], 'b':['Pass','Fail']})
a b
0 Pass Pass
1 Pass Fail
在条件成立的地方创建一个新列
df['c'] = df.apply(lambda row: row.a=='Pass' and row.b=='Pass', axis=1)
a b c
0 Pass Pass True
1 Pass Fail False
将 true/false 值替换为您要显示的内容
df['c'] = df['c'].map({ True: 'Pass', False: 'Fail'})
a b c
0 Pass Pass Pass
1 Pass Fail Fail
TA贡献1895条经验 获得超3个赞
# set column to "Pass" initially
df["Final"] = "Pass"
# set "Fail" rows
df.loc[(
(df.loc[:, ["SW_test", "HW_test", "QA_test"]] == "Fail") |
(df.loc[:, ["SW_test", "HW_test", "QA_test"]] == "fail")
).any(axis = 1), "Final"] = "Fail"
添加回答
举报