Mapping Values with map()
Learn how to transform Series values using dictionaries, functions and lambda expressions with Pandas map().
1. What is map() in Pandas?
The map() method is used to transform each value
in a Pandas Series. It can replace existing values with new
values by using a dictionary, another mapping Series or a
function.
It is especially useful when converting codes into readable labels, replacing categories, or applying a simple transformation to individual values.
import pandas as pd
numbers = pd.Series([1, 2, 3, 4])
result = numbers.map({
1: "One",
2: "Two",
3: "Three",
4: "Four"
})
print(result)
Output
0 One
1 Two
2 Three
3 Four
dtype: object
2. Syntax of map()
Series.map(arg, na_action=None)
| Parameter | Description |
|---|---|
arg |
A dictionary, Series or function used to map or transform values. |
na_action |
Use "ignore" to preserve missing
values without passing them to the mapping
function.
|
map() operates on a
Series. To transform several DataFrame columns or perform
row-wise calculations, consider other Pandas methods.
3. Mapping Values Using a Dictionary
A dictionary defines the relationship between original values
and replacement values. This is one of the most common uses
of map().
students = pd.Series([
"A", "B", "A", "C", "B"
])
grade_names = {
"A": "Excellent",
"B": "Good",
"C": "Average"
}
result = students.map(grade_names)
print(result)
Output
0 Excellent
1 Good
2 Excellent
3 Average
4 Good
dtype: object
Every value is looked up in the dictionary. The original Series remains unchanged unless you assign the result back to the same variable or column.
4. What Happens When a Key Is Missing?
When a dictionary does not contain a key for an existing
non-missing value, map() returns a missing value
for that item.
colors = pd.Series([
"red", "blue", "green"
])
color_codes = {
"red": "#FF0000",
"blue": "#0000FF"
}
result = colors.map(color_codes)
print(result)
Output
0 #FF0000
1 #0000FF
2 NaN
dtype: object
5. Mapping Values Using a Function
You can pass a Python function to map() when
you need to calculate a new value from each Series element.
prices = pd.Series([100, 200, 300, 400])
def add_tax(price):
return price * 1.18
result = prices.map(add_tax)
print(result)
Output
0 118.0
1 236.0
2 354.0
3 472.0
dtype: float64
In this example, each price is multiplied by 1.18 to calculate an illustrative price including 18% tax.
6. Using Lambda Expressions
A lambda expression is a compact way to define a small
function directly inside map().
numbers = pd.Series([10, 20, 30, 40])
result = numbers.map(lambda value: value * 2)
print(result)
Output
0 20
1 40
2 60
3 80
dtype: int64
Lambda expressions work well for short transformations. For complex conditions or reusable business rules, a named function is usually easier to understand.
7. Mapping Student Results
The following example converts numeric scores into descriptive performance categories.
marks = pd.Series([95, 72, 48, 85, 33])
def get_result(mark):
if mark >= 80:
return "Distinction"
elif mark >= 50:
return "Pass"
return "Fail"
result = marks.map(get_result)
print(result)
Output
0 Distinction
1 Pass
2 Fail
3 Distinction
4 Fail
dtype: object
8. Mapping DataFrame Columns
You can use map() on a DataFrame column to
replace codes with readable labels.
df = pd.DataFrame({
"Name": ["Amit", "Neha", "Raj"],
"Department": ["IT", "HR", "IT"]
})
department_names = {
"IT": "Information Technology",
"HR": "Human Resources"
}
df["Department"] = df["Department"].map(
department_names
)
print(df)
Output
Name Department
0 Amit Information Technology
1 Neha Human Resources
2 Raj Information Technology
9. Handling Missing Values
When a Series contains missing values, you can use
na_action="ignore" to prevent missing values
from being passed to a mapping function.
values = pd.Series([10, None, 30, 40])
result = values.map(
lambda value: value * 2,
na_action="ignore"
)
print(result)
Output
0 20.0
1 NaN
2 60.0
3 80.0
dtype: float64
Missing values remain missing. This option does not fill
them with a default value. Use fillna() if
your application requires a replacement.
10. map() vs apply() vs replace()
| Method | Primary purpose | Example |
|---|---|---|
map() |
Map or transform individual Series values. | s.map({"A": "Active"}) |
apply() |
Apply a function to Series values or DataFrame rows and columns. | s.apply(str.upper) |
replace() |
Replace specified values while generally preserving unmatched values. | s.replace({"A": "Active"}) |
map() can introduce missing values for
unmatched keys. If unmatched values should stay unchanged,
replace() may be more appropriate.
11. Common Mistakes and Best Practices
Common Mistakes
- Using
map()on an entire DataFrame instead of a Series. - Forgetting that unmatched dictionary keys can produce missing values.
- Using inconsistent capitalization in dictionary keys.
- Overwriting the original column unintentionally.
- Writing complicated logic inside a long lambda expression.
Best Practices
- Validate mapping keys before transforming data.
- Use descriptive dictionary names.
- Check missing values after mapping.
- Use named functions for complex transformations.
- Choose
map(),apply()orreplace()according to the requirement.
12. Practice Exercises
Exercise 1: Convert Status Codes
Convert the values 1, 2 and 3 into Pending, Approved
and Rejected using a dictionary and map().
status = pd.Series([1, 2, 3, 1, 2])
# Write your solution below
Exercise 2: Apply a Discount
Use a lambda expression to reduce every price by 10%.
prices = pd.Series([100, 250, 500, 1000])
# Write your solution below
Exercise 3: Map Department Names
Convert the department codes IT, HR and Finance into descriptive names.
departments = pd.Series([
"IT", "HR", "Finance", "IT"
])
# Write your solution below
Summary
Pandas map() transforms individual values in
a Series using a dictionary, Series, or function. It is
useful for replacing coded values, converting categories,
and performing simple transformations.
Remember that unmatched dictionary keys generally produce
missing values. Handle missing data carefully and select
apply() or replace() when those
methods better match the task.
Prefer map() for value mapping, validate your
mapping keys, and check the output before updating your
original DataFrame column.