An order export uses N, P, and S where a report needs New, Packed, and Sent. A dictionary lookup can add those labels without replacing the original codes.
Use Series.map() to translate the known values, then add a separate review status. A code absent from the lookup and a code absent from the source can both produce a missing label, but they need different follow-up.

Write down the mapping and the cleanup rule
For this fictional order system, the dictionary is {"N": "New", "P": "Packed", "S": "Sent"}. Matching is case-sensitive. Surrounding whitespace is an entry mistake that we are allowed to trim, while an empty or whitespace-only code means missing data.
| Order | Raw status code |
|---|---|
| O01 | N |
| O02 | S |
| O03 | X |
| O04 | Missing value |
| O05 | "" |
| O06 | " P " |
| O07 | s |
The lowercase s is intentionally unrecognized. Automatically changing case would apply a rule we have not been given. The original code column stays in the result so someone can inspect each decision.
Add labels and distinguish the two gaps
import pandas as pd
orders = pd.DataFrame({
"order": ["O01", "O02", "O03", "O04", "O05", "O06", "O07"],
"status_code": pd.Series(
["N", "S", "X", None, "", " P ", "s"], dtype="string"
),
})
labels = {"N": "New", "P": "Packed", "S": "Sent"}
def label_status(frame, lookup):
if any(not isinstance(k, str) or not k or k.strip() != k for k in lookup):
raise ValueError("Lookup keys must be nonblank text without outer spaces.")
if any(not isinstance(v, str) or not v.strip() for v in lookup.values()):
raise ValueError("Every lookup value must be a nonblank text label.")
result = frame.copy()
code = result["status_code"].astype("string").str.strip().replace("", pd.NA)
result["code_clean"] = code
result["status_label"] = code.map(lookup).astype("string")
result["lookup_status"] = "Mapped"
result.loc[code.isna(), "lookup_status"] = "Missing code"
result.loc[code.notna() & ~code.isin(lookup), "lookup_status"] = "Unknown code"
return result
labeled = label_status(orders, labels)
print(labeled[[
"order", "code_clean", "status_label", "lookup_status"
]].to_string(index=False))
Output:
order code_clean status_label lookup_status
O01 N New Mapped
O02 S Sent Mapped
O03 X <NA> Unknown code
O04 <NA> <NA> Missing code
O05 <NA> <NA> Missing code
O06 P Packed Mapped
O07 s <NA> Unknown code
The function checks that lookup keys are nonblank text without outer spaces and that every label is nonblank text. That makes a missing label meaningful: it signals an absent or unrecognized source code rather than an intentionally empty lookup description.
map() returns a label where the cleaned code is a dictionary key. Unknown keys become missing. The isna() mask identifies absent source codes, and isin(lookup) checks membership in the dictionary’s keys. The separate membership test distinguishes an unknown code from missing source data.
O06 maps to Packed after its surrounding spaces are removed. O03 and O07 remain unknown. O04 and O05 have no usable source code. All seven original rows remain present.
Review unknown codes before preparing the report
review = labeled.loc[
labeled["lookup_status"].eq("Unknown code"), ["order", "status_code"]
]
print(review.to_string(index=False))
print(labeled["lookup_status"].value_counts().reindex(
["Mapped", "Unknown code", "Missing code"], fill_value=0
).to_string())
Output:
order status_code
O03 X
O07 s
lookup_status
Mapped 3
Unknown code 2
Missing code 2
The review table contains the original values X and s. There are three mapped rows, two unknown codes, and two missing codes. They sum to the seven source records.
Ask the source owner whether an unknown code is newly introduced, mistyped, or valid under a rule missing from your lookup. Add an authoritative mapping when one exists. Do not label every unrecognized value Sent simply because that makes a chart look complete.
If a display requires a placeholder, add it in a separate presentation column and keep lookup_status. Filling every missing label with one word would conceal why the label was unavailable.
Preserve keys and validate the lookup source
The example starts with text codes. Keep that type when importing identifier columns; numeric conversion can change their written form before the lookup runs. Also decide whether surrounding spaces are genuinely errors in your own data before copying the trimming rule.
A dictionary holds one value per key. If your lookup begins as a table with duplicate codes, resolve those conflicting rows before converting it to a dictionary; a repeated key can otherwise overwrite an earlier value. For attaching several columns from a checked lookup table, see matching two lists with a validated merge in pandas.
Missing source codes need their own review. The missing-value guide explains why blank text and an actual missing marker are different until you apply a specific rule.
The examples were executed with Python 3.11.3 and pandas 2.3.3.