Pandas DataFrame: apply() function
DataFrame - apply() function
The apply() function is used to apply a function along an axis of the DataFrame.
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.
Syntax:
DataFrame.apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
func | Function to apply to each column or row. | function | Required |
axis | Axis along which the function is applied:
|
{0 or ‘index’, 1 or ‘columns’} Default Value: 0 |
Required |
raw |
|
bool Default Value: False |
Required |
result_type | These only act when axis=1 (columns):
The default behaviour (None) depends on the return value of the applied function: list-like results will be returned as a Series of those. However if the apply function returns a Series these are expanded to columns. |
{'expand', 'reduce', 'broadcast', None} Default Value: None |
Required |
args | Positional arguments to pass to func in addition to the array/series. | tuple | Required |
|
Additional keyword arguments to pass as keywords arguments to func. | Required |
Returns: Series or DataFrame
Result of applying func along the given axis of the DataFrame.
Example:
Download the Pandas DataFrame Notebooks from here.
Previous: DataFrame - combine_first() function
Next: DataFrame - applymap() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/dataframe/dataframe-apply.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics