Pandas Function Applications – How to use pipe(), apply(), applymap()

Free Pandas course with real-time projects Start Now!!

While coding, one has to apply functions to Pandas objects. To apply these pandas function applications – pipe(), apply(), and applymap(), you should know these three important methods. The knowledge of these methods helps us to choose the method of application wisely while coding. The appropriate method for applying the functions depends on whether your function expects to operate element-wise, row wise, or column wise.

  • pipe(): Table wise function applications in Pandas
  • apply(): Row or column wise function operation
  • applymap(): Element-wise function applications in Pandas

How to use function applications in Pandas

Pandas Function Applications

Before we explore the pandas function applications, we need to import pandas and numpy-

>>> import pandas as pd
>>> import numpy as np

1. Table Wise Function Application: pipe()

The custom operations performed by passing a function and an appropriate number of parameters. These are known as pipe arguments. Hence, the operation is performed on the entire DataFrame or Series. When we want to apply one function to a series or DataFrame, then apply another, then another, and so on, the notation can become messy. It can also makes the program more prone to error. Here, pipe() becomes useful.

Define the pipe() function application in Python Pandas

>>> def adder(ele1,ele2):
         return ele1+ele2

It’s the right time to enhance your skills for Pandas Basic Functionality.

import pandas and numpy

Let’s see how pipe() function application works in Pandas Series and DataFrame-

1.1. Using pipe() Functions Application on Pandas Series

First, we create a Pandas Series-

>>> dataflair_s1 = pd.Series([11,21,31,41,51])
>>> dataflair_s1


1   11
2   21
3   31
4   41
5   51
dtype: int64

How to generate a Series in Pandas

Now, using pipe() function application on Pandas series-

>>> dataflair_s1.pipe(adder,3)


1   14
2   24
3   34
4   44
5   54
dtype: int64

How to use pipe function on Pandas Series

Grab these easy steps to Sort Pandas Series and DataFrames

1.2. Using pipe() Function Application on Pandas DataFrame

Creating a DataFrame in Pandas

>>> dataflair_df1=pd.DataFrame(6*np.random.randn(6,3),columns=['c1','c2','c3'])
>>> dataflair_df1


How to Create Pandas Dataframes

Now, using pipe() function application on Pandas DataFrame-

>>> dataflair_df1.pipe(adder,3)


Using using pipe() function application on Pandas DataFrame

Note: Moving forward, we will be using the same DataFrame and Series to avoid any confusion.

2. Row or Column Wise Function Operations: apply()

You may apply arbitrary functions to the axes of a DataFrame or Panel by using the apply() method. It can also be applied to a Series. It takes an optional axis argument. By default, the operation will be performed column-wise, taking every column as an array. It enables the user, to pass a function and then apply it to all the values of the DataFrame or Series. It is a huge improvement for the library as it allows the segregation of data according to the given conditions, making it efficiently usable in machine learning and data science.

Defining the apply() function

>>> def sq(ele1):
      return ele1*ele1

2.1 Applying apply() functions on Pandas Series

Using apply() functions on Pandas Series.

>>> dataflair_s1.apply(sq)


1  121
2   441
3   961
4   1681
5   2601
dtype: int64

How to apply a function on Pandas Series

Get a Complete Difference Between Machine Learning and Data Science

2.2 Applying apply() functions on Pandas DataFrame

Applying function column-wise on Pandas DataFrames

>>> dataflair_df1.apply(sq, axis=0)


How to apply() function coloumn wise on dataframes

Applying function row-wise on Pandas DataFrames

>>> dataflair_df1.apply(sq, axis=1)


Applying function row-wise on Pandas DataFrames
3. Element-wise Function Application: applymap()

Every function cannot be vectorized. The method applymap() on DataFrame is capable of taking and returning a single value. This Pandas function application is used to apply a function to DataFrame, that accepts and returns only one scalar value to every element of the DataFrame. It is a Data-centric method of applying functions to DataFrames. We use the word lambda to define the functions.

>>> dataflair_df1.applymap(lambda x: x**2)


Applying function row-wise on Pandas DataFrames

Note: This method does not work on a Series.


We have learned how to apply functions to pandas and the various situations in which each method is beneficial. Using them in specific cases will not only make your program elegant, presentable, and easy to use, but also remove any possibilities of errors or bugs. Knowing all of these is key to becoming an efficient programmer.

Take the next tutorial on – Panel in Pandas and build your skills for Data Scientist.

If you face any problems while using Pandas Function Applications, feel free to ask in the comments.


If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

3 Responses

  1. Samrat Singh Rathore says:

    good one sir

  2. Dipak nayak says:


  3. mohammed seid says:

    keep it up

Leave a Reply

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