pandas 1.4.2

NotesParametersReturnsBackRef
apply(self, func: 'AggFuncType', axis: 'Axis' = 0, raw: 'bool' = False, result_type=None, args=(), **kwargs)

Objects passed to the function are Series objects whose index is either the DataFrame's index ( axis=0 ) or the DataFrame's columns ( axis=1 ). By default ( result_type=None ), the final return type is inferred from the return type of the applied function. Otherwise, it depends on the result_type argument.

Notes

Functions that mutate the passed object can produce unexpected behavior or errors and are not supported. See gotchas.udf-mutation for more details.

Parameters

func : function

Function to apply to each column or row.

axis : {0 or 'index', 1 or 'columns'}, default 0

Axis along which the function is applied:

  • 0 or 'index': apply function to each column.

  • 1 or 'columns': apply function to each row.

raw : bool, default False

Determines if row or column is passed as a Series or ndarray object:

result_type : {'expand', 'reduce', 'broadcast', None}, default None

These only act when axis=1 (columns):

args : tuple

Positional arguments to pass to func in addition to the array/series.

**kwargs :

Additional keyword arguments to pass as keywords arguments to func .

Returns

Series or DataFrame

Result of applying func along the given axis of the DataFrame.

Apply a function along an axis of the DataFrame.

See Also

DataFrame.aggregate

Only perform aggregating type operations.

DataFrame.applymap

For elementwise operations.

DataFrame.transform

Only perform transforming type operations.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
... df A B 0 4 9 1 4 9 2 4 9

Using a numpy universal function (in this case the same as np.sqrt(df) ):

This example is valid syntax, but we were not able to check execution
>>> df.apply(np.sqrt)
     A    B
0  2.0  3.0
1  2.0  3.0
2  2.0  3.0

Using a reducing function on either axis

This example is valid syntax, but we were not able to check execution
>>> df.apply(np.sum, axis=0)
A    12
B    27
dtype: int64
This example is valid syntax, but we were not able to check execution
>>> df.apply(np.sum, axis=1)
0    13
1    13
2    13
dtype: int64

Returning a list-like will result in a Series

This example is valid syntax, but we were not able to check execution
>>> df.apply(lambda x: [1, 2], axis=1)
0    [1, 2]
1    [1, 2]
2    [1, 2]
dtype: object

Passing result_type='expand' will expand list-like results to columns of a Dataframe

This example is valid syntax, but we were not able to check execution
>>> df.apply(lambda x: [1, 2], axis=1, result_type='expand')
   0  1
0  1  2
1  1  2
2  1  2

Returning a Series inside the function is similar to passing result_type='expand' . The resulting column names will be the Series index.

This example is valid syntax, but we were not able to check execution
>>> df.apply(lambda x: pd.Series([1, 2], index=['foo', 'bar']), axis=1)
   foo  bar
0    1    2
1    1    2
2    1    2

Passing result_type='broadcast' will ensure the same shape result, whether list-like or scalar is returned by the function, and broadcast it along the axis. The resulting column names will be the originals.

This example is valid syntax, but we were not able to check execution
>>> df.apply(lambda x: [1, 2], axis=1, result_type='broadcast')
   A  B
0  1  2
1  1  2
2  1  2
See :

Back References

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

pandas.core.frame.DataFrame.applymap pandas.core.frame.DataFrame.transform pandas.core.frame.DataFrame.aggregate

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#8682
type: <class 'function'>
Commit: