Following would produce your desired result,
res = df.groupby(['patient_id',df['hospitalization_date'].dt.year]).sum().reset_index()
pd.merge(df,res,how='left', left_on= ['patient_id',df['hospitalization_date'].dt.year], right_on = ['patient_id','hospitalization_date'],suffixes=['_x','_sum'])[['patient_id','hospitalization_date_x','previous_year_x','previous_year_sum']]
Output:
patient_id hospitalization_date_x previous_year_x previous_year_sum
0 1 2020-11-02 2 2
1 1 2019-12-20 1 1
2 1 2019-11-30 0 1
3 1 2015-01-01 0 0
4 2 2019-04-17 1 1
5 2 2018-11-05 0 0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…