0
0
PandasComparisonBeginner · 3 min read

Map vs apply vs applymap pandas: Key Differences and Usage

map is used to map values of a Series according to an input correspondence, apply applies a function along an axis of a DataFrame or on a Series, and applymap applies a function elementwise to every cell in a DataFrame. Each serves different scopes: map for Series value mapping, apply for row/column-wise operations, and applymap for element-wise DataFrame transformations.
⚖️

Quick Comparison

Here is a quick table summarizing the main differences between map, apply, and applymap in pandas.

FunctionData TypeScopeInputOutputTypical Use Case
mapSeriesElement-wise on Series valuesdict, Series, or functionSeriesReplace or map Series values
applySeries or DataFrameRow-wise or column-wise on DataFrame or element-wise on SeriesfunctionSeries or DataFrameApply function along axis or on Series
applymapDataFrameElement-wise on all DataFrame cellsfunctionDataFrameApply function to each cell in DataFrame
⚖️

Key Differences

map is designed specifically for Series. It replaces or maps each value in the Series using a dictionary, another Series, or a function. It is not available for DataFrames.

apply is more flexible. For a DataFrame, it applies a function along either rows or columns (axis=0 or axis=1). For a Series, it applies a function element-wise. The output shape can vary depending on the function.

applymap works only on DataFrame objects and applies a function to every single element individually. It is useful for element-wise transformations when you want to change each cell's value.

⚖️

Code Comparison

Example: Convert a Series of letters to uppercase using map.

python
import pandas as pd

s = pd.Series(['a', 'b', 'c', 'd'])
result = s.map(str.upper)
print(result)
Output
0 A 1 B 2 C 3 D dtype: object
↔️

apply and applymap Equivalent

Equivalent operation using apply on a Series and applymap on a DataFrame.

python
import pandas as pd

# Using apply on Series
s = pd.Series(['a', 'b', 'c', 'd'])
result_apply = s.apply(str.upper)

# Using applymap on DataFrame

df = pd.DataFrame({'col1': ['a', 'b'], 'col2': ['c', 'd']})
result_applymap = df.applymap(str.upper)

print(result_apply)
print(result_applymap)
Output
0 A 1 B 2 C 3 D dtype: object col1 col2 0 A C 1 B D
🎯

When to Use Which

Choose map when you want to replace or map values in a Series using a dictionary or function. Use apply when you need to apply a function along rows or columns of a DataFrame or element-wise on a Series. Use applymap when you want to apply a function to every single element in a DataFrame individually.

In short, map is for simple value mapping on Series, apply is for flexible row/column or Series-wise operations, and applymap is for element-wise DataFrame transformations.

Key Takeaways

map is for mapping or replacing values in a Series using dict, Series, or function.
apply applies functions along DataFrame rows/columns or element-wise on Series.
applymap applies a function element-wise to every cell in a DataFrame.
Use map for simple value substitution, apply for flexible axis-wise operations, and applymap for element-wise DataFrame changes.