将宽表格式更改为长表格式并按y拆分日期

2024-05-16 10:00:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张这样的桌子:

temp = [['K98R', 'AB',34,'2010-07-27', '2013-08-17', '2008-03-01', '2011-05-02', 44],['S33T','ES',55, '2009-07-23', '2012-03-12', '2010-09-17', '', 76]]
Data = pd.DataFrame(temp,columns=['ID','Initials','Age', 'Entry','Exit','Event1','Event2','Weight'])

你在上表中看到的是,有一个进入和退出日期,事件1和2的日期,第二个病人的事件2也有一个丢失的日期,因为事件没有发生。还要注意,第一个患者的事件1发生在入职日期之前。你知道吗

我要达到的目标有两个: 1把进出的时间分成几年 2将宽格式转换为长格式,每年一行 三。检查事件1和事件2是否在每行中包含的时间段内发生

为了进一步解释,这里是我要尝试的输出。你知道吗

ID    Initial   Age   Entry       Exit     Event1   Event2 Weight
K89R    AB       34 27/07/2010  31/12/2010  1       0       44
K89R    AB       35 1/01/2011   31/12/2011  1       1       44 
K89R    AB       36 1/01/2012   31/12/2012  1       1       44
K89R    AB       37 1/01/2013   17/08/2013  1       1       44
S33T    ES       55 23/07/2009  31/12/2009  0       0       76
S33T    ES       56 1/01/2010   31/12/2010  1       0       76
S33T    ES       57 1/01/2011   31/12/2011  1       0       76
S33T    ES       58 1/01/2012   12/03/2012  1       0       76

这里您注意到的是,从进入到退出的日期期间被拆分为每个患者的单独行,每个行代表一年。事件列现在被编码为0(表示事件尚未发生)或1(事件已发生),然后由于事件已发生而被转入之后的年份。你知道吗

随着时间的推移,每排患者的年龄都会增加

患者ID和初始值以及体重保持不变。你知道吗

谁能帮我一下吗,谢谢


Tags: 患者idageabes时间exit事件
1条回答
网友
1楼 · 发布于 2024-05-16 10:00:48

从获取进入和退出之间的年数开始:

# Convert to datetime
df.Entry = pd.to_datetime(df.Entry)
df.Exit = pd.to_datetime(df.Exit)
df.Event1 = pd.to_datetime(df.Event1)
df.Event2 = pd.to_datetime(df.Event2)
# Round up, to include the upper years 
import math
df['Years_Between'] = (df.Exit - df.Entry).apply(lambda x: math.ceil(x.days/365))

# printing the df will provide the following:

    ID  Initials    Age Entry   Exit    Event1  Event2  Weight  Years_Between
0   K98R    AB  34  2010-07-27  2013-08-17  2008-03-01  2011-05-02  44  4
1   S33T    ES  55  2009-07-23  2012-03-12  2010-09-17  NaT 76  3

循环浏览数据并为每年创建新行:

new_data = []

for idx, row in df.iterrows():  

  year  = row['Entry'].year
  new_entry = pd.to_datetime(year,  format='%Y')

  for y in range(row['Years_Between']):

    new_entry = new_entry + pd.DateOffset(years=1)
    new_exit = new_entry + pd.DateOffset(years=1) - pd.DateOffset(days=1)

    record = {'Entry': new_entry,'Exit':new_exit}

    if row['Entry']> new_entry:
      record['Entry'] = row['Entry']

    if row['Exit']< new_exit:
      record['Exit'] = row['Exit']

    for col in ['ID', 'Initials', 'Age', 'Event1', 'Event2', 'Weight']:
      record[col] = row[col]

    new_data.append(record)

创建一个新的数据帧,比较日期:

df_new = pd.DataFrame(new_data, columns = ['ID','Initials','Age', 'Entry','Exit','Event1','Event2','Weight'])
df_new['Event1'] = (df_new.Event1 <= df_new.Exit).astype(int)
df_new['Event2'] = (df_new.Event2 <= df_new.Exit).astype(int)

# printing df_new will provide:
    ID  Initials    Age Entry   Exit    Event1  Event2  Weight
0   K98R    AB  34  2011-01-01  2011-12-31  1   1   44
1   K98R    AB  34  2012-01-01  2012-12-31  1   1   44
2   K98R    AB  34  2013-01-01  2013-08-17  1   1   44
3   K98R    AB  34  2014-01-01  2013-08-17  1   1   44
4   S33T    ES  55  2010-01-01  2010-12-31  1   0   76
5   S33T    ES  55  2011-01-01  2011-12-31  1   0   76
6   S33T    ES  55  2012-01-01  2012-03-12  1   0   76

相关问题 更多 >