Compare outpatient hospital activity and payments in Python
Look up the same comprehensive APC at Duke, UNC and WakeMed, keeping regular payments, outliers and source blanks distinct.
Compare an outpatient group across hospitals
You have a hospital roster and want to compare reported activity for a particular outpatient service group. This example looks up APC 5115, Level 5 Musculoskeletal Procedures, at Duke University Hospital, UNC Hospitals and WakeMed. It returns services, beneficiaries and historical payment averages from calendar 2024.
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 APC 5115 record for those hospitals, retaining all 23 fields. It does not include their other outpatient groups.
The full $29 reference contains 116,182 hospital/C-APC records across 3,126 hospitals and 72 comprehensive groups. Service counts are available in 63,518 records; the remaining rows retain the source’s blank activity values. Review the complete product and coverage.
Code example
Run this standard-library Python code in a notebook cell, IPython or a script beside the CSV. It reads one row at a time and selects the same year and APC for each hospital.
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
# Genuine hospital selections; replace with your own CCN strings or CSV roster.
roster = [{"hospital_ccn": "340030"}, {"hospital_ccn": "340061"}, {"hospital_ccn": "340069"}]
year, apc_code = "2024", "5115" # Level 5 Musculoskeletal Procedures
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["apc_code"] == apc_code and row["hospital_ccn"] in selected:
if row["hospital_ccn"] in lookup:
raise ValueError("Duplicate hospital/year/APC key")
lookup[row["hospital_ccn"]] = row
def amount(value):
return f"${Decimal(value):,.2f}" if value != "" else "not reported"
results = []
for item in roster:
row = lookup.get(item["hospital_ccn"])
result = dict(hospital_ccn=item["hospital_ccn"], status="in_file" if row else "not_in_file")
for field in ["hospital_name", "beneficiaries", "services", "average_regular_allowed",
"average_regular_medicare_payment", "outlier_services", "average_outlier_payment",
"beneficiaries_status", "activity_status", "outlier_status"]:
result[field] = row[field] if row else ""
results.append(result)
if row:
print(f'{result["hospital_ccn"]} | {result["hospital_name"]}')
print(f' Services: {result["services"] or "not reported"}; beneficiaries: {result["beneficiaries"] or "not reported"}')
print(f' Regular allowed/service: {amount(result["average_regular_allowed"])}; '
f'Medicare/service: {amount(result["average_regular_medicare_payment"])}')
print(f' Outlier services: {result["outlier_services"] or "not reported"}; '
f'average among outlier services: {amount(result["average_outlier_payment"])}')
else:
print(f'{result["hospital_ccn"]} | not in this file; activity is unknown')
Replace roster with your CCN strings or the hospital_ccn column from your own CSV. Change apc_code when using the full purchased file. Keep identifiers as text.
Analysis of the output
340030 | Duke University Hospital
Services: 518; beneficiaries: 501
Regular allowed/service: $11,678.03; Medicare/service: $10,045.95
Outlier services: 24; average among outlier services: $4,040.71
340061 | Unc Hospitals
Services: 101; beneficiaries: 100
Regular allowed/service: $11,386.24; Medicare/service: $9,753.63
Outlier services: 0; average among outlier services: $0.00
340069 | Wakemed
Services: 141; beneficiaries: 139
Regular allowed/service: $11,545.88; Medicare/service: $9,910.48
Outlier services: not reported; average among outlier services: not reported
Duke, UNC and WakeMed report 518, 101 and 141 services for this group. Their beneficiary counts are 501, 100 and 139. These counts help identify reported outpatient activity; services and beneficiaries are different measures, and the same person can appear in other groups or hospitals.
Regular allowed payments average $11,678.03, $11,386.24 and $11,545.88 per service. These include Medicare and beneficiary cost sharing. The separate regular Medicare-paid averages exclude beneficiary cost sharing. Neither regular measure includes special outlier payments. The source’s estimated-charge field represents billed charges, not either payment measure.
Duke reports 24 outlier services, with an average additional outlier payment of $4,040.71 among those services. UNC reports zero outlier services. WakeMed’s outlier fields are blank, so its outlier result is not reported, not zero. Do not add the outlier average directly to the regular-payment average: one denominator is outlier-paid services and the other is all services in the group.
The figures retain their original precision in the CSV and are rounded only for display. Differences between historical averages do not establish hospital quality, efficiency or a current patient price. Hospital payment characteristics and patient mix matter.
In the full file, source_blank flags identify unavailable original cells; reported includes numeric zero. CMS describes privacy suppression, but the data do not disclose a precise reason or hidden count for every blank. An absent hospital/APC combination is also unknown. Do not fill either case with zero or sum beneficiaries across groups as unique patients.
Using the full dataset
Extract the purchased ZIP and replace the sample hospital_services.csv with the full file. The code runs unchanged. Its unique key is year + hospital_ccn + apc_code. Join hospitals.csv on year and CCN, and apc_groups.csv on year and APC.
Six CSV tables supply activity, hospital and group references, source-record accounting, field mappings and the retained edition. Dictionaries, availability definitions, checksums, independent reconciliation and this example accompany the download. Hospital and APC summaries show reported and blank group coverage alongside partial service totals.
The hospital and care-facility selection page distinguishes this outpatient reference from inpatient discharge activity and hospital quality measures. These products are sold separately. Review historical CCN identity and reporting periods before combining context; APCs and inpatient MS-DRGs are not equivalent services.
Sources and versions
Source: CMS Medicare Outpatient Hospitals by Provider and Service, calendar 2024, modified June 25, 2026 and captured by ACD October 27, 2026 UTC. It covers selected comprehensive APCs under Original Medicare Part B fee-for-service OPPS, excluding Maryland, territories and Medicare Advantage. It does not include every outpatient procedure or insurer.
The download records differences between the older CMS documentation and observed blank/zero handling in the 2024 file. Original observations remain unchanged. The source and preparation record separates observation dates, publisher revisions and ACD processing.
The government source is free; ACD sells preparation, documentation, validation and delivery. Processing improvements for 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.