pandas.DataFrame.apply() function with example in python | Apply Function in Pandas

Spread the love

pandas.DataFrame.apply() : This function apply a function along an axis of the DataFrame.

Syntax: DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)[source]

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.

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.
broadcast : bool, optional
Only relevant for aggregation functions:

False or None : returns a Series whose length is the length of the index or the number of columns (based on the axis parameter)
True : results will be broadcast to the original shape of the frame, the original index and columns will be retained.
Deprecated since version 0.23.0: This argument will be removed in a future version, replaced by result_type=’broadcast’.
raw : bool, default False
False : passes each row or column as a Series to the function.
True : the passed function will receive ndarray objects instead. If you are just applying a NumPy reduction function this will achieve much better performance.
reduce : bool or None, default None
Try to apply reduction procedures. If the DataFrame is empty, apply will use reduce to determine whether the result should be a Series or a DataFrame. If reduce=None (the default), apply’s return value will be guessed by calling func on an empty Series (note: while guessing, exceptions raised by func will be ignored). If reduce=True a Series will always be returned, and if reduce=False a DataFrame will always be returned.

Deprecated since version 0.23.0: This argument will be removed in a future version, replaced by result_type=’reduce’.
result_type : {‘expand’, ‘reduce’, ‘broadcast’, None}, default None
These only act when axis=1 (columns):

‘expand’ : list-like results will be turned into columns.
‘reduce’ : returns a Series if possible rather than expanding list-like results. This is the opposite of ‘expand’.
‘broadcast’ : results will be broadcast to the original shape of the DataFrame, the original index and columns will be retained.
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.

New in version 0.23.0.

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

Additional keyword arguments to pass as keywords arguments to func.

applied : Series or DataFrame

pandas.DataFrame.apply() function with program example

import pandas as pd import numpy as np df = pd.DataFrame({'x': [1, 2, 2,3,5,0], 'y': [10,20,30,40,50,60]}) print("Dataframe", df) print("Dataframe after sqrt function", df.apply(np.sqrt)) print("Dataframe after mean function", df.apply(np.mean)) print("Dataframe after mean function", df.apply(np.std))


Dataframe    x   y
0  1  10
1  2  20
2  2  30
3  3  40
4  5  50
5  0  60
Dataframe after sqrt function           x         y
0  1.000000  3.162278
1  1.414214  4.472136
2  1.414214  5.477226
3  1.732051  6.324555
4  2.236068  7.071068
5  0.000000  7.745967
Dataframe after mean function x     2.166667
y    35.000000
dtype: float64
Dataframe after mean function x     1.572330
y    17.078251
dtype: float64



Leave a Reply

Your email address will not be published. Required fields are marked *