Fixing MSHA Mine IDs and County Codes That Lose Their Leading Zeros
On this page
Problem statement
MSHA's mine id is seven characters, "0100003" for an Alabama quarry, and its county code three, "007". Read with pandas' defaults, both become integers, 100003 and 7, and every join to MSHA's other files and to county boundaries then fails for part of the country. The county code has a second problem: it is only the county part of a FIPS code, so even read as text it matches no county until the state's code is put in front. This page shows the damage from reading the codes as numbers, how many ids it changes, and how to build a county key that joins.
Quick answer
Read the same three columns with default types and as text:
num = pd.read_csv(Z.open("Mines.txt"), sep="|", encoding="latin-1", usecols=["MINE_ID", "FIPS_CNTY_CD", "STATE"])
print(num.dtypes.to_string())
print(num[num.STATE == "AL"].head(2).to_string(index=False))
txt = read("Mines.txt")[["MINE_ID", "FIPS_CNTY_CD", "STATE"]]
print(txt[txt.STATE == "AL"].head(2).to_string(index=False))
MINE_ID int64
STATE str
FIPS_CNTY_CD int64
MINE_ID STATE FIPS_CNTY_CD
100003 AL 117
100004 AL 7
MINE_ID FIPS_CNTY_CD STATE
0100003 117 AL
0100004 007 AL
With default types, MINE_ID and FIPS_CNTY_CD are integers: the Alabama quarries are mine 100003 in county 117 and mine 100004 in county 7. Read as text, the same rows are "0100003" and "0100004" in counties "117" and "007". The leading zero of the id is the first digit of the state code, and it is gone.
How many change, and joins that fail
1. How many ids change
print(f"MINE_IDs that start with 0: {txt.MINE_ID.str.startswith('0').sum():,}; county codes with 4 characters as text: {(txt.FIPS_CNTY_CD.str.len() == 4).sum():,}")
print(txt.FIPS_CNTY_CD.str.len().value_counts().to_dict())
MINE_IDs that start with 0: 12,613; county codes with 4 characters as text: 0
{3: 92090}
12,613 mine ids start with 0, the mines of the states whose Bureau of Mines code is below 10, and all of them change when read as numbers. Every county code is three characters as text, so the numbers lose one or two zeros for most counties.
2. Joins that fail
prod = read("MinesProdYearly.txt")
a = num.MINE_ID.astype(str).isin(prod.MINE_ID).mean()
b = txt.MINE_ID.isin(prod.MINE_ID).mean()
print(f"mines found in the production file: as numbers {a:.1%}, as text {b:.1%}")
mines found in the production file: as numbers 31.3%, as text 36.8%
Matched against the employment and production file, which covers mines that have worked since 2000, 36.8% of the Mines file's ids are found when both are text, but only 31.3% after reading the ids as numbers and converting back: the ids with a leading zero no longer match. The fix is dtype=str on every MSHA file, which keeps every code exactly as MSHA wrote it, rather than padding numbers back afterwards.
3. A county code without its state
counties = gpd.read_file("data/cb_2024_us_county_20m.zip")
st_fips = gpd.read_file("data/cb_2024_us_state_20m.zip").set_index("STUSPS").STATEFP
full = txt.STATE.map(st_fips) + txt.FIPS_CNTY_CD
print(f"FIPS_CNTY_CD matching a county: as given {txt.FIPS_CNTY_CD.isin(counties.GEOID).mean():.1%}; padded to 5 {txt.FIPS_CNTY_CD.str.zfill(5).isin(counties.GEOID).mean():.1%}; with the state's FIPS in front {full.isin(counties.GEOID).mean():.1%}")
FIPS_CNTY_CD matching a county: as given 0.0%; padded to 5 0.0%; with the state's FIPS in front 99.1%
As given, the county code matches no county at all, 0.0%, and padding it to five digits still matches none, because it has no state. With the state's two-digit FIPS code, taken from the state abbreviation, in front, 99.1% match. MSHA's own BOM_STATE_CD is a Bureau of Mines state code, not a FIPS code, and must not be used for the prefix.
The accident file's state code
The data used on this page
The US Mine Safety and Health Administration's Open Government Data sets: the Mines file of every mine ever registered, annual employment and production by mine since 2000, and the accident reports from 2000 on (24 of their 57 columns, without the narratives), all as published on 23 October 2026, downloaded on 28 October 2026 and saved on this site, because MSHA replaces the files every week. Public domain. The files are pipe-delimited and Latin-1 encoded. County and state boundaries and county populations are the Census Bureau's 2024 files, public domain.
from pathlib import Path
import zipfile
import geopandas as gpd
import numpy as np
import pandas as pd
import requests
Path("data").mkdir(exist_ok=True)
# MSHA's Mines file, annual employment and production by mine since 2000 and accident reports since 2000 (24 columns,
# no narratives), all of 23 October 2026, with the Census Bureau's county estimates, counties and states. Saved
# 28 October 2026: MSHA replaces the files every week.
# Live: https://arlweb.msha.gov/OpenGovernmentData/DataSets/Mines.zip (and MinesProdYearly.zip, Accidents.zip)
ARCHIVE = "msha-mines-2026-10-28.zip"
if not Path(f"data/{ARCHIVE}").exists():
Path(f"data/{ARCHIVE}").write_bytes(requests.get(f"https://data.spatialworkflow.io/{ARCHIVE}", timeout=900).content)
Z = zipfile.ZipFile(f"data/{ARCHIVE}")
for f in ["cb_2024_us_county_20m.zip", "cb_2024_us_state_20m.zip"]:
Path(f"data/{f}").write_bytes(Z.read(f))
def read(name):
return pd.read_csv(Z.open(name), sep="|", encoding="latin-1", dtype=str, low_memory=False) # pipe-delimited, Latin-1, ids keep zeros
mines = read("Mines.txt")
for c in ["LATITUDE", "LONGITUDE", "NO_EMPLOYEES"]:
mines[c] = pd.to_numeric(mines[c], errors="coerce")
print(f"MSHA Mines file: {len(mines):,} mines; {mines.CURRENT_MINE_STATUS.value_counts().head(3).to_dict()}")
MSHA Mines file: 92,090 mines; {'Abandoned': 69608, 'Abandoned and Sealed': 8823, 'Active': 6667}
Example 1: the accident file's state code
acc = read("msha-accidents-2000-2026.txt")
print(acc.FIPS_STATE_CD.str.len().value_counts().to_dict(), acc.FIPS_STATE_CD.head(3).tolist())
{2: 275521} ['01', '01', '01']
The accident file carries a two-character FIPS state code, "01" for Alabama, which survives as text but would become 1 as a number. Read every MSHA file with dtype=str and convert only the columns that are really numbers.
Internal links
- Reading the Mines file β the whole file read as text
- Active mines by county β the county join in use
- Census joins that lose leading zeros β the same problem in census data
- MSHA accident counts β the accident file