问题
I have a dataframe like as shown below
df = pd.DataFrame({'subject_id' :[1,2,3,4,5],
'date_of_interview':['2007-05-27','2008-03-13','2010-11-19','2011-10-05','2004-11-02'],
'Age':[31,35,78,72,43],
'value':[6,0.33,1990,np.nan,2001],
'age_detected':[25,35,98,65,40]})
df['date_of_interview'] = pd.to_datetime(df['date_of_interview'])
I would like to create a new column called dis_date
based on value
and age_detected
column
Ex: subject_id = 1 has date_of_interview as 2007-05-27. If we look at his value column, we can see that he has a value of 6 which means we have to subtract 6 years from the date_of_interview to get 2001-05-27
as dis_date
Whereas if you look at subject_id = 3, he has a year value in value column, so his dis_date will be 1990-11-19
When there is NA
in value column, we have to look at his age_detected
column and subtract it from Age
to get the number of years.
Ex: subject_id = 4 has Age
as 72 and AGE_DETECTED
as 65. now the diff is 7 and his dis_date will be 2004-10-05
Please note values in value column if less than 6 digits represent no of years. If it's 1, it means subtract 1 year. If it's 0.33 meaning subtract 4 months. 1 year = 12 months. 0.33 = 3.96 months (4 months)
I was trying something like this but it doesn't help
for i in range(len(df['value'])):
if (len(str(df['value'][i]))) < 6:
df['dis_date'] = df['date_of_interview'] - pd.DateOffset(years=df['value'][i])
I expect my output to be like as shown below
回答1:
In this solution are created helper columns for verify replaced years or subtracted months:
#if value less like 1 multiple by 12, another values set to NaNs
df['m1'] = np.where(df['value'].lt(1), df['value'].mul(12).round(), np.nan)
#if values more like 1000 it is year
df['y1'] = df['value'].where(df['value'].gt(1000))
#if values between 1, 1000 is necessary subtract years from value column
y1 = df['Age'].sub(df['age_detected'])
df['y2'] = np.where(y1.between(1, 1000), df['date_of_interview'].dt.year.sub(y1), np.nan)
#joined years to one column
df['y'] = df['y1'].fillna(df['y2'])
#replaced years by another column
f1 = lambda x: x['date_of_interview'] - pd.DateOffset(year=(int(x['y'])))
df['dis_date1'] = df.dropna(subset=['date_of_interview','y']).apply(f1, axis=1)
#subtracted months if non missing values
f1 = lambda x: x['date_of_interview'] - pd.DateOffset(months=(int(x['m1'])))
df['dis_date2'] = df.dropna(subset=['m1']).apply(f1, axis=1)
#join together
df['dis_date'] = df['dis_date1'].fillna(df['dis_date2'])
print (df)
subject_id date_of_interview Age value age_detected m1 y1 \
0 1 2007-05-27 31 6.00 25 NaN NaN
1 2 2008-03-13 35 0.33 35 4.0 NaN
2 3 2010-11-19 78 1990.00 98 NaN 1990.0
3 4 2011-10-05 72 NaN 65 NaN NaN
4 5 2004-11-02 43 2001.00 40 NaN 2001.0
y2 y dis_date1 dis_date2 dis_date
0 2001.0 2001.0 2001-05-27 NaT 2001-05-27
1 NaN NaN NaT 2007-11-13 2007-11-13
2 NaN 1990.0 1990-11-19 NaT 1990-11-19
3 2004.0 2004.0 2004-10-05 NaT 2004-10-05
4 2001.0 2001.0 2001-11-02 NaT 2001-11-02
来源:https://stackoverflow.com/questions/60037211/derive-date-column-based-on-different-types-of-values