Add metro areas to customer ZIP codes in Python
Connect fictional ZIP territories to genuine metropolitan and micropolitan relationships, keeping address shares and outside-area outcomes.
Connect customer ZIP codes to regional markets
A business has customer or service ZIP codes and wants to group those records by metropolitan or micropolitan area. A core-based statistical area (CBSA) is one of those regions. This crosswalk adds its code, readable name and the share of the ZIP’s addresses associated with it.
input.csv contains three fictional service-territory labels—Cary central, Cary west and Maricao—with real postal ZIPs. The same records appear in the territories list in the Python code below. Only the business labels are invented; the geographic relationships and address shares are genuine.
Download zip_metro.csv below. It is a small version of the paid crosswalk: every relationship for ZIPs 27513, 27519 and 00606, with all 15 fields unchanged. The input CSV is optional because its records are also in the code. The full $29 edition includes 47,629 relationships for 39,472 ZIPs, optional assignments, coverage and Census area references. Inspect the complete product.
Code example
Place the sample in your working directory and run the code in a notebook, IPython or a script. It uses Python’s standard library, keeps ZIPs as text and prints the matching regions with residential and business address percentages.
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
# Fictional territory labels for real ZIP codes; use your own text ZIPs here.
territories = [
{"territory": "Cary central", "zip_code": "27513"},
{"territory": "Cary west", "zip_code": "27519"},
{"territory": "Maricao", "zip_code": "00606"},
]
with open("zip_metro.csv", newline="", encoding="utf-8") as file:
wanted = {item["zip_code"] for item in territories}
by_zip = {code: [] for code in wanted}
for row in csv.DictReader(file):
if row["zip_code"] in by_zip:
by_zip[row["zip_code"]].append(row)
results = []
for item in territories:
matches = by_zip[item["zip_code"]]
if not matches:
print(f"{item['territory']} ({item['zip_code']}): no source match")
for row in matches:
result = {
"territory": item["territory"],
"zip_code": item["zip_code"],
"cbsa_code": row["cbsa_code"],
"cbsa_name": row["cbsa_name"],
"area_type": row["area_type"],
"residential_percent": f"{Decimal(row['residential_ratio']) * 100:.2f}",
"business_percent": f"{Decimal(row['business_ratio']) * 100:.2f}",
}
results.append(result)
print(f"{result['territory']} ({result['zip_code']}) | "
f"{result['cbsa_code']} {result['cbsa_name']} | "
f"residential {result['residential_percent']}% | "
f"business {result['business_percent']}%")
Analysis of the output
Cary central (27513) | 39580 Raleigh-Cary, NC | residential 100.00% | business 100.00%
Cary west (27519) | 20500 Durham-Chapel Hill, NC | residential 6.84% | business 0.21%
Cary west (27519) | 39580 Raleigh-Cary, NC | residential 93.16% | business 99.79%
Maricao (00606) | 99999 Outside a metropolitan or micropolitan area | residential 100.00% | business 100.00%
ZIP 27513 connects entirely to Raleigh-Cary in this source snapshot. ZIP 27519 spans Raleigh-Cary and Durham-Chapel Hill: approximately 93.16% of its residential addresses and 99.79% of its business addresses are associated with Raleigh-Cary. A single ZIP label would hide that split.
A business can use these regional identifiers to organize a territory report, compare coverage across markets or attach compatible regional statistics. Keep both relationships when a split matters. If the task requires one region per ZIP, choose the address type that fits the task and use the optional assignment file described below.
For ZIP 00606, HUD supplies code 99999: outside a metropolitan or micropolitan area. That is a known source outcome, not a failed lookup. Other ZIPs can have both named-area and outside-area relationships. A ZIP absent from the file is different: the code prints “no source match” without guessing a region.
These percentages describe addresses, not customers, residents or sales. Assigning customer activity by an address distribution introduces an assumption; geocoding individual addresses is more appropriate when their actual region matters.
Adding regional context
The resulting CBSA codes also provide keys for compatible regional data. For example, the Metro Labor Force & Unemployment History includes these genuine annual observations:
| CBSA code | Area | 2025 unemployment rate |
|---|---|---|
| 20500 | Durham-Chapel Hill, NC | 3.4% |
| 39580 | Raleigh-Cary, NC | 3.2% |
These are regional rates, not unemployment estimates for ZIP 27519. The 2025 annual observations are eleven-month averages excluding October because of the federal shutdown. They use unadjusted BLS annual observations and compatible July 2023 CBSA definitions. A Q2 2026 postal relationship does not establish where each 2025 address belonged.
To extend the example, join cbsa_code to the regional file’s area_code after selecting one period and adjustment form. Keep split relationships separate; do not average these two rates using postal address shares and label the result a ZIP unemployment rate. The crosswalk already answers the basic regional-lookup question without a second purchase.
Using the full dataset
Extract the purchase and replace the sample with the full zip_metro.csv; the code runs unchanged. Replace territories with your own records using csv.DictReader, keeping or adapting the territory and zip_code fields. If starting with ZIP+4, explicitly reduce it to five digits. The product does not supply ZIP+4 geography.
largest_share_area.csv has one row per ZIP and address type: residential, business, other or total. Filter to one type before joining. The chosen code has the largest positive reported share. Ties choose the lowest code and remain flagged; all-zero categories have no assignment. Outside-CBSA code 99999 participates in the ranking, so a ZIP is never forced into a named area.
ratio_checks.csv reports original sums and deviations from one. Values are not normalized. zip_coverage.csv distinguishes named-area, mixed and outside-only source coverage. HUD excludes PO-box-only ZIPs and some ungeocoded addresses; the absence of a particular ZIP does not establish which exclusion caused it.
area_reference.csv lists the 935 named Census areas. county_area_reference.csv retains their 1,915 county memberships, metropolitan divisions and combined statistical areas. The main crosswalk maps to CBSAs; it is not the separately published ZIP-to-metropolitan-division crosswalk.
All six reference CSVs fit within Excel’s worksheet row limit. Import through Data > From Text/CSV and set ZIP and area codes to text before loading. Keep ratios as text if exact source decimal digits matter: spreadsheet numeric precision can round them.
Postal ZIPs are not Census ZCTAs, and current relationships do not reconstruct historic ZIP boundaries. Compare ZIP, county and tract products when a different geographic unit fits the question.
Sources and versions
The HUD-USPS ZIP Code Crosswalk supplies the original quarterly relationships free after registration. This edition uses Q2 2026, the latest quarter offered when checked November 30, 2026. Census July 2023 delineations supply names, types and county membership.
HUD’s public field description still refers to older definitions and divisions. The actual file contains the complete July 2023 CBSA code universe. We checked every supplied relationship against an independent aggregation of HUD’s same-quarter ZIP-to-county source using Census July 2023 membership; all four ratios agree within 1e-11. The source and preparation record documents the discrepancy and comparison limits.
Source quarter, geographic definition, capture date and ACD processing revision are separate facts. June 30 is quarter end, not a verified source publication date. Processing improvements and genuine publisher-error corrections to this purchased edition are included; future source quarters can be separately purchased. There is no subscription.
Example checked November 30, 2026 against both these samples and the complete purchased files. Source and processing records, sample checksums · Expected result CSV. Independent preparation; no agency endorsement.