pandas 1.4.2

ParametersReturnsBackRef
applymap(self, func: 'PythonFuncType', na_action: 'str | None' = None, **kwargs) -> 'DataFrame'

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.

versionadded
**kwargs :

Additional keyword arguments to pass as keywords arguments to func .

versionadded

Returns

DataFrame

Transformed DataFrame.

Apply a function to a Dataframe elementwise.

See Also

DataFrame.apply

Apply a function along input axis of DataFrame.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame([[1, 2.12], [3.356, 4.567]])
... df 0 1 0 1.000 2.120 1 3.356 4.567
This example is valid syntax, but we were not able to check execution
>>> df.applymap(lambda x: len(str(x)))
   0  1
0  3  4
1  5  5

Like Series.map, NA values can be ignored:

This example is valid syntax, but we were not able to check execution
>>> df_copy = df.copy()
... df_copy.iloc[0, 0] = pd.NA
... df_copy.applymap(lambda x: len(str(x)), na_action='ignore') 0 1 0 <NA> 4 1 5 5

Note that a vectorized version of func often exists, which will be much faster. You could square each number elementwise.

This example is valid syntax, but we were not able to check execution
>>> 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.

This example is valid syntax, but we were not able to check execution
>>> df ** 2
           0          1
0   1.000000   4.494400
1  11.262736  20.857489
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.frame.DataFrame.apply pandas.core.series.Series.map pandas.core.generic.NDFrame.pipe

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#8841
type: <class 'function'>
Commit: