Check the identifier in the DataFrame, then in the CSV as plain text, then in Excel’s import preview. The first place 00123 becomes 123 tells you which step to fix. A value already reduced to 123 does not reveal how many zeros it originally had. This is a documentation-based comparison; no pandas or Excel run was recorded.
If the DataFrame already contains 123, fix the input
Inspect the value in the exact DataFrame passed to export. If the source CSV contains 00123 but the DataFrame contains 123, read the identifier column as text: pd.read_csv("input.csv", dtype={"code": str}). Check the resulting value before exporting. pandas documents per-column dtype selection and says column types can otherwise be inferred. This remedy applies to a nonmissing identifier: recognized missing values can still be parsed as missing despite the requested dtype.
If the DataFrame was assembled from an integer or another source that had already lost the zeros, changing read_csv or to_csv cannot recover the original spelling. Return to the original record. A known fixed width may support a display convention, but do not infer an unknown identifier from 123.
Sources: pandas.read_csv — pandas.read_csv — dtype
If the DataFrame has 00123, inspect the CSV itself
Open the exported CSV in a plain text editor before opening it in Excel. For the illustrative identifier, the expected CSV text is the following two literal lines, with the first line as the header: code 00123 This is an expected comparison point, not tested output. You may also inspect df.to_csv(index=False) for the DataFrame being exported; pandas documents that to_csv returns CSV text when no output path is supplied. Compare the same record in that text and in the saved file.
Use this reading map to choose the next action. It is a diagnostic comparison, not an experimental result. If the DataFrame contains 00123 but the raw CSV says 123, check that the object exported is the object you inspected and look for changes between inspection and export. If the raw CSV still says 00123, preserve that original file for a fresh Excel import.
| First place 00123 becomes 123 | Likely step to investigate | Action |
|---|---|---|
| DataFrame; source CSV still has 00123 | pandas input | Read the nonmissing identifier column as text and inspect it again |
| Raw CSV; exported DataFrame had 00123 | Export path or intervening change | Confirm the exported object and inspect the actual saved CSV |
| Excel; raw CSV still has 00123 | Excel conversion on import | Restart import from the original CSV and retain the column as Text |
Sources: pandas.read_csv — pandas.read_csv — dtype, pandas.DataFrame.to_csv — pandas.DataFrame.to_csv — Returns, Keeping leading zeros and large numbers — Keeping leading zeros and large numbers — Set automatic data conversions
If only Excel shows 123, protect the original text during import
Excel can convert numerical text to a number and remove leading zeros. Start from the original CSV that still contains 00123. In Excel, use Data > From Text/CSV and examine the import preview before loading. Verify that the original digits remain in the import data. Microsoft documents opening the editor and setting the identifier column’s data type to Text before Close & Load. If the preview or an earlier import step has already reduced the value to 123, restart from the original CSV; selecting Text after conversion cannot restore the missing digits.
Microsoft also describes Automatic Data Conversions controls for Excel for Microsoft 365 and Excel 2024, including their Mac editions. Availability depends on version. A custom number format can display a known width, but Microsoft says it does not restore zeros removed before formatting. For identifiers with unknown original width, recover the original text rather than applying a guessed format.
Sources: Keeping leading zeros and large numbers — Keeping leading zeros and large numbers — Convert numbers to text when you import text data, Keeping leading zeros and large numbers — Keeping leading zeros and large numbers — Set automatic data conversions, Keeping leading zeros and large numbers — Keeping leading zeros and large numbers — Use a custom format to keep the leading zeros
Primary sources checked: 2026-11-10. Prepared with AI assistance and a separate evidence review. This is documentation-based guidance, not a hands-on test or performance benchmark.