Home > Mobile >  Create a new variable based on 4 other variables
Create a new variable based on 4 other variables

Time:12-16

I have a dataframe in Python called df1 where I have 4 dichotomous variables called Ordering_1; Ordering_2, Ordering_3, Ordering_4 with True/False values.

I need to create a variable called Clean, which is based on the 4 other variables. Meaning, when Ordering_1 == True, then Clean == Ordering_1, when Ordering_2==True, then Clean == Ordering_2. Then Clean would be a combination of all the true values from Ordering_1; Ordering_2, Ordering_3, Ordering_4.

enter image description here

So you can code this:

  1. Create a new blank "clean" column: df1["clean"]=""

  2. Set the rows where the series df.Ordering_1 = True to "Ordering_1":
    df1.loc[df1.Ordering_1,["clean"]] = "Ordering_1"
    enter image description here

  3. Proceed with the remaining columns in the same way.

  • Related