我有一个数据帧和dict,如下所示
df = pd.DataFrame({
'subject_id':[1,2,3,4,5],
'age':[42,56,75,48,39],
'date_visit':['1/1/2020','3/3/2200','13/11/2100','24/05/2198','30/03/2071'],
'a11fever':['Yes','No','Yes','Yes','No'],
'a12diagage':[36,np.nan,np.nan,40,np.nan],
'a12diagyr':[np.nan,np.nan,2091,np.nan,np.nan],
'a12diagyrago':[6,np.nan,9,np.nan,np.nan],
'a20cough':['Yes','No','No','Yes','No'],
'a21cough':[np.nan,'Yes',np.nan,np.nan,np.nan],
'a22agetold':[37,np.nan,np.nan,46,np.nan],
'a22yrsago':[np.nan,6,np.nan,2,np.nan],
'a22yrtold':[np.nan,2194,np.nan,np.nan,np.nan]
})
df['date_visit'] = pd.to_datetime(df['date_visit'])
disease_dict = {'a11fever' : 'fever', 'a20cough' : 'cough','a21cough':'cough'}
此数据框包含有关患者医疗状况和诊断日期的信息
但是正如您所看到的,诊断日期不是直接可用的并且我们必须根据包含诸如age
、yr
、ago
、diag
等关键字的列来推导它,这些关键字出现在条件列的下一个5-6列中(例如:a11fever
)。在这个条件列之后查找接下来的5列,您将能够获得派生条件所需的信息日期。相似对于其他条件,如cough
我希望我的输出如下所示
我试过下面的方法,但没用
df = df[(df['a11fever'] =='Yes') | (df['a20cough'] =='Yes') | (df['a21cough'] =='Yes')]
# we filter by `Yes` above because we only nned to get dates for people who had medical condition (`fever`,`cough`)
df.fillna(0,inplace=True)
df['diag_date'] = df["date_visit"] - pd.DateOffset(years=df.filter('age'|'yr'|'ago')) # doesn't help throws error. need to use regex here to select non-na values any of other columns
pd.wide_to_long(df, stubnames=['condition', 'diag_date'], i='subject_id', j='grp').sort_index(level=0)
df.melt('subject_id', value_name='valuestring').sort_values('subject_id')
请注意,我知道疾病的列名。我不知道的是实际的列名,从中我可以获得导出日期所需的信息。但我知道它包含像age
、ago
、yr
、diag
这样的关键字
diag_date
是通过从date_vist
列中减去derived date
得到的。你知道吗
规则屏幕截图
例如:subject_id = 1
因发烧于1/1/2020
到医院就诊,他在36
(a12diagage
)或6
年前(a12diagyrago
)被诊断出发烧。我们知道他现在的年龄和访问日期,所以我们可以选择从任何一列中减去
如您所见,我无法找到如何基于regex选择列并减去它
用途:
相关问题 更多 >
编程相关推荐