modin.pandas.DataFrame.applymap¶
- DataFrame.applymap(func: PythonFuncType, na_action: str | None = None, **kwargs)[source]¶
Apply a function to a Dataframe elementwise.
This method applies a function that accepts and returns a scalar to every element of a DataFrame.
- Parameters:
func (callable) – Python function, returns a single value from a single value.
na_action ({None, 'ignore'}, default None) – If ‘ignore’, propagate NaN values, without passing them to func.
**kwargs – Additional keyword arguments to pass as keywords arguments to func.
- Returns:
Transformed DataFrame.
- Return type:
See also
Series.apply
: For applying more complex functions on a Series.DataFrame.apply
: Apply a function row-/column-wise.Examples
>>> df = pd.DataFrame([[1, 2.12], [3.356, 4.567]]) >>> df 0 1 0 1.000 2.120 1 3.356 4.567
>>> df.applymap(lambda x: len(str(x))) 0 1 0 3 4 1 5 5
When you use the applymap function, a user-defined function (UDF) is generated and applied to each column. However, in many cases, you can achieve the same results more efficiently by utilizing alternative dataframe operations instead of applymap. For example, You could square each number elementwise.
>>> df.applymap(lambda x: x**2) 0 1 0 1.000000 4.494400 1 11.262736 20.857489
But it’s better to avoid applymap in that case.
>>> df ** 2 0 1 0 1.000000 4.494400 1 11.262736 20.857489