Skip to content

Instantly share code, notes, and snippets.

@TavoloPerUno
Last active February 20, 2023 20:28
Show Gist options
  • Save TavoloPerUno/a65affedd4d76ecb725648e7a5086857 to your computer and use it in GitHub Desktop.
Save TavoloPerUno/a65affedd4d76ecb725648e7a5086857 to your computer and use it in GitHub Desktop.
"""This module has a list of functions that can be used to fetch NDCs matching
drug names using NLM Drug APIs"""
from typing import List
import requests
import pandas as pd
def get_rxcui_info(drug_name: str) -> pd.DataFrame:
"""
This function returns RxCUIs associated with drug names input to this
function. Drug name can be partial or full. The RxCUI is a unique,
unambiguous identifier that is assigned to an individual drug entity in
RxNorm and used to relate to all things associated with that drug.
Parameters
----------
drug_name : str
Name of the drug. Can be partial or full name with dosage.
Returns
-------
pd.DataFrame
"""
api_endpoint_drugs = "https://rxnav.nlm.nih.gov/REST/drugs.json"
dct_response = requests.get(
api_endpoint_drugs, params={"name": drug_name}
).json()
return pd.concat(
[
pd.DataFrame(
dct_concept_group.get("conceptProperties", []),
index=range(
len(dct_concept_group.get("conceptProperties", []))
),
)
for dct_concept_group in dct_response.get("drugGroup", {}).get(
"conceptGroup", []
)
]
or [pd.DataFrame()],
ignore_index=True,
)
def get_ndcs(rxcui: str) -> List[str]:
"""
Get list of NDCs for the input RxCUI
Parameters
----------
rxcui : str
RxCUI for which NDCs should be pulled
Returns
-------
List[str]
"""
ndc_url = f"https://rxnav.nlm.nih.gov/REST/rxcui/{rxcui}/ndcs.json"
url_response = requests.get(ndc_url).json()
return url_response.get("ndcGroup", {}).get("ndcList", {}).get("ndc", [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment