Knowledge base / Python
pandas map: dictionaries, functions and unknown codes
I use a small dictionary to show mapping and explain why missing inputs and unknown codes deserve separate checks.

When I have a column of codes and need descriptions, I usually start with Series.map. First, though, I decide what should happen to a code absent from the dictionary. A missing description might mean a new category, a typo or missing input. I would rather distinguish those situations before filling every gap with the same label.
This short reference focuses on mapping values. All data is synthetic, and the expected results follow from the pandas 3.1.1 documentation. The snippets avoid random inputs and dependencies on a DataFrame overwritten in an earlier example.
Use a dictionary to turn codes into labels
The Series.map reference accepts a function, dictionary or Series as the mapping rule. Its result retains the input index. With an ordinary dictionary, an unmatched key produces a missing value.
import pandas as pd
codes = pd.Series(["N", "D", "X", None], index=[10, 20, 30, 40], dtype="object")
labels = {"N": "new", "D": "done"}
result = codes.map(labels)
| Index | Code | Meaning of the result |
|---|---|---|
| 10 | N | new |
| 20 | D | done |
| 30 | X | no match |
| 40 | missing | no match |
The first two labels follow directly from the dictionary pairs. Neither remaining input has a key. This table describes values rather than reproducing a console display. I would retain the original column: the missing outputs alone no longer explain whether the source contained an unfamiliar code or no value at all.
Review unknown codes separately from missing inputs
Row 30 raises a question about X. Row 40 raises a question about absent input. I would avoid automatically classifying both as other, which could hide a dictionary change. Agree with the reader whether unknown codes should stop preparation, appear in a separate review list or receive an approved label. That is a data decision the method cannot make for you.
For a callable, na_action="ignore" skips passing missing inputs to the function. It does not create missing dictionary keys. If X remains undefined, this option cannot supply its description. Separate input completeness from dictionary completeness before choosing a fallback. The entry on pandas fillna discusses filling gaps later in a workflow.
A function receives an individual value
This snippet supplies its own input and does not depend on a previously overwritten frame:
import pandas as pd
quantities = pd.Series([1, 2, 3])
def double(value):
return value * 2
result = quantities.map(double)
The successive values are 2, 4 and 6. I would first ask whether a custom function is necessary. For multiplication alone, quantities * 2 expresses the intention directly. I am not making an unmeasured performance claim about a particular project. A function is useful when it carries a rule worth naming, rather than adding another layer around an ordinary operation.

DataFrame also has a map method
The name is not restricted to individual columns. DataFrame.map has existed since pandas 2.1.0, succeeding the name applymap. It takes an elementwise callable, rather than a dictionary mapping in the style of Series.map.
import pandas as pd
frame = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
result = frame.map(lambda value: value + 1)
Column a becomes 2 and 3; column b becomes 4 and 5. For this operation I would choose the simpler frame + 1. In a frame containing several kinds of data, consider whether the same rule makes sense for every cell. Adding one to a product description has a different meaning from adding one to a quantity.
Mapping and selective replacement serve different purposes
To change selected values while retaining everything else, consider Series.replace. For the values N, D, X and a replacement dictionary containing only N: "new", replace retains D and X. Mapping through that incomplete dictionary instead produces missing values for them. Choose according to whether you need a complete code dictionary or a selective correction.
Do not treat mapping as an automatic substitute for joining arbitrary tables. If one key has several descriptions or you need several reference columns, establish the expected number of rows first. Investigate key uniqueness and the intended relationship. A convenient value-mapping method does not settle the underlying business definition of a reference table.
Display types and a small exercise
The pandas 3.0 changes include default inference of the str text dtype. Avoid assuming that every text result will display the historical dtype: object. Compare value meaning, and examine types in your environment when a later operation depends on them. The first example explicitly sets the input to object; it does not promise one universal result dtype for every mapping.
Add X: "review" to the first dictionary. Before using any tool, identify the one row that should gain a description: index 30. Row 40 still lacks an input code. Then decide how to document these different gaps in a report. That exercise checks the main decision behind a mapping operation: understanding why an output is missing, rather than merely replacing the visible blank.
Key takeaways
What happens to an unknown dictionary key?
An ordinary dictionary produces a missing value. Retain the input code to distinguish an unknown key from absent source data.
Does map work on a DataFrame?
Yes. DataFrame.map has existed since pandas 2.1.0 and accepts a callable applied to individual elements.
How does map differ from replace?
An incomplete ordinary dictionary produces missing outputs in map. Selective replacement through replace leaves values outside the rule unchanged.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
