Pandas Series Apply Method

PandasPandasBeginner
Practice Now

Introduction

The apply() method in pandas allows us to apply a function to the values of a Series. It can be used to apply either a Python method or a NumPy ufunc to the entire Series or to individual elements of the Series.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataCleaningGroup(["`Data Cleaning`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) pandas/DataCleaningGroup -.-> pandas/data_mapping("`Data Mapping`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") subgraph Lab Skills pandas/data_mapping -.-> lab-68734{{"`Pandas Series Apply Method`"}} python/lists -.-> lab-68734{{"`Pandas Series Apply Method`"}} python/tuples -.-> lab-68734{{"`Pandas Series Apply Method`"}} python/lambda_functions -.-> lab-68734{{"`Pandas Series Apply Method`"}} python/importing_modules -.-> lab-68734{{"`Pandas Series Apply Method`"}} python/numerical_computing -.-> lab-68734{{"`Pandas Series Apply Method`"}} python/data_analysis -.-> lab-68734{{"`Pandas Series Apply Method`"}} end

Create a Series

First, let's create a Series using the pd.Series() function from the pandas library. We will pass a list of numbers to the function to create the Series.

import pandas as pd

s = pd.Series([1, 2, 3])

Apply a NumPy ufunc

Next, we can apply a NumPy ufunc, such as np.pi, to the values of the Series. In this example, we will multiply each value of the Series by the value of pi using a lambda function.

import numpy as np

s.apply(lambda x: x * np.pi)

Apply a Python method

We can also apply a Python method to the Series. In this example, we will use the lower() method to convert the elements of the Series to lowercase.

s.apply(lambda x: x.lower())

Apply a function with condition

Lastly, we can apply a lambda function with a condition to the Series. In this example, we will check if each value in the Series is between 2 and 5 and return True or False.

s.apply(lambda x: x >= 2 and x <= 5)

Summary

The apply() method in pandas allows us to apply a function to the values of a Series. We can use it to apply a NumPy ufunc or a Python method to the entire Series or to individual elements of the Series. It is a versatile method that can be used to perform various operations on the elements of a Series.

Other Pandas Tutorials you may like