How to Use Pandas DataFrame apply() in Python
![](https://plantpot.works/wp-content/uploads/2021/09/7367.png)
Contents
In this article, you will learn how to use Pandas DataFrame apply() in Python.
Pandas DataFrame apply()
The apply() function in Pandas DataFrame allows you to apply a function to each row or column of the DataFrame. The apply() function is used to apply a function along an axis of the DataFrame.
Syntax
Here is the syntax of apply() function in Pandas DataFrame:
DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)
Parameters
func
: The function to be applied to each row or column of the DataFrame.axis
: The axis along which the function is applied. 0 means apply the function to each column, 1 means apply the function to each row.raw
: If True, apply the function to a Series of values. If False (default), apply the function to each column or row.result_type
: The type of the result. If None (default), the return type is inferred from the result of the function.args
: Additional arguments to pass to the function.
Example
Applying a function to each row of a DataFrame
Suppose we have a DataFrame with the following data:
import pandas as pd
data = {'Name': ['John', 'Paul', 'George', 'Ringo'],
'Age': [32, 21, 44, 55],
'Country': ['USA', 'UK', 'UK', 'USA']}
df = pd.DataFrame(data)
Name Age Country
0 John 32 USA
1 Paul 21 UK
2 George 44 UK
3 Ringo 55 USA
We can create a function that calculates the length of the name of each person in the DataFrame and apply it to each row of the DataFrame using the apply() function:
def name_length(row):
return len(row['Name'])
df['Name Length'] = df.apply(name_length, axis=1)
Name Age Country Name Length
0 John 32 USA 4
1 Paul 21 UK 4
2 George 44 UK 6
3 Ringo 55 USA 5
Here, we defined the function name_length that takes a row of the DataFrame as an argument and returns the length of the Name column for that row. Then we used the apply() function with axis=1 to apply this function to each row of the DataFrame, and we assigned the resulting series to a new column called Name Length.
Applying a function to each column of a DataFrame
Suppose we have a DataFrame with the following data:
import pandas as pd
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
A B C
0 1 4 7
1 2 5 8
2 3 6 9
We can create a function that adds a constant value to each column of the DataFrame and apply it to each column of the DataFrame using the apply() function:
def add_constant(col, constant):
return col + constant
df.apply(add_constant, axis=0, args=(10,))
A