I am new to Python. In a Data frame, there are two columns, DOB (Date of Birth) and DOD (Date of Death). I want to create a new categorical column in the dataframe with name "IS_ALIVE". The condition for this new field, IS_ALIVE should be "1" when DOB is populated and DOD is null; IS_ALIVE should be "0" when DOB and DOD both are not null, meaning, some date is populated the DOD.
I searched and tried multiple ways, no luck. Please help me.
You can create boolean mask with isna
and notna
chained by &
(bitwise AND) and convert to integers for True/False
to 1/0
:
df['IS_ALIVE'] = (df.DOB.notna() & df.DOD.isna()).astype(int)
#for old versions of pandas
#df['IS_ALIVE'] = (df.DOB.notnull() & df.DOD.isnull()).astype(int)