-
宝慕林4294392
eq搭配使用all:df['Final'] = df.iloc[:,1:].eq('Pass').all(1)#If case sensitive you can usedf['Final'] = df.iloc[:,1:].isin(['Pass','pass']).all(1)#ordf['Final'] = df.iloc[:,1:].apply(lambda x: x.str.lower().eq('pass')).all(1)#ordf['Final'] = df.iloc[:,1:].applymap(str.lower).eq('pass').all(1)此外,您可以使用 map 而不是再次映射 True/False np.where:df['Final'] = np.where(df['Final'], 'Pass', 'Fail')
-
慕少森
cols = ['SW_test', 'HW_test', 'QA_test']df['Final'] = df[cols].eq('Pass').all(1) Item SW_test HW_test QA_test Final0 PC Pass Pass Pass Pass1 Laptop Fail Fail Pass Fail2 Mouse Pass Pass Fail Fail
-
杨__羊羊
您可以应用 lambda 函数来检查条件成立的位置,然后可以用您想要的任何值替换 true/false 值。例如:#create a dataframedf = pd.DataFrame({'a':['Pass','Pass'], 'b':['Pass','Fail']}) a b0 Pass Pass1 Pass Fail在条件成立的地方创建一个新列df['c'] = df.apply(lambda row: row.a=='Pass' and row.b=='Pass', axis=1) a b c0 Pass Pass True1 Pass Fail False将 true/false 值替换为您要显示的内容df['c'] = df['c'].map({ True: 'Pass', False: 'Fail'}) a b c0 Pass Pass Pass1 Pass Fail Fail
-
蛊毒传说
# set column to "Pass" initiallydf["Final"] = "Pass"# set "Fail" rowsdf.loc[( (df.loc[:, ["SW_test", "HW_test", "QA_test"]] == "Fail") | (df.loc[:, ["SW_test", "HW_test", "QA_test"]] == "fail") ).any(axis = 1), "Final"] = "Fail"