Package: icd
Tools for working with icd codes and comorbidities. This was inspired by the R package,icd, as a simple python implementation for some of the base functionality. This has been benchmarked to be able to hand large datasets (tens of millions of rows) for various icd code manipulation tasks.If you would be interested in helping contribute to this repository, feel free tosend me an email.UsageBasic usage includes two very common tasks while dealing with icd code data.Transforming datasets from a long to wide formatProcessing icd codes for known comorbidity mappingsTransforming from long to wideData is commonly in a long format that may have a key for an individual such asperson_idwith many claimsclaim_idbelonging to it.For example:claim_idperson_idicd_cd_1icd_cd_2icd_cd_3001Acode_6code_2002Acode_8003Acode_3code_2code_6004Bcode_1005Bcode_2code_3006Ccode_4code_2code_5For easier processing, we must transform the table into a more collapsed version. The number oficdcolumns then becomes the maximum unique codes for any givenperson_id.Such as:person_idicd_cd_1icd_cd_2icd_cd_3icd_cd_4Acode_6code_2code_8code_3Bcode_1code_2code_3Ccode_4code_2code_5To accomplish this task, simply use the functionlong_to_short_transformationas such:importpandasaspdimporticddata={"person_id":[1,1,1,2,2,3],"dx_1":["F11","E40","","F32","C77","G10"],"dx_2":["F1P","E400","","F322","C737",""]}df=pd.DataFrame.from_dict(data)icd.long_to_short_transformation(df,"person_id",["dx_1","dx_2"])Wheredfis your pandas dataframe,“person_id”is the column you want to roll up on, and[“dx_1”,”dx_2”]is the array of columns that contain icd codes.It is important to note that even if you only have one icd column, itmust still be an array. Also, you mustimpute NaN valuesto be anempty stringsuch as “”.The function will return a new dataframe with index ofperson_id, a column ofperson_id, as well as as many unique columns as needed in the following formicd_0,icd_1, … ,icd_n.Processing icd codes to known comorbiditiesThe second task has to do with actually mapping comorbidities to these icd codes. For this, you can use the functionicd_to_comorbidities. This can be seen from going from a table of the format:person_idicd_cd_1icd_cd_2icd_cd_3icd_cd_4Acode_6code_2code_8code_3Bcode_1code_2code_3Ccode_4code_2code_5To the format:person_idcomorb_1comorb_2comorb_3comorb_4ATrueFalseTrueTrueBFalseTrueFalseFalseCFalseFalseFalseFalseThis comorbidity mapping is pending on the mapping used.An example of doing is is carried out as such:importpandasaspdimporticddf=pd.DataFrame.from_dict({'icd_0':{1:'F1P',2:'F322',3:''},'icd_1':{1:'F11',2:'C77',3:'G10'},'icd_2':{1:'',2:'C737',3:''},'icd_3':{1:'E400',2:'F32',3:''},'icd_4':{1:'E40',2:'',3:''},'person_id':{1:1,2:2,3:3}})icd.icd_to_comorbidities(df,"person_id",["icd_0","icd_1","icd_2","icd_3","icd_4"])The default default mapping is thequan_elixhauser10, which is a transcription by Quan of the original Elixhauser icd 9 comorbidities in thefollowing paper.Optionally, you can provide amappingkeyword argument as such:icd.icd_to_comorbidities(df,"person_id",["icd_0","icd_1","icd_2","icd_3","icd_4"],mapping="quan_elixhauser10")The currently supported mappings are the default“quan_elixhauser10”as well as the“charlson10”mapping as referenced from the same paper above. Additionally, you can find them laid out in SAS codehere.If you want to to create a custom comborbidity mapping, simply pass in a dict for the mapping argument instead of a supported keyword string. The dict must follow the following format as such:custom_mapping={"paraplegia_and_hemiplegia":['G81','G82','G041','G114','G801','G802','G830','G831','G832','G833','G834','G839'],"renal_disease":['N18','N19','N052','N053','N054','N055','N056','N057','N250','I120','I131','N032','N033','N034','N035','N036','N037','Z490','Z491','Z492','Z940','Z992'],"cancer":['C00','C01','C02','C03','C04','C05','C06','C07','C08','C09','C10','C11','C12','C13','C14','C15','C16','C17','C18','C19','C20','C21','C22','C23','C24','C25','C26','C30','C31','C32','C33','C34','C37','C38','C39','C40','C41','C43','C45','C46','C47','C48','C49','C50','C51','C52','C53','C54','C55','C56','C57','C58','C60','C61','C62','C63','C64','C65','C66','C67','C68','C69','C70','C71','C72','C73','C74','C75','C76','C81','C82','C83','C84','C85','C88','C90','C91','C92','C93','C94','C95','C96','C97'],"moderate_or_sever_liver_disease":['K704','K711','K721','K729','K765','K766','K767','I850','I859','I864','I982'],"metastitic_carcinoma":['C77','C78','C79','C80'],"aids_hiv":['B20','B21','B22','B24']}icd.icd_to_comorbidities(df,"person_id",["icd_0","icd_1","icd_2","icd_3","icd_4"],mapping=custom_mapping)The above function returns a new DataFrame with theperson_idvalues as the index, a column of whatever “person_id” string is passed in, along with a column for every comorbidity populated with eitherTrueorFalse.Installationicd can easily be downloaded from Pypi package index via the following:pipinstallicdRequirementspandasCompatibilityicd currently supports Python 3.4, 3.5, and 3.6LicenceMITAuthorsicdwas written byMark Hoffmann.