applymap()
applymap applies a function to every single cell of a DataFrame.
In this page:
Syntax
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()
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
- Using applymap on a Series
- Using it where a vectorized operation exists
- 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: