【发布时间】:2019-11-18 16:35:22
【问题描述】:
我有一个超过4 million rows and 30 columns 的数据框。我只是提供我的患者数据框的样本
df = pd.DataFrame({
'subject_ID':[1,1,1,1,1,2,2,2,2,2,3,3,3],
'date_visit':['1/1/2020 12:35:21','1/1/2020 14:35:32','1/1/2020 16:21:20','01/02/2020 15:12:37','01/03/2020 16:32:12',
'1/1/2020 12:35:21','1/3/2020 14:35:32','1/8/2020 16:21:20','01/09/2020 15:12:37','01/10/2020 16:32:12',
'11/01/2022 13:02:31','13/01/2023 17:12:31','16/01/2023 19:22:31'],
'item_name':['PEEP','Fio2','PEEP','Fio2','PEEP','PEEP','PEEP','PEEP','PEEP','PEEP','Fio2','Fio2','Fio2']})
我想做两件事
1) 找出序列中缺失的主题及其记录
2) 获取每个主题的item_name 计数
对于q2,这是我尝试过的
df.groupby(['subject_ID','item_name']).count() # though this produces output, column name is not okay. I mean why do it show the count value on `date_visit` column?
对于q1,这就是我正在尝试的
df['day'].le(df['shift_date'].add(1))
我希望我的输出如下所示
【问题讨论】:
-
缺少日期的定义是什么?
-
例如。
subject_id = 2 has records only for dates 1,3,8,9,10。通过这个我们可以推断出他的日期2,4,5,6,7的记录丢失了。 -
如果您看到
subject_id = 1 you can see that he/she has records continuously. There is no break in between their dates。这就是为什么Seq_status = Yes表明他/她在序列中 -
@Datanovice - 更新了示例数据框和预期输出。有微小的变化。
-
@SSMK 你想要丢失的日期还是只想要它们的总数?
标签: python python-3.x pandas dataframe pandas-groupby