Pandas DataFrame apply() function is used to apply a function along an axis of the DataFrame. The function syntax is:
def apply(
self,
func,
axis=0,
broadcast=None,
raw=False,
reduce=None,
result_type=None,
args=(),
**kwds
)
The important parameters are:
- func: The function to apply to each row or column of the DataFrame.
- axis: axis along which the function is applied. The possible values are {0 or ‘index’, 1 or ‘columns’}, default 0.
- args: The positional arguments to pass to the function. This is helpful when we have to pass additional arguments to the function.
- kwargs: additional keyword arguments to pass to the function. This is helpful when we have to pass additional keyword arguments to the function.
Pandas DataFrame apply() Examples
Let’s look at some examples of using apply() function on a DataFrame object.
1. Applying a Function to DataFrame Elements
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]})
def square(x):
return x * x
df1 = df.apply(square)
print(df)
print(df1)
Output:
A B
0 1 10
1 2 20
A B
0 1 100
1 4 400
The DataFrame on which apply() function is called remains unchanged. The apply() function returns a new DataFrame object after applying the function to its elements.
2. apply() with lambda
If you look at the above example, our square() function is very simple. We can easily convert it into a lambda function. We can create a lambda function while calling the apply() function.
df1 = df.apply(lambda x: x * x)
The output will remain the same as the last example.
3. apply() along axis
We can apply a function along the axis. But, in the last example, there is no use of the axis. The function is being applied to all the elements of the DataFrame.
The use of axis becomes clear when we call an aggregate function on the DataFrame rows or columns. Let’s say we want to get the sum of elements along the columns or indexes. The output will be different based on the value of the axis argument.
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]})
df1 = df.apply(np.sum, axis=0)
print(df1)
df1 = df.apply(np.sum, axis=1)
print(df1)
Output:
A 3
B 30
dtype: int64
0 11
1 22
dtype: int64
In the first example, the sum of elements along the column is calculated. Whereas in the second example, the sum of the elements along the row is calculated.
4. DataFrame apply() with arguments
Let’s say we want to apply a function that accepts more than one parameter. In that case, we can pass the additional parameters using the ‘args’ argument.
import pandas as pd
def sum(x, y, z):
return x + y + z
df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]})
df1 = df.apply(sum, args=(1, 2))
print(df1)
Output:
A B
0 4 13
1 5 23
5. DataFrame apply() with positional and keyword arguments
Let’s look at an example where we will use both ‘args’ and ‘kwargs’ parameters to pass positional and keyword arguments to the function.
import pandas as pd
def sum(x, y, z, m):
return (x + y + z) * m
df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]})
df1 = df.apply(sum, args=(1, 2), m=10)
print(df1)
Output:
A B
0 40 130
1 50 230
DataFrame applymap() function
If you want to apply a function element-wise, you can use applymap() function. This function doesn’t have additional arguments. The function is applied to each of the element and the returned value is used to create the result DataFrame object.
import pandas as pd
import math
df = pd.DataFrame({'A': [1, 4], 'B': [100, 400]})
df1 = df.applymap(math.sqrt)
print(df)
print(df1)
Output:
A B
0 1 100
1 4 400
A B
0 1.0 10.0
1 2.0 20.0
Let’s look at another example where we will use applymap() function to convert all the elements values to uppercase.
import pandas as pd
df = pd.DataFrame({'Name': ['Pankaj', 'Meghna'], 'Role': ['ceo', 'cto']})
df1 = df.applymap(str.upper)
print(df)
print(df1)
Output:
Name Role
0 Pankaj ceo
1 Meghna cto
Name Role
0 PANKAJ CEO
1 MEGHNA CTO
Hi,
I have one problem in which two columns have 10 values and all are same assume 890 in one column and 689 in another and i have 3rd column where values are like this =>value = [23, 45, 67, 89, 90, 234, 1098, 4567]
i want another column in which i have to add the value of third column and first compare it to 2nd column if it equals i have to stop adding for that column and then take next column i have to add values of 3rd column till its value equal to other column and collect its corresponding date where the sum has stopped since i will have one more column which contains a different date.
3980 0 2021-04-12 00:00:00 9.4
3980 0 2021-04-13 00:00:00 9.4
3980 0 2021-04-12 00:00:00 9.8
3980 0 2021-04-13 00:00:00 9.8
3980 0 2021-03-01 00:00:00 760
3980 0 2021-03-02 00:00:00 1630
3980 0 2021-03-03 00:00:00 1150
3980 0 2021-03-04 00:00:00 1000
3980 0 2021-03-05 00:00:00 20
3980 0 2021-03-08 00:00:00 210
3980 0 2021-03-09 00:00:00 340
3980 0 2021-03-10 00:00:00 150
3980 0 2021-03-11 00:00:00 160
3980 0 2021-03-12 00:00:00 50
3980 0 2021-03-15 00:00:00 10
3980 0 2021-03-16 00:00:00 350
3980 0 2021-03-17 00:00:00 200
3980 0 2021-03-18 00:00:00 50
If you find any solution please mail me
Thank you as I have been searching for ways to apply functions to pandas df as the current data is in insufficient!