Compare inpatient hospital activity and payments in Python
Look up the same inpatient group at Duke, UNC and WakeMed, with discharge counts and separate total and Medicare payments.
Compare an inpatient group across hospitals
You have hospital identifiers and want to compare their reported activity for the same type of inpatient stay. This example selects Duke University Hospital, UNC Hospitals and WakeMed for MS-DRG 470: major hip and knee joint replacement or reattachment of a lower extremity without a major complication or comorbidity. It returns discharge counts and average total and Medicare payments.
The optional input.csv contains three genuine hospital CMS Certification Numbers, or CCNs. The same selections appear in the roster list in the Python code below. No hospital observations are invented.
Download hospital_services.csv into your working folder. This genuine subset of the purchased file contains every DRG 470 record for these three hospitals, retaining all 17 fields. It is selected for this comparison; it does not include the hospitals’ other inpatient groups.
The full $29 reference contains 145,879 hospital/group records, 2,906 hospitals and 540 MS-DRGs from calendar 2024 Original Medicare activity. Review the complete product and coverage.
Code example
This example uses Python’s standard library and runs in a notebook cell, IPython or a script in the folder containing the CSV. It reads one row at a time and keeps the selected hospitals, year and inpatient group.
Paths are relative to your working folder; use from pathlib import Path; print(Path.cwd()) to check it, or supply full paths.
import csv
from decimal import Decimal
# These are real CMS hospital identifiers. Replace this list with your own roster.
roster = [{"hospital_ccn": "340030"}, {"hospital_ccn": "340061"}, {"hospital_ccn": "340069"}]
year, drg_code = "2024", "470"
selected = {r["hospital_ccn"] for r in roster}
lookup = {}
with open("hospital_services.csv", newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
if row["year"] == year and row["drg_code"] == drg_code and row["hospital_ccn"] in selected:
if row["hospital_ccn"] in lookup:
raise ValueError("Duplicate hospital/year/DRG key")
lookup[row["hospital_ccn"]] = row
results = []
for item in roster:
row = lookup.get(item["hospital_ccn"])
result = dict(hospital_ccn=item["hospital_ccn"], status="published" if row else "not_in_file")
for field in ["hospital_name", "discharges", "average_total_payment", "average_medicare_payment"]:
result[field] = row[field] if row else ""
results.append(result)
if row:
print(f'{result["hospital_ccn"]} | {result["hospital_name"]} | {result["discharges"]} discharges | '
f'total ${Decimal(result["average_total_payment"]):,.2f} | Medicare ${Decimal(result["average_medicare_payment"]):,.2f}')
else:
print(f'{result["hospital_ccn"]} | not in this file; activity is unknown')
Replace roster with your own CCN strings or a CSV’s hospital_ccn column. Change drg_code when using the full purchased file. Preserve leading zeros in both identifiers.
Analysis of the output
340030 | Duke University Hospital | 128 discharges | total $20,913.18 | Medicare $16,180.77
340061 | Unc Hospitals | 51 discharges | total $23,765.08 | Medicare $17,251.29
340069 | Wakemed | 12 discharges | total $17,441.17 | Medicare $13,444.17
Duke, UNC and WakeMed have 128, 51 and 12 published discharges respectively for this group in 2024. These are inpatient discharges, not unique patients or all hip and knee procedures across every insurer and care setting. The result helps identify hospitals with published activity for the selected group.
The average total payments are $20,913.18, $23,765.08 and $17,441.17 per discharge. The Medicare-paid averages are $16,180.77, $17,251.29 and $13,444.17. Total payments also include patient responsibility and applicable third-party payments. The source’s separate covered-charge field represents billed charges, not either payment measure. The CSV retains more decimal precision than the printed output.
These historical averages can support a payment comparison, but the difference alone does not establish efficiency or quality. Payment components can include teaching, disproportionate-share, capital and outlier amounts; patient mix and other rules also matter. They are not current prices, hospital costs or patient quotes. Maryland’s payment arrangements differ from those of other states.
CMS omits hospital/group combinations with ten or fewer discharges. A different lookup that returns not_in_file therefore has unknown activity, not necessarily zero. Published groups cannot reconstruct complete hospital or national totals, and discharges should not be interpreted as distinct people. CMS’s separate summary files use unsuppressed underlying claims.
Using the full dataset
Extract the purchased ZIP and replace the sample hospital_services.csv with the full file. The code runs unchanged. Its main key is year + hospital_ccn + drg_code. Join hospitals.csv on year and CCN, and drg_groups.csv on year and DRG. A hospital has many groups; do not use its CCN alone as the activity table’s unique key.
Six CSV tables supply activity, hospital and group lookups, full source-record accounting, original-column mappings and the retained source edition. Dictionaries, quality reports, independent reconciliation, checksums and the verified example accompany them. All 15 original fields remain in the 17-field activity table.
The hospital and care-facility selection page separates activity from quality and other care settings. The separately priced Hospital Facility & Quality Reference adds hospital attributes and reported measures. Check the CCN, historical facility identity and each measure’s reporting period before joining; the quality reference is not a same-period quality score for these discharges.
Sources and versions
Source: CMS Medicare Inpatient Hospitals by Provider and Service, calendar 2024, modified April 23, 2026 and captured by ACD October 27, 2026 UTC. It describes Original Medicare Part A fee-for-service IPPS short-stay hospital activity, excluding Medicare Advantage.
CMS’s currently linked dictionary dates to May 2023, methodology to May 2024 and technical specifications to April 2023. The download documents a CCN field-name alias and an outpatient wording error in the PDF dictionary; the API dictionary and methodology identify this as inpatient data. Original observations and source labels remain unchanged.
The source and preparation record distinguishes these dates and ACD’s processing revision. The government source is free; ACD sells preparation, documentation, validation and delivery. Improvements to processing these retained inputs are included; genuinely new source editions may be sold separately. No automatic refresh is promised.
Example checked October 27, 2026 against both these samples and the complete purchased files. Source and processing records, sample checksums · Expected result CSV. Independent preparation; no agency endorsement.