First: prefer vectorized operations
Pandas is fastest when you use vectorized operations.
import pandas as pd
df = pd.DataFrame({"amount": [100, 200, 150]})
df["amount_with_tax"] = df["amount"] * 1.18
print(df)Use apply/map when you really need custom logic.
map() (Series only)
map transforms values in a single Series.
Example: map categories
import pandas as pd
s = pd.Series(["pune", "delhi", "mumbai"])
state_map = {"pune": "MH", "delhi": "DL", "mumbai": "MH"}
print(s.map(state_map))If a value is missing from the map, it becomes NaN.
apply() (Series or DataFrame)
Apply on a Series
import pandas as pd
s = pd.Series([1, 2, 3, 4])
print(s.apply(lambda x: x * x))Apply on rows (axis=1)
import pandas as pd
df = pd.DataFrame({
"name": ["Asha", "Ravi"],
"score": [88, 91],
"bonus": [5, 2],
})
def final_score(row):
return row["score"] + row["bonus"]
df["final"] = df.apply(final_score, axis=1)
print(df)Row-wise apply is easy to write but can be slower for large data.
applymap() (DataFrame element-wise)
Element-wise transform for the whole DataFrame.
import pandas as pd
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
print(df.applymap(lambda x: x * 10))Quick guidance
- Use vectorized operations if possible
- Use
mapfor simple value mapping (Series) - Use
applyfor custom logic on a Series or across rows/columns - Use
applymapfor element-wise transforms
Visualize it
flowchart LR A["Series.map(fn)"] --> R1["New Series, same shape"] B["Series/DataFrame.apply(fn)"] --> R2["Scalar, Series, or DataFrame"] C["DataFrame.applymap(fn)"] --> R3["New DataFrame, every cell transformed"]
๐งช Try It Yourself
Exercise 1 โ Map Values With a Dictionary
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Apply a Custom Function to a Series
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ Apply Row-Wise With axis=1
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
What happens when s.map(state_map) meets a value that is not a key in the dict?
Values missing from the mapping come back as NaN.
Show answer
B โ It becomes NaN โ Values missing from the mapping come back as NaN.
Which call adds a final column by running final_score on each row?
axis=1 passes each row to the function, so row["score"] + row["bonus"] works. axis=0 would pass whole columns.
Show answer
A โ df.apply(final_score, axis=1) โ axis=1 passes each row to the function, so row["score"] + row["bonus"] works. axis=0 would pass whole columns.
For a DataFrame df with columns a = [1, 2] and b = [3, 4], what does df.applymap(lambda x: x * 10) do?
applymap applies the function element by element across the whole DataFrame.
Show answer
A โ Multiplies every element by 10 โ applymap applies the function element by element across the whole DataFrame.
Which approach does the lesson say is fastest for adding 18 percent tax to an amount column?
Vectorized operations are the fastest. Use apply or map only when you need custom logic.
Show answer
A โ df["amount"] * 1.18 โ Vectorized operations are the fastest. Use apply or map only when you need custom logic.
Next
Custom transforms often need to handle gaps in the data โ continue to Handling Missing Data (isna, fillna, dropna).