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.
Functions that mutate the passed object can produce unexpected behavior or errors and are not supported. See gotchas.udf-mutation
for more details.
Function to apply to each column or row.
Axis along which the function is applied:
0 or 'index': apply function to each column.
1 or 'columns': apply function to each row.
Determines if row or column is passed as a Series or ndarray object:
These only act when axis=1
(columns):
Positional arguments to pass to func
in addition to the array/series.
Additional keyword arguments to pass as keywords arguments to func
.
Result of applying func
along the given axis of the DataFrame.
Apply a function along an axis of the DataFrame.
DataFrame.aggregate
Only perform aggregating type operations.
DataFrame.applymap
For elementwise operations.
DataFrame.transform
Only perform transforming type operations.
>>> 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)
):
>>> 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: int64This 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
>>> 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.
>>> 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.
>>> df.apply(lambda x: [1, 2], axis=1, result_type='broadcast') A B 0 1 2 1 1 2 2 1 2See :
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
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