Pandas Function Applications – How to use pipe(), apply(), applymap()
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
Keeping you updated with latest technology trends, Join DataFlair on Telegram
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.
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
Now, using pipe() function application 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
Now, 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.
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)
Applying function row-wise on Pandas DataFrames
>>> dataflair_df1.apply(sq, axis=1)
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)
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.