If pandas removes leading zeros from a CSV identifier, open the original CSV in a text editor first. If the zeros are there, read the ID column as text:

from io import StringIO import pandas as pd data = StringIO("id\n00123\n123\n") df = pd.read_csv(data, dtype={"id": "string"}) assert df["id"].tolist() == ["00123", "123"] 

For your own file, replace data with the file path and id with your column name. If the zeros are already missing from the source, this cannot recover them.

I’m working on DEAR NEXT AI, a free troubleshooting guide collection. If you try this on a real CSV, please share your pandas version, whether it worked, and where you got stuck. Please keep your data private.

Full guide

This post was drafted with AI assistance.