I have a dataset containing diagnosis columns (DIAGX1-DIAGX42) for patients and I need to create a variable that sums the values for these based on weights from an external index.
df_patients
patients = [('pat1', 'Z509', 'M33', 'M32', 'M315'),
('pat2', 'I099', 'I278', 'M05', 'F01'),
('pat3', 'N057', 'N057', 'N058', 'N057')]
labels = ['patient_num', 'DIAGX1', 'DIAGX2', 'DIAGX3', 'DIAGX4']
df_patients = pd.DataFrame.from_records(patients, columns=labels)
df_patients
Input
patient_num DIAGX1 DIAGX2 DIAGX3 DIAGX4
pat1 Z509 M33 M32 M315
pat2 I099 I278 M05 F01
pat3 N057 N057 N058 N057
Output
patient_num DIAGX1 DIAGX2 DIAGX3 DIAGX4 Score
pat1 Z509 M33 M32 M315 1
pat2 I099 I278 M05 F01 6
pat3 N057 N057 N058 N057 0
external_index, where if a column from the dataset above contains a value in any of the below that the value would be added. Only one member contributes to a value been given, e.g a value of both F01
, F02
both in dementia
will only result in 2
being allocated for that record/patient, values are only added/summed if they occur across grouped indexes e.g. F01
=2 and I099
=1 sum to 3
- congestive_heart_failure = 2
- dementia = 2
- chronic_pulmonary_disease= 1
- rheumatologic_disease = 1
congestive_heart_failure = [
"I099",
"I255",
"I420",
"I425",
"I426",
"I427",
"I428",
"I429",
"I43",
"I50",
"P290",
]
dementia = ["F01", "F02", "F03", "F051", "G30", "G311"]
chronic_pulmonary_disease = [
"I278",
"I279",
"J40",
"J41",
"J42",
"J43",
"J44",
"J45",
"J47",
"J60",
"J61",
"J62",
"J63",
"J64",
"J65",
"J66",
"J67",
"J684",
"J701",
"J703",
]
rheumatologic_disease = [
"M05",
"M06",
"M315",
"M32",
"M33",
"M34",
"M351",
"M353",
"M360",
]