Python Central HubSign in

Data Analytics with Python / Phase 3: Data Manipulation with Pandas

Applying Functions (apply, map, applymap)

Transform values using vectorized operations, map, apply, and applymap, and learn when each is appropriate.

First: prefer vectorized operations

Pandas is fastest when you use vectorized operations.

Python
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

Python
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

Python
import pandas as pd
 
s = pd.Series([1, 2, 3, 4])
print(s.apply(lambda x: x * x))

Apply on rows (axis=1)

Python
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.

Python
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 map for simple value mapping (Series)
  • Use apply for custom logic on a Series or across rows/columns
  • Use applymap for element-wise transforms

Visualize it

diagram map, apply, applymap at a glance mermaid
map transforms one Series; apply works on a Series or DataFrame rows/columns; applymap runs map over every cell.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Map Values With a Dictionary

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Apply a Custom Function to a Series

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Apply Row-Wise With axis=1

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. What happens when s.map(state_map) meets a value that is not a key in the dict?

    Show answer

    B โ€” It becomes NaN โ€” Values missing from the mapping come back as NaN.

  2. Which call adds a final column by running final_score on each row?

    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.

  3. For a DataFrame df with columns a = [1, 2] and b = [3, 4], what does df.applymap(lambda x: x * 10) do?

    Show answer

    A โ€” Multiplies every element by 10 โ€” applymap applies the function element by element across the whole DataFrame.

  4. Which approach does the lesson say is fastest for adding 18 percent tax to an amount column?

    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).

Was this page helpful?

Let us know how we did