← Back to Pandas Course | Chapter 6: Data Operations | Lesson 3 of 7

applymap()

applymap applies a function to every single cell of a DataFrame.

In this page:

  1. applymap()
Syntax
python
df.applymap(function_name)

applymap()

DataFrame.applymap(func) calls func on each element and returns a DataFrame of the same shape. It is handy for formatting an entire table. In pandas 2.1 and later the same method is named DataFrame.map, and applymap is deprecated.

Note: Use applymap for pandas 1.x and DataFrame.map for pandas 2.1 and newer.

Example: applymap()

python
import pandas as pd

df = pd.DataFrame({"a": [1.234, 2.345], "b": [3.456, 4.567]})
print(df.applymap(lambda x: round(x, 1)))

# Output:
#      a    b
# 0  1.2  3.5
# 1  2.3  4.6
Related Topics
Common Mistakes
  1. Using applymap on a Series
  2. Using it where a vectorized operation exists
  3. Ignoring the deprecation in newer versions
Chapter Summary
  • applymap works cell by cell
  • It returns a same-shape DataFrame
  • Series use map instead
  • Renamed DataFrame.map in pandas 2.1
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.