Home > database >  Create column based on conditions using pandas
Create column based on conditions using pandas

Time:08-31

I have 2 columns Runid and type. I want to create a column result with following values. if runid='R1' and type==1.5 then result column should have value pass and if runid=='R2' and type==1 then result column should have value fail.

Pleas help with corresponding pandas code.

CodePudding user response:

Considering you have only two conditions:

def result(runid, typ):
    if runid == "R1" and typ == 1.5:
        return "PASS"
    else: # add condition here if needed
        return "FAIL"

df["result"] = df.apply(lambda x: result(x.runid, x.type), axis=1)

CodePudding user response:

You can use enter image description here

If necessary, you can add more conditions to categorize the other combinations of values.

  • Related