I have data as below(sample only, data will have all combinations of below values). I have to create new column based conditions applied on 3 columns.
Output: A B C
Normal Normal Normal
High High High
High High Low
Low Low High
Normal Normal Low
Normal Normal High
Normal High High
No Test No Test High
No Test No Test Low
No Test No Test No Test
No Test High High
def flag_df(df): if df[(df['A']=='Normal') & (df['B']=='Normal') & (df['C']=='Normal')]: return 'Normal' elif df[(df['A']=='High']) & (df['B']=='High') & (df['B']=='Low')]: return 'OH' elif df[(df['A']=='Normal']) & (df['B']=='Normal') & (df['C']=='Low')]: return 'SHPE' elif df[(df['A']=='Normal']) & (df['B']=='Normal') & (df['C']=='High')]: return 'SHPO' elif df[(df['A']=='No Test']) & (df['B']=='No Test') & (df['C']=='No Test')]: return 'No Test' elif df[(df['A']=='No Test']) | (df['B']=='No Test') & (df['C']=='High')]: return 'SHPO' df['D'] = df.apply(flag_df, axis = 1)The results are coming correctly for below
#expect is col-A or Col-B has "High" but Col C has "High" it should label Col-D as 'SHPO' elif df[(df['A']=='No Test']) | (df['B']=='High') & (df['C']=='High')]: return 'SHPO' #expect is col-A AND Col-B has "no Test" but Col C has "High" it should label Col-D as 'SHPO' elif df[(df['A']=='No Test']) & (df['B']=='No Test') | (df['C']=='High')]: return 'SHPO'How to work with | or & operators on 3 or more column conditions, is there a better approach to write a function? Appreciate your help, Thank you!!!