A product code such as 0042 is a label, even though it contains only digits. If a CSV import turns it into the number 42, the two leading zeros disappear before you start matching or grouping records.

Give the code column an explicit string type when you call pd.read_csv(). Also choose how the reader handles missing values: a valid code such as NA can otherwise become missing during the same import.

The codes 0042, 0100, NA, and 0000 stay text after the CSV import. Only the empty code field becomes missing, while the units column remains numeric.
An explicit text type preserves the codes; a separate rule identifies missing fields. These five records are fictional.Credit: MathBlog

Separate identifiers from quantities

This fictional export contains five item records. Codes are text identifiers; units are whole-number quantities. The code NA is valid in this example. An empty code field means that the identifier is unknown.

CSV source values. The final code field is empty; the zero-unit record is a genuine quantity.
Code Units
0042 3
0100 0
NA 2
0000 4
Empty field 1

The complete CSV text is included in the code. StringIO lets pandas read that text as a file-like object, so no file download or local input file is needed for the example.

Set the types before pandas infers them

from io import StringIO
import pandas as pd

csv_text = """code,units
0042,3
0100,0
NA,2
0000,4
,1
"""

items = pd.read_csv(
    StringIO(csv_text),
    dtype={"code": "string", "units": "Int64"},
    keep_default_na=False,
    na_values={"code": [""], "units": [""]},
)
print(items.to_string(index=False))

Output:

code  units
0042      3
0100      0
  NA      2
0000      4
<NA>      1

The lowercase "string" dtype stores the codes as text with support for missing values. The uppercase "Int64" dtype stores nullable whole numbers. It is appropriate for this units column; it would not preserve the written form of an identifier.

Two settings control the missing-value rule. keep_default_na=False disables the reader’s default collection of missing markers. The per-column na_values dictionary then declares only empty fields missing in these two columns. The literal code NA survives, while the final empty code becomes <NA>, pandas’ displayed missing marker.

Choose the missing markers from the source specification. If an export uses NULL or another marker for an absent value, add that marker to the appropriate column’s rule. Do not keep a placeholder as a real identifier merely to avoid a missing-value count.

Check exact codes, missing values, and totals

print("Codes:", items["code"].fillna("<missing>").tolist())
print("Missing codes:", items["code"].isna().sum())
print("Total units:", items["units"].sum())

Output:

Codes: ['0042', '0100', 'NA', '0000', '<missing>']
Missing codes: 1
Total units: 10

Check each result separately. The first four identifiers retain their exact characters, one record has a missing identifier, and the five quantities add to ten units. The zero quantity contributes no units but remains a valid observed value.

The fillna("<missing>") call only prepares the displayed list. It does not replace the missing entry in items. For more ways to inspect those entries, see finding missing values in pandas.

Why a string dtype alone may not be enough

Compare the import above with this deliberately incomplete version:

default_missing = pd.read_csv(
    StringIO(csv_text), dtype={"code": "string"}
)
print("Missing code positions:", default_missing.index[
    default_missing["code"].isna()
].tolist())

Output:

Missing code positions: [2, 4]

Positions 2 and 4 are the original NA and empty code. The string dtype preserves leading zeros in the other identifiers, but it does not override the default missing-value rules. That is why the complete import specifies both types and missing markers.

Keep the original representation when combining data

Converting the integer 42 to text gives "42", not "0042". Padding could recreate a four-character code only if an authoritative rule says every valid code has that width. It cannot tell you whether a source originally contained 42, 042, or 0042.

If an earlier spreadsheet or export has already removed the zeros, inspect the original source or its identifier specification. Choosing a string dtype at the next import cannot recover information that is no longer in the CSV.

Use the same code types and missing-marker rules across files before combining multiple CSV exports. Match on the preserved identifier, not a numeric approximation of it.

The examples were executed with Python 3.11.3 and pandas 2.3.3.

Further reading