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
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework