Pandas Function Applications – How to use pipe(), apply(), applymap()
Get Job-Ready: Data Analysis using Python with 70+ 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
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
Output:
1Â Â 11
2Â Â 21
3Â Â 31
4Â Â 41
5Â Â 51
dtype: int64
Now, using pipe() function application on Pandas series-
>>> dataflair_s1.pipe(adder,3)
Output:
1Â Â 14
2Â Â 24
3Â Â 34
4Â Â 44
5Â Â 54
dtype: int64
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
Output-
Now, using pipe() function application on Pandas DataFrame-
>>> dataflair_df1.pipe(adder,3)
Output:
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)
Output:
1Â 121
2Â Â 441
3Â Â 961
4Â Â 1681
5Â Â 2601
dtype: int64
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)
Output:
Applying function row-wise on Pandas DataFrames
>>> dataflair_df1.apply(sq, axis=1)
Output:
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)
Output:
Note: This method does not work on a Series.
Summary
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.
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google
good one sir
Tvs
keep it up