Compare food and housing spending by income in Python
Compare genuine BLS food and housing expenditure means and budget shares for lower- and higher-income consumer units in 2019 and 2025.
Compare food and housing in consumer budgets
Suppose you are preparing a brief on how spending differs across income groups. A dollar comparison tells you how much each group spends; the spending share tells you how large that category is within its total budget. This example reads both measures for food and housing in 2019 and 2025, using the lowest and highest income quintiles.
Download expenditures.csv into your working folder. It contains eight genuine rows selected from the paid product, with all 30 fields unchanged. The years, income-group keys and category names are selected in the Python code below.
The complete $29 edition contains 21,330 expenditure records for 2012–2025, plus annual group definitions, category histories, original source cells and notes. Income and age are separate national breakdowns. Inspect the full coverage, fields and files.
Code example
This standard-library example runs in a notebook cell, IPython or a script. It filters the income-quintile table and prints nominal annual dollars per consumer unit, the published share of total spending, and the dollar mean’s standard error. Change the selections when using the complete file.
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
# The free CSV contains genuine product rows for these years and income groups.
# Use the same filename from the purchased ZIP to select other years/categories.
years = {'2019', '2025'}
groups = {'q1': 'Lowest-income fifth', 'q5': 'Highest-income fifth'}
categories = {'Food', 'Housing'}
results = []
seen = set()
with open('expenditures.csv', newline='', encoding='utf-8') as file:
for row in csv.DictReader(file):
if (row['table_family'] != 'income_quintile' or row['year'] not in years
or row['group_id'] not in groups or row['category_path'] not in categories):
continue
key = (row['year'], row['group_id'], row['category_path'])
if key in seen:
raise ValueError('Duplicate expenditure record')
seen.add(key)
results.append({
'year': row['year'], 'group_id': row['group_id'],
'category': row['category_path'], 'mean_usd': row['mean_usd'],
'share_pct': row['share_pct'], 'standard_error_usd': row['standard_error_usd'],
'mean_status': row['mean_usd_status'], 'share_status': row['share_pct_status'],
})
if len(results) != len(years) * len(groups) * len(categories):
raise ValueError('The selected comparison is incomplete')
results.sort(key=lambda row: (row['year'], row['group_id'], row['category']))
for row in results:
if row['mean_status'] != 'reported' or row['share_status'] != 'reported':
print(f"{row['year']} | {groups[row['group_id']]} | {row['category']} | "
f"Review source status: {row['mean_status']}, {row['share_status']}")
continue
dollars = Decimal(row['mean_usd'])
print(f"{row['year']} | {groups[row['group_id']]} | {row['category']} | "
f"${dollars:,.0f} per consumer unit | {row['share_pct']}% of spending | "
f"mean SE ${row['standard_error_usd']}")
Analysis of the output
2019 | Lowest-income fifth | Food | $4,400 per consumer unit | 15.3% of spending | mean SE $255.34
2019 | Lowest-income fifth | Housing | $11,531 per consumer unit | 40.2% of spending | mean SE $263.9
2019 | Highest-income fifth | Food | $13,987 per consumer unit | 11.5% of spending | mean SE $257.17
2019 | Highest-income fifth | Housing | $36,302 per consumer unit | 29.9% of spending | mean SE $582.39
2025 | Lowest-income fifth | Food | $5,156 per consumer unit | 13.6% of spending | mean SE $180.67
2025 | Lowest-income fifth | Housing | $16,184 per consumer unit | 42.7% of spending | mean SE $354.19
2025 | Highest-income fifth | Food | $18,415 per consumer unit | 11.5% of spending | mean SE $389.58
2025 | Highest-income fifth | Housing | $48,373 per consumer unit | 30.3% of spending | mean SE $1729.38
In 2025, the lowest-income fifth spent an estimated $16,184 per consumer unit on housing, compared with $48,373 for the highest-income fifth. Housing represented 42.7% of total spending in the lowest-income group and 30.3% in the highest-income group. The higher-income group spent more dollars, while housing took a smaller share of its spending.
The corresponding housing shares in 2019 were 40.2% and 29.9%. This is a descriptive comparison of published estimates. It does not establish a statistically significant change or follow the same families over time: each year’s quintiles use that year’s income distribution and thresholds.
A consumer unit is a family, a financially independent person, or people who share major expenses. The means include consumer units that did not purchase the item. Shares divide the group’s mean category expenditure by its mean total expenditure; they are not percentages of income or averages of each family’s budget share.
The displayed standard error measures uncertainty in the dollar mean. It is not the standard error of the spending share or of the difference between two groups. The full data also retains CV/RSE percentages and the source’s caution flags.
Adding inflation context
These amounts are nominal dollars. The companion Consumer Prices & Inflation History provides national CPI-U indexes for a constant-dollar comparison. Use one consistent annual series and its published annual averages, and inspect the reported-month count: affected 2025 averages use eleven available months. The CE and CPI categories are not a supplied crosswalk, and an inflation adjustment alone does not explain why spending changed. Price, quantity and group-composition changes can all matter.
Using the full dataset
Extract the purchased ZIP and replace the sample with its complete expenditures.csv. The example runs unchanged. The unique key is year + table_family + category_id + group_id. For an age comparison, select table_family = age and an age key from groups.csv; age refers to the consumer unit’s reference person. Income and age cannot be combined into a joint breakdown from these files.
Join categories.csv on year, table family and category ID to inspect original hierarchy. Parent categories and their children overlap. Join groups.csv on year, table family and group ID for group labels, income boundaries, average consumer-unit size and weighted population estimates. Those population estimates are not sample sizes. The 65-and-older age group overlaps the 65–74 and 75-and-older groups.
Check the per-value status before using a number. Blank numeric fields distinguish suppression, no data and amounts too small to display. In 2022–2023, BLS suppressed certain estimates with high relative standard errors; from 2024 it published numeric values with caution flags. The preparation reads flags encoded in Excel formatting as well as cell text and preserves the year-specific note meanings.
Before drawing a trend, review category_changes.csv and the source notes. Category IDs identify exact normalized label paths; changed labels receive different IDs, and unchanged labels do not guarantee unchanged definitions. This table records appearances and disappearances rather than guessing successor categories. source_values.csv supplies original values, formats and cell coordinates for an audit.
Sources and versions
Source: BLS Consumer Expenditure annual tables. This edition retains the income-quintile and reference-person age workbooks for calendar years 2012–2025, captured November 18, 2026 UTC. The 2025 tables were released October 29, 2026; each workbook’s publication date is retained separately. This is a snapshot of the currently hosted history, not an archive of every original publication vintage.
The 2025 collection interruption affected October and November. BLS adjusted weights for the published annual tables; the note is retained. Review the table guide and glossary for expense definitions and statistical interpretation.
BLS provides the source workbooks free. ACD sells the preparation, validation, documentation and delivery. Processing improvements and documented publisher error corrections to this purchased edition are included; new source editions may be sold separately. Read the preparation record.
Example checked November 18, 2026 against both these samples and the complete purchased files. Source and processing records, sample checksums · Expected result CSV. Independent preparation; no agency endorsement.