0

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank for your comment but it does not work. It gives me 0 all the time in new column 'new_dummy'.
If you are getting all 0 for all records then only reason I can think of can be possible if your one or all of dummy (1/2/3) variables are integers not strings... & if that's the case then either convert your dummy vars into string or edit apply-lambda code like str(x['dummy1'])...
It was the string... Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.