Skip to main content

Tools for working with icd codes and comorbidities

Project description

Latest PyPI version Latest Travis CI build status Coverage

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 to send me an email.


Basic usage includes two very common tasks while dealing with icd code data.

  • Transforming datasets from a long to wide format
  • Processing icd codes for known comorbidity mappings

Transforming from long to wide

Data is commonly in a long format that may have a key for an individual such as person_id with many claims claim_id belonging to it.

For example:

claim_id person_id icd_cd_1 icd_cd_2 icd_cd_3
001 A code_6 code_2  
002 A code_8    
003 A code_3 code_2 code_6
004 B code_1    
005 B code_2 code_3  
006 C code_4 code_2 code_5

For easier processing, we must transform the table into a more collapsed version. The number of icd columns then becomes the maximum unique codes for any given person_id.

Such as:

person_id icd_cd_1 icd_cd_2 icd_cd_3 icd_cd_4
A code_6 code_2 code_8 code_3
B code_1 code_2 code_3  
C code_4 code_2 code_5  

To accomplish this task, simply use the function long_to_short_transformation as such:

import pandas as pd
import icd

data = {"person_id":[1,1,1,2,2,3],
df = pd.DataFrame.from_dict(data)

Where df is 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, it must still be an array. Also, you must impute NaN values to be an empty string such as “”.

The function will return a new dataframe with index of person_id, a column of person_id, as well as as many unique columns as needed in the following form icd_0, icd_1, … , icd_n.

Processing icd codes to known comorbidities

The second task has to do with actually mapping comorbidities to these icd codes. For this, you can use the function icd_to_comorbidities. This can be seen from going from a table of the format:

person_id icd_cd_1 icd_cd_2 icd_cd_3 icd_cd_4
A code_6 code_2 code_8 code_3
B code_1 code_2 code_3  
C code_4 code_2 code_5  

To the format:

person_id comorb_1 comorb_2 comorb_3 comorb_4
A True False True True
B False True False False
C False False False False

This comorbidity mapping is pending on the mapping used.

An example of doing is is carried out as such:

import pandas as pd
import icd

df = 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 the quan_elixhauser10, which is a transcription by Quan of the original Elixhauser icd 9 comorbidities in the following paper.

Optionally, you can provide a mapping keyword 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 code here.

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'],
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 the person_id values as the index, a column of whatever “person_id” string is passed in, along with a column for every comorbidity populated with either True or False.


icd can easily be downloaded from Pypi package index via the following:

pip install icd



icd currently supports Python 3.4, 3.5, and 3.6




icd was written by Mark Hoffmann.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

icd-0.1.3.tar.gz (8.4 kB view hashes)

Uploaded source

Built Distribution

icd-0.1.3-py3-none-any.whl (12.8 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page