map() (Pandas)

Series.map() substitutes or transforms each value in a Series, using a function, a dict, or another Series as the lookup. It is the simplest way to recode categories or apply a per-element transform.

Pandas

Pass a dict and map replaces each value with its dict entry — perfect for turning codes into labels, like {'M': 'Male', 'F': 'Female'}. Pass a function and it is applied to every element. Values with no match in a dict become NaN, which is often how you spot unexpected categories.

map works on a Series only. The DataFrame-wide equivalent is df.map() (which applies a function element-wise across every cell), while Series.apply() overlaps heavily with map but is meant more for functions than for dict lookups.

Because mapping with a dict is a vectorized lookup, it is fast and the idiomatic choice for category recoding compared with writing a manual loop or a long chain of replace calls.

Syntax

s.map(arg, na_action=None)

Examples

Python

import pandas as pd
s = pd.Series(["a", "b"])
print(s.map({"a": 1, "b": 2}))

Output:

0    1
1    2
dtype: int64

Learn it in a lesson

See also

Back to the Dictionary