Tutorial

Pandas DataFrame apply() Examples

Published on August 3, 2022
Pandas DataFrame apply() Examples

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author


Default avatar

Sr Technical Writer

Sr. Technical Writer@ DigitalOcean | Medium Top Writers(AI & ChatGPT) | 2M+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
June 23, 2021

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

- swetha

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 10, 2021

    Thank you as I have been searching for ways to apply functions to pandas df as the current data is in insufficient!

    - Carolyn

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Featured on Community

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more