apply, map & applymap

apply, map, and applymap are pandas methods that run your own function across data: map transforms a Series element by element, apply runs a function over a Series or over the rows and columns of a DataFrame, and applymap (now DataFrame.map) touches every single cell.

Learn apply, map & applymap in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Learn which tool to reach for, how the axis argument changes everything, and how lambdas keep your transformations short.

What You'll Learn in This Lesson

1 Series.apply() and Series.map()

On a single column, Series.apply(func) calls your function once per value and collects the results into a new Series. Series.map does the same when given a function, but it can also take a dictionary to recode values, which is perfect for relabelling categories.

2 DataFrame.apply: axis=0 vs axis=1

On a DataFrame, apply hands your function a whole Series at a time. With axis=0 (the default) each column is passed in, so you get one summary per column. With axis=1 each row is passed in, which is how you combine several columns into one new value.

3 Elementwise: applymap (now DataFrame.map)

When you want to transform every individual cell of a DataFrame the same way, use applymap . In pandas 2.1 and later this method was renamed DataFrame.map ; both do exactly the same elementwise work, so you may see either name.

🎯 Your Turn: Pass or Fail

Replace the blank so each score becomes "Pass" when it is 50 or more and "Fail" otherwise. The expected output is a Series with values Pass, Fail, Pass.

Common Errors

Without axis=1, apply walks columns and your row lookup like row["math"] fails or gives the wrong shape.

applymap and DataFrame.map only exist on DataFrames. On a single column use Series.apply or Series.map instead.

🎯 Mini Challenge: Order Summary

Recode a status column with map, then build a row total with a row-wise apply.

❓ Frequently Asked Questions

Lesson complete — you can transform data your way!

You now know when to use map for recoding, apply for flexible per-column or per-row logic, and applymap / DataFrame.map for every cell. Lambdas keep these transformations short and readable.

🚀 Up next: String Operations with .str — clean and reshape text columns.

Practice quiz

What can Series.map accept that Series.apply cannot?

  • A function
  • A lambda
  • A dictionary for recoding values
  • A Series of numbers

Answer: A dictionary for recoding values. map accepts a dict (or Series) to substitute values, as well as a function; apply only takes a function.

If you map a Series with a dict and a value's key is missing, what happens to that value?

  • It becomes NaN
  • It stays unchanged
  • It raises an error
  • It becomes 0

Answer: It becomes NaN. Values whose key is absent from the mapping dict become NaN.

What does the default axis=0 in DataFrame.apply pass to your function?

  • Each row as a Series
  • A single scalar
  • The whole DataFrame
  • Each column as a Series

Answer: Each column as a Series. axis=0 (the default) passes each column to the function, giving one result per column.

Which axis lets you combine several columns of the same row into one value?

  • axis=0
  • axis=1
  • axis=2
  • axis=-1 only

Answer: axis=1. axis=1 passes each row as a Series, so you can read multiple columns of that row.

Which method transforms every individual cell of a DataFrame the same way?

  • DataFrame.map (formerly applymap)
  • Series.map
  • groupby
  • merge

Answer: DataFrame.map (formerly applymap). applymap, renamed DataFrame.map in pandas 2.1+, applies a function elementwise to every cell.

What replaced the deprecated DataFrame.applymap in pandas 2.1?

  • DataFrame.apply
  • Series.map
  • DataFrame.map
  • DataFrame.transform

Answer: DataFrame.map. DataFrame.map does the same elementwise operation and is the modern name.

For df.apply(lambda col: col.max() - col.min()) on columns math=[80,90,70] and science=[60,85,95], what are the results?

  • math 70, science 60
  • math 20, science 35
  • math 10, science 10
  • An error

Answer: math 20, science 35. Per column: math 90-70=20, science 95-60=35.

Which call correctly computes a per-row total across columns 'a' and 'b'?

  • a

Row logic needs axis=1 so each row Series is passed in.

On a single column, why use apply with a function instead of map with a dict?

  • apply is always faster
  • apply lets you supply default logic for unknown values
  • map cannot take a function
  • map only works on numbers

Answer: apply lets you supply default logic for unknown values. A function in apply can handle any input, including a default for values a dict would map to NaN.

Which method does NOT exist on a Series (only on a DataFrame)?

  • apply
  • map
  • applymap
  • head

Answer: applymap. applymap and DataFrame.map are DataFrame-only; on a Series use apply or map.

Continue this course