Pandas DataFrame apply() Examples

Filed Under: Python
Pandas Dataframe Apply

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

References

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages