Pandasdrill

Cleaning Updated November 2026

map

Series.map(arg) replaces each value using a dict, a Series (as a lookup table) or a function, returning a new Series. Values not found in the dict or Series become NaN, which makes map a quick way to recode categories or look up labels.

Syntax

Series.map(arg, na_action=None)
PartWhat it is
argA dict, a Series (index is the lookup key) or a callable
na_action"ignore" skips NaN values instead of passing them to the function

Examples

import pandas as pd

df = pd.DataFrame({"name": ["Ana", "Ben", "Cara"], "dept": ["S", "IT", "S"]})
df["dept_name"] = df["dept"].map({"S": "Sales", "IT": "Information Tech"})
df

Result: name dept dept_name 0 Ana S Sales 1 Ben IT Information Tech 2 Cara S Sales

df["dept"].map({"S": "Sales"})

Result: 0 Sales 1 NaN 2 Sales Name: dept, dtype: object

Keys missing from the dict give NaN; use replace to keep unmatched values unchanged.

rates = pd.Series({"S": 0.10, "IT": 0.15})
df["rate"] = df["dept"].map(rates)
df["rate"].tolist()

Result: [0.1, 0.15, 0.1]

A Series works as a lookup table: its index is matched against the values.

Common mistakes

  • Whole column turns into NaN: the dict keys do not match the column values exactly (type, case or whitespace); check df["col"].unique() first.
  • Using map to replace only some values wipes the rest: use df["col"].replace({...}) which leaves unmatched values alone.
  • Calling map on a DataFrame to use a dict raises or does something else; DataFrame.map applies a function per cell, so apply the dict column by column.

Practice map

These drills check your answer on live data, free, no account needed.

  • Rename columns and replace a placeholderRename the columns `Qty` to `qty`, `Unit Price` to `unit_price` and `Region` to `region`. Then replace the text "N/A" in the region column with "Unknown". Return the table.
  • Classify with applyWrite a function that labels an order "small" when qty is under 5, "medium" from 5 to 19, and "large" at 20 or more. Apply it to the `qty` column and return the labels as a Series.

Questions

map vs replace vs apply?

map rewrites every value from a dict/Series/function (unmatched become NaN); replace swaps only the listed values and keeps the rest; apply runs a function per value and is the same as map with a function.

How do I map values from another DataFrame?

Build a Series indexed by the key: lookup = other.set_index("id")["label"]; df["label"] = df["id"].map(lookup). This is faster than a merge for a single column.

Related

  • applyHow to use apply in pandas on a Series or DataFrame, with axis=1 for row-wise logic, lambdas, returning several columns, and why vectorized code is faster.
  • renameHow to rename columns in pandas with rename(columns={...}), rename every column with a function like str.lower, assign df.columns, and rename the index.
  • astypeHow to change a column type in pandas with astype and pd.to_numeric: text to numbers, float to int, strings, categories, nullable Int64, bad values and NaN.
  • fillnaHow to fill NaN values in pandas with fillna: a constant, a different value per column, the column mean or median, forward and backward fill, and by group.

Previous: applyNext: fillna