Ems
Ems

Reputation: 7

Pandas - Create a binary column that returns True if a True is present in at least one out of two different columns

I have a data frame (enct) with two binary columns (008.45 and a04.7) and I would like to create a third column name "cdi" that would return True if there is at least one true in one of those two columns. Here's an example depicting the desired results:

008.45      a04.7       cdi
True        False      True
False       False      False   
False       True       True

Here is the code that I have tried: enct["cdi"] = enct.apply(lambda x: True if x == "True" in (x['008.45'], x["a04.7"]) else False)

Upvotes: 0

Views: 92

Answers (2)

Canasta
Canasta

Reputation: 228

Use OR operator(|)

enct['cdi'] = enct['008.45'] | enct['a04.7']

Upvotes: 3

wasif
wasif

Reputation: 15470

You can just use df.any():

df[['008.45','a04.7']].any().any()

Upvotes: 0

Related Questions