Skip to main content

Health Data Downloader Package

The Health Data Downloader is a Python package that makes it easy to download and read real world health data.

Current data sources supported:

  • NHANES National Health and Nutrition Examination Survey (CDC)

It enables you to search, download and read data that also includes the meaning of codes and values, as well as storing categorical data with meaning such as Female/Male instead of 1/0.

The NHANES Submodule

If you want to download health data from NHANES this module can help you not only download the data but also add documentation from the official NHANES website, and read this data into a pandas dataframe.

NHANES is the National Health and Nutrition Examination Survey which collects data about the health of adults and children in the United States.

Note: This is not an official repository from the NHANES project nor the CDC

This project provides workflows that enable you to:

  • discover and download datasets from NHANES by component (meaning what type of data like Laboratory data, examination etc.) and by cycle (which years)
  • read the XPT files into a pandas DataFrames
  • extract NHANES variable documentation into CSV files and rename variable codes into human-readable names (such as heart_rate instead of LF321)
  • search and summarize local files if they are already downloaded

Quick start

For a runnable walkthrough of variable search, downloads, DataFrame documentation, and labeled categories, open the NHANES download notebook or run it in Google Colab.

For a focused laboratory example, use the hs-CRP notebook or open it in Colab.

In thie quick start we will:

  1. Install required packages.
  2. Run a download.
  3. Read and inspect data.
pip install healthdata

You can choose which components you want to download (such as Demographics, Laboratory etc.) and which years (can be either "2017-2020" or "2016"). This will also download documentation about these fields from the NHANES website to add them when the data is read. If the data is already downloaded in the output directory the module will log this and skip the download.

from healthdata.nhanes.downloader import download_nhanes_data

download_nhanes_data(
    components=["Demographics", "Examination"],
    output_dir="nhanes_data",
    years=["2017-2018"],
)

You can read this data now and inspect the official documentation directly in the dataframe:

from healthdata.nhanes import read_nhanes_data, get_column_doc

df = read_nhanes_data(
    "nhanes_data/2017-2018/2017/Demographics/DEMO_J.XPT",
    decode_categories=True,
    allow_download_prompt=False,
)
print(df["gender"].head())  # Male/Female, as a pandas categorical instead of 1/0
print(get_column_doc(df, "gender"))
print(df.attrs["documentation_df"].head())

You can also search the CDC catalogue for variables like "blood pressure" without downloading data files:

from healthdata.nhanes import search_variables

matches = search_variables("blood pressure", component="Examination", years=["2017-2018"])
print(matches[["variable_name", "description", "dataset", "cycle"]].head())

Or search your downloaded codebooks offline (local data):

matches = search_variables("gender", search_dir="nhanes_data")
print(matches[["variable_name", "dataset", "cycle", "data_path"]].head())

See the NHANES guide here for more info on access to docs, categorical data, and search options, or how it works for the data flow.

Current caveats

  • HTML codebook parsing depends on current NHANES page text patterns. CDC page structure changes may require parser updates.
  • Network errors and partial downloads are not retried automatically.

Upcoming Additions

Planned additions include NCBI GEO and other open health and biomedical datasets. The current release supports NHANES only.

License

This project is licensed under the MIT License. Commercial use is permitted under its terms. Downloaded datasets may be subject to separate terms set by their respective providers.

For paid support or consulting, contact the author at contact@ismail.bio.

Author

Ismail Elouafiq

Contact: contact@ismail.bio

Citing healthdata

If you use healthdata in your research, please cite it. Use the "Cite this repository" button on GitHub, or:

Elouafi

Metadata

Release files for healthdata 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for healthdata 0.1.3
File Size Uploaded
healthdata-0.1.3.tar.gz 21.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for healthdata 0.1.3
File Interpreter ABI Platform
healthdata-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 37.5 kB

Release files / healthdata-0.1.3.tar.gz

Download URL healthdata-0.1.3.tar.gz
Size 21.4 kB
Tags Source
SHA-256 checksum
How to use checksums
28d62952f52799a0063e9e5beb42b45f5414c02c587a952df7ba04c62a321e00
BLAKE2b-256 checksum
How to use checksums
05394e2bab9f609d13f782f108fc21cce141c0de1ce5d13e9435ab13971cf6f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.14

Release files / healthdata-0.1.3-py3-none-any.whl

Download URL healthdata-0.1.3-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e7db9d445d6f84e12354eaef35d063aedd3854e2b361066f202d6dc8cc1b6c01
BLAKE2b-256 checksum
How to use checksums
e6bc4c0df8d3aacc53dd9f0be5da0adc9dfb3ac72cc504c3549ab4f4d38adbb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page