Analyzing FHIR Data in a Tabular Format With Python
Learning objectives
Understand the high-level approaches for converting FHIR-formatted data into tabular data for analysis in Python.
Learn how to request data from a FHIR server and creating tidy tabular data tables using the FHIR-PYrate library.
Introduction
For the best learning experience, run this tutorial interactively, via one of the environment setup options. Use the above button depending on your chosen setup option.
Data analysis approaches in Python often use Pandas DataFrames to store tabular data. There are two primary approaches to loading FHIR-formatted data into Pandas DataFrames:
Writing Python code to manually convert FHIR instances in JSON format into DataFrames.
This does not require any special skills beyond data manipulation in Python, but in practice can be laborious (especially with large number of data elements) and prone to bugs.
Using a purpose-built library like FHIR-PYrate to automatically convert FHIR instances into DataFrames.
It is recommended to try this approach first, and only fall back to (1) if needed.
In this tutorial, we’re using a FHIR server located at http://localhost:8080/fhir but any FHIR server loaded with appropriate data can be used. For instructions on setting up your own test server, see Standing up a FHIR Testing Server.
Learning Paths
This tutorial offers three difficulty levels to accommodate different experience levels:
You can follow the tutorial sequentially or jump to the section that matches your experience level.
Retrieving FHIR data (Beginner Level)
In this section, you’ll learn how to:
Connect to a FHIR server
Retrieve basic patient data
Convert FHIR resources to a Pandas DataFrame
Tip 1: Beginner Level Validation Checklist
Your setup is successful if you can confirm:
Dependencies install without errors
FHIR server connection established (status 200)
DataFrame displays patient data
Common issues:
Package installation errors: Check Python version (3.8+ required)
Empty DataFrame: Check search parameters
Check the server connection.
# Load dependencyimport requests, osfhir_server = os.environ.get('FHIR_SERVER')print(f"Using FHIR server: {fhir_server}")# Check if the server is running and connection is successfulresponse = requests.get(f"{fhir_server}/metadata")print(f"Server status: {response.status_code}")
Using FHIR server: http://localhost:8080/fhir
Server status: 200
Understanding the FHIR Metadata Endpoint
The metadata endpoint (/metadata) is a special FHIR endpoint that returns the server’s capability statement - a structured document that describes what the server can do. When we query this endpoint:
We’re checking if the server is responsive (status code 200)
We’re verifying it’s a valid FHIR server
The response contains details about supported resources, operations, and search parameters
This is a lightweight way to validate connectivity before attempting more complex queries.
If connection to the server is successful (code 200), proceed with the next code block to pull data from the server.
# Load dependenciesfrom fhir_pyrate import Pirateimport pandas as pd# Instantiate a Pirate object using the FHIR-PYrate library to query the serversearch = Pirate( auth=None, # Pass the configured session base_url=fhir_server, print_request_url=True,)# Use the whimsically named `steal_bundles()` method# to instantiate a search interaction# For more information, see https://github.com/UMEssen/FHIR-PYrate/#piratebundles = search.steal_bundles( resource_type="Patient", request_params={"_count": 10, # Get 10 instances per page }, num_pages=1, # Get 1 page (so a total of 10 instances))# Execute the search and convert to a Pandas DataFramedf = search.bundles_to_dataframe(bundles)df.head(5)
If successful, you should see a DataFrame with multiple columns containing patient information. Common columns include:
identifier_0_value: Patient ID
gender: Patient gender
birthDate: Patient date of birth
name_0_family: Patient family name
If you don’t see this structure, review the validation checklist in Tip 1.
It is easier to see the contents of this DataFrame by printing out its first row vertically:
# Print the first row of the DataFrame vertically for easier reading.pd.set_option("display.max_rows", 100) # Show all rowsdf.head(1).T
0
resourceType
Patient
id
837e80f6-a7a5-77f8-36aa-c7b8ff002c4b
meta_versionId
1
meta_lastUpdated
2025-05-22T21:40:48.562+00:00
meta_source
#PqX7rAkKbpo7HbGt
meta_profile_0
http://hl7.org/fhir/us/core/StructureDefinitio...
text_status
generated
text_div
<div xmlns="http://www.w3.org/1999/xhtml">Gene...
extension_0_url
http://hl7.org/fhir/us/core/StructureDefinitio...
extension_0_extension_0_url
ombCategory
extension_0_extension_0_valueCoding_system
urn:oid:2.16.840.1.113883.6.238
extension_0_extension_0_valueCoding_code
2106-3
extension_0_extension_0_valueCoding_display
White
extension_0_extension_1_url
text
extension_0_extension_1_valueString
White
extension_1_url
http://hl7.org/fhir/us/core/StructureDefinitio...
extension_1_extension_0_url
ombCategory
extension_1_extension_0_valueCoding_system
urn:oid:2.16.840.1.113883.6.238
extension_1_extension_0_valueCoding_code
2186-5
extension_1_extension_0_valueCoding_display
Non Hispanic or Latino
extension_1_extension_1_url
text
extension_1_extension_1_valueString
Non Hispanic or Latino
extension_2_url
http://hl7.org/fhir/StructureDefinition/patien...
extension_2_valueString
Dallas143 Hirthe744
extension_3_url
http://hl7.org/fhir/us/core/StructureDefinitio...
extension_3_valueCode
M
extension_4_url
http://hl7.org/fhir/StructureDefinition/patien...
extension_4_valueAddress_city
Nice
extension_4_valueAddress_state
Provence-Alpes-Cote d'Azur
extension_4_valueAddress_country
FR
extension_5_url
http://synthetichealth.github.io/synthea/disab...
extension_5_valueDecimal
5.530027
extension_6_url
http://synthetichealth.github.io/synthea/quali...
extension_6_valueDecimal
74.469973
identifier_0_system
https://github.com/synthetichealth/synthea
identifier_0_value
837e80f6-a7a5-77f8-36aa-c7b8ff002c4b
identifier_1_type_coding_0_system
http://terminology.hl7.org/CodeSystem/v2-0203
identifier_1_type_coding_0_code
MR
identifier_1_type_coding_0_display
Medical Record Number
identifier_1_type_text
Medical Record Number
identifier_1_system
http://hospital.smarthealthit.org
identifier_1_value
837e80f6-a7a5-77f8-36aa-c7b8ff002c4b
identifier_2_type_coding_0_system
http://terminology.hl7.org/CodeSystem/v2-0203
identifier_2_type_coding_0_code
SS
identifier_2_type_coding_0_display
Social Security Number
identifier_2_type_text
Social Security Number
identifier_2_system
http://hl7.org/fhir/sid/us-ssn
identifier_2_value
999-66-1459
identifier_3_type_coding_0_system
http://terminology.hl7.org/CodeSystem/v2-0203
identifier_3_type_coding_0_code
DL
identifier_3_type_coding_0_display
Driver's License
identifier_3_type_text
Driver's License
identifier_3_system
urn:oid:2.16.840.1.113883.4.3.25
identifier_3_value
S99915912
identifier_4_type_coding_0_system
http://terminology.hl7.org/CodeSystem/v2-0203
identifier_4_type_coding_0_code
PPN
identifier_4_type_coding_0_display
Passport Number
identifier_4_type_text
Passport Number
identifier_4_system
http://standardhealthrecord.org/fhir/Structure...
identifier_4_value
X42393955X
name_0_use
official
name_0_family
Weber641
name_0_given_0
Louie190
name_0_prefix_0
Mr.
telecom_0_system
phone
telecom_0_value
555-814-1743
telecom_0_use
home
gender
male
birthDate
1922-11-05
deceasedDateTime
2003-07-11T12:20:54-04:00
address_0_extension_0_url
http://hl7.org/fhir/StructureDefinition/geoloc...
address_0_extension_0_extension_0_url
latitude
address_0_extension_0_extension_0_valueDecimal
42.088105
address_0_extension_0_extension_1_url
longitude
address_0_extension_0_extension_1_valueDecimal
-70.678992
address_0_line_0
121 Durgan Boulevard Unit 90
address_0_city
Green Harbor-Cedar Crest
address_0_state
MA
address_0_country
US
maritalStatus_coding_0_system
http://terminology.hl7.org/CodeSystem/v3-Marit...
maritalStatus_coding_0_code
S
maritalStatus_coding_0_display
S
maritalStatus_text
S
multipleBirthBoolean
False
communication_0_language_coding_0_system
urn:ietf:bcp:47
communication_0_language_coding_0_code
fr-FR
communication_0_language_coding_0_display
French (France)
communication_0_language_text
French (France)
address_0_postalCode
NaN
If you look at the output above, you can see FHIR-PYrate collapsed the hierarchical FHIR data structure into DataFrame columns. FHIR-PYrate does this by taking an element from the FHIR-formatted data like Patient.identifier[0].value and converting to an underscore-delimited column name like identifier_0_value. (Note that Patient.identifier has multiple values in the FHIR data, so there are multiple identifier_N_... columns in the DataFrame.)
This mapping allows you to access nested FHIR data using familiar DataFrame operations.
Selecting specific columns (Intermediate Level)
Tip 3: Intermediate Skills Check
Before proceeding with this section, ensure you can:
Understand FHIR resource structure
Work with basic DataFrame operations
Read FHIRPath syntax
Practice Exercise:
Try modifying the previous code to only retrieve patient names and birth dates.
Usually not every single value from a FHIR instance is needed for analysis. There are two ways to get a more concise DataFrame:
Use the approach above to load all elements into a DataFrame, remove the unneeded columns, and rename the remaining columns as needed. The process_function capability in FHIR-PYrate allows you to integrate this approach into the bundles_to_dataframe() method call.
Use FHIRPath to select specific elements and map them onto column names.
The second approach is typically more concise. For example, to generate a DataFrame like this…
id
gender
date_of_birth
marital_status
…
…
…
…
…you could use the following code:
# Instantiate and perform the FHIR search interaction in a single function calldf = search.steal_bundles_to_dataframe( resource_type="Patient", request_params={"_count": 10, # Get 10 instances per page }, num_pages=1, # Get 1 page (so a total of 10 instances) fhir_paths=[ ("id", "identifier[0].value"), ("gender", "gender"), ("date_of_birth", "birthDate"), ("marital_status", "maritalStatus.coding[0].code"), ],)df
Data types are appropriate (e.g., dates for birthDate)
No errors in FHIRPath expressions
While FHIRPath can be quite complex, its use in FHIR-PYrate is often straightforward. Nested elements are separated with ., and elements with multiple sub-values are identified by [N] where N is an integer starting at 0.
Examples illustrating the relationship between FHIRPath and DataFrame column names:
When using FHIRPath, maritalStatus.coding[0].code refers to the same data that appears in the column named maritalStatus_coding_0_code in the full DataFrame output. The [0] indicates it’s the first coding in the maritalStatus array.
Similarly, in the DataFrame output we saw a column identifier_3_type_coding_0_system which corresponds to the FHIRPath expression identifier[3].type.coding[0].system. This refers to the system identifier for the type of the fourth identifier (arrays are zero-indexed).
The element paths can typically be constructed by looking at the hierarchy resource pages in the FHIR specification, or by examining the column names in a full DataFrame output and converting the underscore notation to FHIRPath notation.
See Key FHIR Resources for more information on reading the FHIR specification.
Working with Multiple Resources (Advanced Level)
In this section, you’ll learn techniques for working with multiple FHIR resources simultaneously - a common requirement for clinical data analysis. Building on the previous sections, we’ll explore:
Handling elements with multiple values
Retrieving and linking related resources using _include and _revinclude parameters
Creating more targeted queries with resource-specific filters
Elements with multiple sub-values
There are multiple identifier[N].value values for each instance of Patient in this dataset.
# Instantiate and perform the FHIR search interaction in a single function calldf = search.steal_bundles_to_dataframe( resource_type="Patient", request_params={"_count": 10, # Get 10 instances per page }, num_pages=1, # Get 1 page (so a total of 10 instances) fhir_paths=[("id", "identifier[0].value"), ("identifiers", "identifier.value")],)df
This will give you separate identifier_0, identifier_1, … columns for each Patient.identifier[N] value.
Retrieving multiple resource types
FHIR-PYrate supports working with multiple resource types in a single query using the _include or _revinclude parameters. This allows you to retrieve related resources in a single API call.
Azure FHIR API limits _include and _revinclude parameters to 100 items. See the Azure documentation for more details.
Using _revinclude with FHIRPath
In this example, we retrieve Patient resources along with related Observation resources, and we use FHIRPath to select specific fields from each resource type:
# Retrieve patients and related observationsdfs = search.steal_bundles_to_dataframe( resource_type="Patient", request_params={# Get instances of Observation where `Observation.patient` refers to a fetched Patient instance"_revinclude": "Observation:patient","_count": 10, # Get 10 instances per page }, num_pages=1, # Get 1 page (so a total of 10 instances) fhir_paths=[# Common paths that could appear in either resource ("id", "id"),# Patient-specific paths ("patient_name", "name[0].family"), ("birth_date", "birthDate"),# Observation-specific paths ("observation_code", "code.coding[0].code"), ("observation_value", "valueQuantity.value"), ("observation_unit", "valueQuantity.unit") ])# `dfs` is a dictionary where the key is the FHIR resource type, and the value is the DataFrame# Split these into separate variables for easy access:df_patients = dfs["Patient"]df_observations = dfs["Observation"]# Each DataFrame will only contain columns relevant to its resource type# Empty columns are automatically removed from each DataFrameprint(f"Patient columns: {df_patients.columns.tolist()}")print(f"Observation columns: {df_observations.columns.tolist()}")# Look at the first row of each DataFramedf_patients.head(1)df_observations.head(1)
Sometimes you need more fine-grained control over how related resources are queried. In these cases, you can use trade_rows_for_dataframe to retrieve related resources based on data in an existing DataFrame:
df_observations2 = search.trade_rows_for_dataframe( df_patients, resource_type="Observation", request_params={"_count": "10", # Get 10 instances per page }, num_pages=1,# Load Observations where `Observation.subject` references the instance of Patient# identified by `id` in the `df_patients` DataFrame df_constraints={"subject": "id"}, fhir_paths=[ ("observation_id", "id"), ("patient", "subject.reference"), ("status", "status"), ("code", "code.coding[0].code"), ("code_display", "code.coding[0].display"), ("value", "valueQuantity.value"), ("value_units", "valueQuantity.unit"), ("datetime", "effectiveDateTime"), ],)# Look at the resultsdf_observations2.head(5)
The trade_rows_for_dataframe approach offers several advantages:
More precise control over query parameters for each related resource
Ability to process patient data row by row, useful for large datasets
Option to retain columns from the original DataFrame using the with_ref parameter
Filtering by resource attributes
When querying resources, you often need to filter by specific attributes. For example, you might want to retrieve all smoking status observations:
# Directly search for smoking status observationsdf_observations2 = search.steal_bundles_to_dataframe( resource_type="Observation", request_params={"code": "http://loinc.org|72166-2", # LOINC code for smoking status"_count": 20, # Get more observations since we're not limiting by patient }, num_pages=1, fhir_paths=[ ("observation_id", "id"), ("patient", "subject.reference"), ("status", "status"), ("code", "code.coding[0].code"), ("code_display", "code.coding[0].display"), ("value", "valueCodeableConcept.coding[0].code"), ("value_display", "valueCodeableConcept.coding[0].display"), ("datetime", "effectiveDateTime"), ],)# Look at the first row of the Observations DataFramedf_observations2.head(15)
Note that when retrieving Observation resources, you’ll need to choose the appropriate data type for Observation.value[x] based on the type of observation. For quantitative observations, use valueQuantity.value, but for coded observations (like smoking status), use valueCodeableConcept.coding[0].code.
Summary and Next Steps
This tutorial has covered:
Beginner level: Connecting to a FHIR server and retrieving basic patient data
Intermediate level: Using FHIRPath to select specific columns and create focused DataFrames
Advanced level: Working with multiple resources, handling nested data, and performing filtered queries
To continue your learning:
Experiment with different resource types beyond Patient and Observation
Try more complex FHIRPath expressions to extract specific data elements
Combine data from multiple resources for comprehensive clinical analysis
Build visualization and analysis workflows with the retrieved data