Well, i am trying to calculate the age of patients from a dataset. I managed initially to do that with a function, but i calculated it from today to birthdate. So i tried to add an if statement for the case where the patient died. In this case i wanted to calculate the age from the death date to birthdate.
Here is my code:
def calculate_age(born, alive, death):
today = date.today()
today = datetime.now()
age_in_years = today.year - born.year - ((today.month, today.day) < (born.month, born.day))
months = (today.month - born.month - (today.day < born.day)) %12
age = today - born
if alive == 'No':
age_in_years1 = death.year - born.year - ((death.month, death.day) < (born.month, born.day))
months = (death.month - born.month - (death.day < born.day)) %12
age = death - born
return age_in_years1
else:
return age_in_years
Then i tried to apply the function:
df['age'] = df['birthdate'].apply(calculate_age,args = (df.alive, df.death))
And i get the following error:
ValueError Traceback (most recent call last)
<ipython-input-61-bde1cb6c3981> in <module>()
----> 1 df['age'] = df['birthdate'].apply(calculate_age,args = (df.alive, df.death))
^
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Can anyone help?