0
0
Pandasdata~5 mins

applymap() for DataFrame-wide operations in Pandas

Choose your learning style9 modes available
Introduction

We use applymap() to change or check every single value in a table (DataFrame) easily.

You want to add 1 to every number in a table of sales data.
You need to convert all text in a table to uppercase.
You want to check if each value in a table is positive or not.
You want to format all numbers in a table to show two decimal places.
Syntax
Pandas
DataFrame.applymap(function)

The function is applied to each cell in the DataFrame.

This works only on DataFrames, not on Series (single columns).

Examples
Adds 1 to every value in the DataFrame.
Pandas
df.applymap(lambda x: x + 1)
Converts every string in the DataFrame to uppercase.
Pandas
df.applymap(str.upper)
Checks if each value is greater than zero, returns True or False.
Pandas
df.applymap(lambda x: x > 0)
Sample Program

This code creates a table with numbers. Then it changes each number to the word 'Positive' if it is more than zero, or 'Non-positive' if it is zero or less.

Pandas
import pandas as pd

# Create a simple DataFrame
data = {'A': [1, -2, 3], 'B': [4, 5, -6]}
df = pd.DataFrame(data)

# Use applymap to check if values are positive
result = df.applymap(lambda x: 'Positive' if x > 0 else 'Non-positive')

print(result)
OutputSuccess
Important Notes

applymap() is slower than vectorized operations, so use it for simple or custom cell-wise changes.

For operations on rows or columns, use apply() instead.

Summary

applymap() changes every cell in a DataFrame using a function.

It is useful for cell-by-cell transformations or checks.

Works only on DataFrames, not on single columns or Series.