无法根据 python 中的条件计算年龄。获取值错误
Cant calculate the age with conditions in python. Getting a value error
好吧,我正在尝试从数据集中计算患者的年龄。我最初设法用一个函数来做到这一点,但我计算了从今天到生日的时间。因此,我尝试为患者死亡的情况添加一个 if 语句。在这种情况下,我想计算从死亡日期到出生日期的年龄。
这是我的代码:
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
然后我尝试应用函数:
df['age'] = df['birthdate'].apply(calculate_age,args = (df.alive, df.death))
我收到以下错误:
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().
有人能帮忙吗?
尝试:
df.apply(lambda x: calculate_age(x.birthdate, x.alive, x.death), axis=1)
这是使用 Pandas get the age from a date (example: date of birth)
的替代方法
import pandas as pd
import numpy as np
# Recreate a sample dataframe
np.random.seed(2018)
df = pd.DataFrame({
'birthday': [pd.Timestamp(1970,1,1) + pd.Timedelta(days=i)
for i in np.random.randint(10000,size=10)],
'alive': np.random.choice(['yes','no'], size=10, p = [0.8, 0.2]),
'death': [pd.Timestamp.today().date() - pd.Timedelta(days=i)
for i in np.random.randint(1000,size=10)]
})
df.loc[df['alive'] == 'yes', 'death'] = pd.Timestamp('nat')
# Calculate age
df['age'] = ((np.where(df['alive'] == 'yes', pd.Timestamp.today().date(), df['death'])
- df['birthday']).astype('<m8[Y]').astype(int))
# Display
print(df)
Returns:
alive birthday death age
0 yes 1995-12-02 NaT 22
1 no 1977-09-26 2016-01-29 38
2 yes 1972-07-06 NaT 45
3 yes 1990-01-20 NaT 28
4 yes 1978-01-29 NaT 40
5 yes 1988-04-17 NaT 30
6 yes 1985-11-03 NaT 32
7 no 1975-11-06 2017-01-23 41
8 no 1990-03-08 2017-06-24 27
9 yes 1980-12-07 NaT 37
好吧,我正在尝试从数据集中计算患者的年龄。我最初设法用一个函数来做到这一点,但我计算了从今天到生日的时间。因此,我尝试为患者死亡的情况添加一个 if 语句。在这种情况下,我想计算从死亡日期到出生日期的年龄。
这是我的代码:
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
然后我尝试应用函数:
df['age'] = df['birthdate'].apply(calculate_age,args = (df.alive, df.death))
我收到以下错误:
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().
有人能帮忙吗?
尝试:
df.apply(lambda x: calculate_age(x.birthdate, x.alive, x.death), axis=1)
这是使用 Pandas get the age from a date (example: date of birth)
的替代方法import pandas as pd
import numpy as np
# Recreate a sample dataframe
np.random.seed(2018)
df = pd.DataFrame({
'birthday': [pd.Timestamp(1970,1,1) + pd.Timedelta(days=i)
for i in np.random.randint(10000,size=10)],
'alive': np.random.choice(['yes','no'], size=10, p = [0.8, 0.2]),
'death': [pd.Timestamp.today().date() - pd.Timedelta(days=i)
for i in np.random.randint(1000,size=10)]
})
df.loc[df['alive'] == 'yes', 'death'] = pd.Timestamp('nat')
# Calculate age
df['age'] = ((np.where(df['alive'] == 'yes', pd.Timestamp.today().date(), df['death'])
- df['birthday']).astype('<m8[Y]').astype(int))
# Display
print(df)
Returns:
alive birthday death age
0 yes 1995-12-02 NaT 22
1 no 1977-09-26 2016-01-29 38
2 yes 1972-07-06 NaT 45
3 yes 1990-01-20 NaT 28
4 yes 1978-01-29 NaT 40
5 yes 1988-04-17 NaT 30
6 yes 1985-11-03 NaT 32
7 no 1975-11-06 2017-01-23 41
8 no 1990-03-08 2017-06-24 27
9 yes 1980-12-07 NaT 37