Apply Function along an Axis of DataFrame
To apply a function along an axis of the DataFrame in Pandas, call apply() method on this DataFrame, and pass required values for the function to be applied, axis and other parameters.
In this tutorial, we will learn the syntax of DataFrame.apply() method and how to use this method to apply a function along an axis of this DataFrame, with examples..
Syntax
The syntax of pandas DataFrame.apply() method is
DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwargs)
where
Parameter | Value | Description |
---|---|---|
func | function | Function to apply to each column or row. |
axis | {0 or ‘index’, 1 or ‘columns’}. default value is 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 value is False. | Determines if row or column is passed as a Series or ndarray object: 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. |
result_type | {‘expand’, ‘reduce’, ‘broadcast’, None} default value is 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. |
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. |
Return Value
DataFrame.
Examples
Apply Numpy Square Root Function on DataFrame
In the following program, we take a DataFrame, and apply numpy.sqrt() function for each element of this DataFrame.
Example.py
import pandas as pd
import numpy as np
df = pd.DataFrame(
{'col_1': [4, 9, 25], 'col_2': [16, 81, 64]})
result = df.apply(np.sqrt)
print(result)
Output
col_1 col_2
0 2.0 4.0
1 3.0 9.0
2 5.0 8.0
Apply Numpy Sum Function along Index Axis of DataFrame
In the following program, we take a DataFrame, and apply numpy.sum() function along index axis (axis=0).
Example.py
import pandas as pd
import numpy as np
df = pd.DataFrame(
{'col_1': [4, 9, 25], 'col_2': [16, 81, 64]})
result = df.apply(np.sum, axis=0)
print(result)
Output
col_1 38
col_2 161
dtype: int64
Apply Numpy Sum Function along Column Axis of DataFrame
In the following program, we take a DataFrame, and apply numpy.sum() function along column axis (axis=1).
Example.py
import pandas as pd
import numpy as np
df = pd.DataFrame(
{'col_1': [4, 9, 25], 'col_2': [16, 81, 64]})
result = df.apply(np.sum, axis=1)
print(result)
Output
0 20
1 90
2 89
dtype: int64
Conclusion
In this Pandas Tutorial, we learned the syntax of DataFrame.apply() method, how to use this method to apply a function to each of the element in DataFrame, along an axis, using pandas DataFrame.apply() method.