I have a data frame like as shown below
df1 = pd.DataFrame({'person_id': [11, 21, 31, 41, 51],
'date_1': ['12/30/1961', '05/29/1967', '02/03/1957', '7/27/1959', '01/13/1971'],
'date_2': ['07/23/2017','05/29/2017','02/03/2015',np.nan,np.nan]})
df1 = df1.melt('person_id', value_name='dates')
I would like to get the number of days to the previous and next year.
I am able to get the previous and next year using the below code
df1['cur_year'] = pd.DatetimeIndex(df1['dates']).year
df1['prev_year'] = (df1['cur_year'] - 1)
df1['next_year'] = (df1['cur_year'] + 1)
As you can see that the year
values are constantly changing for each row and I don't have a fixed baseline date, how can I calculate the difference in days to dates like 31/12
for the previous year and 01/01
for the next year.
Please note that end date is not included while getting the number of days
I have shown a sample output for 2 subjects below.
updated screenshot
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…