map, apply and DataFrame.map let you transform pandas data with your own functions – cleaning text, converting codes to names, creating grades or row-wise calculations.
What You Will Learn
- Series.map with a function or dictionary
- Series.apply with a lambda
- DataFrame.apply on columns and rows
- DataFrame.map for every cell (formerly applymap)
- When to use vectorised code instead
Prerequisites
Lambda Functions and Pandas DataFrame.
Example
import pandas as pd
df = pd.DataFrame({"name": ["aman", "riya", "karan", "neha"],
"city": ["LDH", "JAL", "LDH", "ASR"],
"marks": [82, 91, 68, 55],
"fee": [15000, 30000, 15000, 25000]})
df["name"] = df["name"].map(str.title) # function
df["city"] = df["city"].map({"LDH": "Ludhiana", "JAL": "Jalandhar", "ASR": "Amritsar"}) # dict
df["grade"] = df["marks"].apply(lambda m: "A" if m >= 85 else "B" if m >= 70 else "C")
df["fee_after_discount"] = df.apply(lambda r: r.fee * 0.9 if r.marks >= 80 else r.fee, axis=1)
print(df)
name city marks fee grade fee_after_discount Aman Ludhiana 82 15000 B 13500.0 Riya Jalandhar 91 30000 A 27000.0 Karan Ludhiana 68 15000 C 15000.0 Neha Amritsar 55 25000 C 25000.0
| Method | Works on | Use for |
|---|---|---|
Series.map(dict or func) |
Each value of one column | Code → name lookups, simple transforms (unmatched keys become NaN) |
Series.apply(func) |
Each value of one column | Custom logic like grades |
DataFrame.apply(func, axis=1) |
Each row | Calculations using several columns |
DataFrame.apply(func) |
Each column | Column summaries |
DataFrame.map(func) |
Every cell | Formatting all values (pandas ≥ 2.1; older: applymap) |
Column-wise and Cell-wise
print(df[["marks", "fee"]].apply(lambda col: col.max() - col.min()))
print(df[["marks"]].map(lambda x: f"{x}%").head(2))
marks 36 fee 15000 marks 82% 91%
Prefer Vectorised Code When Possible
import numpy as np
df["fee_after_discount"] = np.where(df["marks"] >= 80, df["fee"] * 0.9, df["fee"])
df["grade"] = pd.cut(df["marks"], bins=[0, 69, 84, 100], labels=["C", "B", "A"])
Explanation: apply(axis=1) runs Python code row by row and is slow on large data. np.where, pd.cut and column arithmetic run in optimised C and can be 10–100× faster on big DataFrames.
Series
Pandas series: Introduction · Series · DataFrame · Selection & Filtering · Missing Data · Apply & Map · DateTime · Merging · GroupBy · Pivot Tables
Interview Questions
Q: map vs apply on a Series?
map also accepts a dict or Series for lookups; apply only takes a function but supports extra arguments.
Q: Why avoid apply(axis=1) on big data?
It loops in Python; vectorised operations are much faster.
Practice Exercises
- Map state codes (PB, HR, DL) to state names.
- Create a “category” column: “High” if fee > 20000 else “Standard”, first with apply then with np.where.
Summary
Use map for lookups and simple transforms, apply for custom logic on a column or row, DataFrame.map for every cell – and vectorised NumPy/pandas operations for speed.
Next: Pandas DateTime · GroupBy
Machine Learning Tutorials, Courses and Certifications