Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

python - counting instances in previous year

I have a very large data set of hospital admissions. For each admission I would like to know the number of admissions that took place within the previous year. My dataset is in a form of a dataframe including patient ID and hospitalization date. I would like to have an additional column stating the counts of admissions in the previous year - for example, column "previous year" in the table below.

patient_id hospitalization_date previous_year
1 Nov 2 2020 2
1 Dec 20 2019 1
1 Nov 30 2019 0
1 Jan 1 2015 0
2 April 17 2019 1
2 Nov 5 2018 0
question from:https://stackoverflow.com/questions/65938774/counting-instances-in-previous-year

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...