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.
| Function | Data Type | Scope | Input | Output | Typical Use Case |
|---|---|---|---|---|---|
| map | Series | Element-wise on Series values | dict, Series, or function | Series | Replace or map Series values |
| apply | Series or DataFrame | Row-wise or column-wise on DataFrame or element-wise on Series | function | Series or DataFrame | Apply function along axis or on Series |
| applymap | DataFrame | Element-wise on all DataFrame cells | function | DataFrame | Apply 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.
import pandas as pd s = pd.Series(['a', 'b', 'c', 'd']) result = s.map(str.upper) print(result)
apply and applymap Equivalent
Equivalent operation using apply on a Series and applymap on a DataFrame.
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)
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.map for simple value substitution, apply for flexible axis-wise operations, and applymap for element-wise DataFrame changes.