zipcode features
similar to uszipcode-project
Getting CBSA mapping
If you need CBSA data you can append it to the dataframe with the following example:
from zipcode_features import us_get_demographics
import pandas as pd
def _get_cbsa_data():
return pd.read_excel(
"https://github.com/EricSchles/zipcode_features/raw/refs/heads/main/zipcode_features/CBSA_ZIP_122025.xlsx",
sheet_name='Export Worksheet'
)[["CBSA", "ZIP"]]
demo = us_get_demographics(state="NY")
cbsa_zip_map = _get_cbsa_data()
df = pd.merge(demo, cbsa_zip_map, how="left", left_on="zipcode", right_on="ZIP")
For the semantic names you can get them here.
Here's a python script to parse them:
import urllib.request
import PyPDF2
import json
import re
import io
def fetch_cbsa_to_json():
url = "https://www2.census.gov/programs-surveys/cps/methodology/2015%20Geography%20Cover.pdf"
print("Downloading Census PDF...")
# Using a User-Agent to ensure the request isn't blocked by the server
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
try:
response = urllib.request.urlopen(req)
pdf_bytes = io.BytesIO(response.read())
except Exception as e:
print(f"Failed to download PDF: {e}")
return
print("Parsing PDF...")
reader = PyPDF2.PdfReader(pdf_bytes)
cbsa_mapping = {}
# Regular expression to match a 5-digit FIPS/CBSA code followed by the area name
# Example match: "11460 Ann Arbor, MI"
pattern = re.compile(r'\b(\d{5})\s+(.+?)(?=\s+\d{5}|\n|$)')
for page in reader.pages:
text = page.extract_text()
if text:
matches = pattern.findall(text)
for code, name in matches:
# Clean up any trailing spaces or artifacts
clean_name = name.strip()
# Exclude standalone numbers or random headers that might get caught
if len(clean_name) > 2 and not clean_name.isdigit():
cbsa_mapping[code] = clean_name
print(f"Extracted {len(cbsa_mapping)} CBSA codes.")
# Save the mapping to a JSON file
output_file = 'cbsa_codes.json'
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(cbsa_mapping, f, indent=4)
print(f"Successfully saved to {output_file}")
if __name__ == "__main__":
fetch_cbsa_to_json()
Here's a working example for using this with the above:
import requests
from zipcode_features import us_get_demographics
import pandas as pd
def _get_cbsa_data():
return pd.read_excel(
"https://github.com/EricSchles/zipcode_features/raw/refs/heads/main/zipcode_features/CBSA_ZIP_122025.xlsx",
sheet_name='Export Worksheet'
)[["CBSA", "ZIP"]]
demo = us_get_demographics(state="NY")
cbsa_zip_map = _get_cbsa_data()
df = pd.merge(demo, cbsa_zip_map, how="left", left_on="zipcode", right_on="ZIP")
df = df.drop("ZIP", axis=1)
mapping = requests.get("https://raw.githubusercontent.com/EricSchles/zipcode_features/refs/heads/main/zipcode_features/cbsa_codes.json").json()
df["cbsa_name"] = df["CBSA"].map(mapping)
df = df.drop("CBSA", axis=1)
Adding County
from zipcode_features import us_get_demographics
import pandas as pd
def _get_fips_data():
df = pd.read_excel(
"https://github.com/EricSchles/zipcode_features/raw/refs/heads/main/zipcode_features/ZIP_COUNTY_122025.xlsx",
dtype={'ZIP': 'str'},
sheet_name='Export Worksheet'
)[["COUNTY", "ZIP"]]
df["COUNTY"] = df['COUNTY'].astype(str)
return df.dropna()
demo = us_get_demographics(state="NY")
fips_zip_map = _get_fips_data()
df = pd.merge(demo, fips_zip_map, how="left", left_on="zipcode", right_on="ZIP")
df = df.drop("ZIP", axis=1)
df = df.dropna()
Adding Regional Prices
python -m pip install beaapi us
from zipcode_features import us_get_demographics
import pandas as pd
import beaapi
import us
df = us_get_demographics(state="NY")
# get your key here: https://apps.bea.gov/API/signup/
beakey = ""
dataset="Regional"
table = "SARPP"
regional_cpi = beaapi.get_data(
userid=beakey,
method='GetData',
datasetname=dataset, # National Income and Product Accounts
tablename=table, # Table 1.1.1
GeoFips="STATE",
LineCode="1",
ResultFormat="json"
#Frequency='A', # Annual data
)[["GeoName", "DataValue"]]
regional_cpi = regional_cpi[regional_cpi["GeoName"] != "United States"]
regional_cpi["year"] = ["2020", "2021", "2022", "2023", "2024"] * 51
abbreviations_map = us.states.mapping('name', 'abbr')
regional_cpi["state"] = regional_cpi["GeoName"].map(abbreviations_map)
regional_cpi["cpi"] = regional_cpi["DataValue"]
regional_cpi = regional_cpi.drop("DataValue", axis=1)
regional_cpi = regional_cpi[regional_cpi["year"] == "2024"]
regional_cpi["cpi_year"] = regional_cpi["year"]
regional_cpi.drop("year", axis=1)
df = pd.merge(df, regional_cpi, how='left', on="state")
df["regional_cpi"] = df["cpi"]
df = df.drop("cpi", axis=1)
Metadata
Release files for zipcode-features 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| zipcode_features-0.1.7.tar.gz | 8.2 MB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| zipcode_features-0.1.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.5 MB
Release files / zipcode_features-0.1.7.tar.gz
| Download URL | zipcode_features-0.1.7.tar.gz |
|---|---|
| Size | 8.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0240f6ea8a1b32c75b27c45893e57e250ecca087af151b98571b32f984fba242
|
|
BLAKE2b-256 checksum How to use checksums |
c099b328ecac79eea992e2bd58e020fd38e17294d34b7e05951bdf86cb2d6b3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / zipcode_features-0.1.7-py3-none-any.whl
| Download URL | zipcode_features-0.1.7-py3-none-any.whl |
|---|---|
| Size | 8.3 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
401642c273f30d2a9dd1bc90897b003294ee40833e2e9b846b87b56930259e06
|
|
BLAKE2b-256 checksum How to use checksums |
a32a3fd8931272900263dafaf1fa7ff80fcb145fef1ddd8959ba732d17582e18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for zipcode-features 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.