Home > Software design >  Create a dummy based on other variables in Python
Create a dummy based on other variables in Python

Time:09-27

I have three columns dummy variables, 'dummy1', 'dummy2' and 'dummy3. I would like to create a new column with a new dummy, 'new_dummy', with 1 if at least one of dummies of before has a value 1. I am using python.

I have in mind something like this:

mydata['new_dummy'] = mydata.apply(lambda x: x['1'] if x['dummy1'] or x['dummy2'] or x['dummy3'] == '1' else x['0'], axis=1)

But it does not work. Give me an error saying "KeyError: '0'". Any idea why I have that error or any other method to do it?

CodePudding user response:

Try This...

mydata = pd.DataFrame({"dummy1":["1","0"],
                       "dummy2":["0","0"],
                       "dummy3":["0","0"]})

mydata['new_dummy'] = mydata.apply(lambda x: '1' if str(x['dummy1']) == '1' or str(x['dummy2']) == '1' or str(x['dummy3']) == '1' else '0', axis=1)

# Output...

  dummy1 dummy2 dummy3 new_dummy
0      1      0      0         1
1      0      0      0         0
  • Related