★ GenAI Summit™ Asia|12–13 Dec, Kuala Lumpur|For AI Agency® students|See details →|★ GenAI Summit™ Asia|12–13 Dec, Kuala Lumpur|For AI Agency® students|See details →|★ GenAI Summit™ Asia|12–13 Dec, Kuala Lumpur|For AI Agency® students|See details →|★ GenAI Summit™ Asia|12–13 Dec, Kuala Lumpur|For AI Agency® students|See details →|
Two brass inventory stamps show 00127 and 127 side by side, with the two leading zeros in ochre.
AI How-ToDecember 1, 20266 min read

How to Keep Leading Zeros in pandas When Matching Supplier SKUs

Preserve SKUs such as 00127 and literal NA when importing CSVs into pandas. Use an exact-match join, reject duplicate master keys, and verify five expected prices or holds.

GenAI Editorial

To keep leading zeros in pandas, read the SKU as text in both CSV files and decide which strings count as missing before joining them. In our supplier test, 2 of 5 order lines picked up someone else’s price. The merge passed its uniqueness check and marked every row as matched.

The quickest way to catch this is to give the import two nearly identical lines. Ask for one unit of 00127 and one unit of 127. When only the first code exists in the supplier’s price list, the second line should go back to the buyer with an empty price. If both come back at 84, stop the quote and inspect the original CSV.

Which five rows expose the problem?

We built two small CSV files representing one fictional supplier. The supplier treats each SKU as an exact string. Its catalogue has three items: stool 00127 at 84, lamp 00240 at 29 and tray NA at 15. There is no approved alias for the stool. An empty item code cannot authorize a purchase. The prices are test values.

The two files below are small enough to recreate in a Python session. Open a new empty scratch directory first: these commands write files named catalog.csv and orders.csv. The comma after D is intentional. The line exists, but the buyer supplied no SKU.

from pathlib import Path

Path("catalog.csv").write_text("sku,item,price\n00127,Stool,84\n00240,Lamp,29\nNA,Tray,15\n", encoding="utf-8")

Path("orders.csv").write_text("line,sku\nA,00127\nB,127\nC,NA\nD,\nE,00240\n", encoding="utf-8")

Before opening pandas, we wrote down the expected prices in row order: 84, hold, 15, hold, 29. Two lines can only pass by remaining unpriced. This makes the fixture useful for reviewing AI-generated scripts. If your acceptance check counts nonempty prices, the least accurate version wins.

We tested the C CSV parser in pandas 3.0.3 on December 1, 2026. We kept the input bytes, order of operations and join direction fixed. Each run left-joined the orders to the price master, requested a match indicator and required the master key to be unique. We changed how the loader treated identifiers and missing values. If you’re still extracting prices from a PDF, check the quantity tiers before running this join. How to convert a supplier PDF price list to Excel

Why did a five-for-five match assign two wrong prices?

The default reader interpreted the numeric-looking codes as numbers and the literal NA as a missing value. After that conversion, the stool’s identifier and the unmatched order code had the same numeric value. Line B acquired the stool price. There was no extra stool row in the master, so the right-hand uniqueness check passed.

The second wrong price came from a different rule. The tray’s master code NA and the empty field on line D had both become missing. A pandas merge can match a null key on one side to a null key on the other. Our blank order line acquired the tray price of 15. Even line C, which returned the correct price, got there through a missing key. pandas merge documentation

Our second run specified dtype={"sku": "string"} on both reads. Now line B was held and the leading zeros survived. Line D still picked up the tray. A string column can contain missing values. Choosing a text type solves the numeric conversion problem. The CSV reader has a separate policy for recognizing missing fields. pandas CSV reader

In the third run we kept the source strings, switched off the default missing-value vocabulary and excluded empty master keys from the lookup. A and E matched the exact printed codes. C found the actual two-letter tray code. B and D retained their order-line IDs and empty prices. All five dispositions agreed with the handwritten expectation. The pandas IO guide documents the default list and the options for defining your own. pandas missing-value rules

How do you import and check the two files?

This example produces one output row for each order line. The status field tells the buyer whether the price can be used or what they must resolve first. Filtering empty master keys prevents a blank on each side from becoming a valid pair. The whitespace test catches space-only fields without stripping meaningful characters from nonempty codes.

import pandas as pd

options = dict(engine="c", dtype={"sku": "string"}, keep_default_na=False)

orders = pd.read_csv("orders.csv", **options)

catalog = pd.read_csv("catalog.csv", **options)

usable = catalog.loc[catalog["sku"].str.strip().ne("")]

result = orders.merge(usable, on="sku", how="left", validate="many_to_one", indicator=True)

result["status"] = "priced"

result.loc[result["_merge"].eq("left_only"), "status"] = "unknown SKU"

result.loc[result["sku"].str.strip().eq(""), "status"] = "missing SKU"

print(result[["line", "sku", "price", "status"]].to_string(index=False))

With this fixture, the two holds need different follow-up. For B, ask the supplier whether 127 is an authorized alias or a different item. For D, ask the buyer to supply the missing item code. Filling either with zero would turn an unresolved purchase into a free item. Preserve the raw order-line ID so the answer can be traced back to the request.

In a real file, check the price field independently. The example master supplies a valid price for every item. A matching SKU with a blank or malformed price should also be held. Turning off default NA parsing applies to the whole read. If the supplier uses missing-value markers in the price column, configure those for that column and validate the amount before accepting it.

What if the supplier sends a conflicting price or a fixed-width code?

We copied the stool row and changed its price from 84 to 94, leaving the SKU alone. The third import now raised a pandas MergeError because the master had two rows for one key. An instruction to “keep the first duplicate” would silently choose a commercial term. Inspect the effective date, unit, currency or quantity tier that distinguishes the two offers before deciding which columns belong in the lookup.

That same uniqueness check had passed on the default import. It answers a specific question: can each parsed key find more than one master row? It cannot establish that the parser preserved the supplier’s identifier. Keep both checks in your review: the master must be unique at the level you intend to buy, and known near-misses must remain unpriced.

If the supplier explicitly defines a five-character numeric field and accepts shorter aliases, padding may be part of its documented contract. Record that rule for that supplier and check whether normalization creates collisions. Without that authority, padding 127 to 00127 invents the very match this test is meant to catch. If an upstream spreadsheet has already saved both original codes as 127, reimport from the original export or request a fresh copy. A different pandas dtype cannot recover characters that the file no longer contains.

What should you ask your AI coding assistant to return?

We gave Gemini 2.5 Flash the raw fixture and the supplier’s exact-match rule in one temperature-zero request. We asked for parser settings, join validation and the expected price for each order line. It chose string identifiers, disabled default NA recognition, excluded blank keys and selected many-to-one validation. We applied its settings in our test harness and compared the actual output to the expectations saved before the call. It matched all five. That was one run with a clearly stated supplier rule.

Use this prompt with a small extract you are allowed to share:

> Show the read settings for each CSV, the join columns and the check for conflicting master rows. Treat each SKU exactly as the supplier supplied it. For this master, NA is a genuine product code and an empty SKU must stay unpriced. Return one output per input line with the original line ID, matched SKU, proposed price and reason for any hold. Tell me which source value you would need before resolving each hold.

Before accepting the generated import, run three checks yourself:

1. Remove the zeros from one incoming code only. Confirm that its price disappears while the correctly spelled order keeps its price.

2. Put the real code NA next to an empty SKU. Confirm that only the real item gets 15.

3. Add a second master price under 00127 . Confirm that the run stops and names the conflicting key.

The useful handoff is a priced order plus the unresolved rows. A buyer can act on an unknown code. A plausible price attached to the wrong item is much harder to notice.

Sources

  1. pandas.read_csv
  2. pandas.merge
  3. pandas IO tools, NA values
  4. GenAI: Convert a supplier PDF price list to Excel

More where this came from

Keep reading.

See all posts →