Pandas DataFrame Apply Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the apply() method in the Pandas library for Python. The apply() method allows us to apply a function to each row or column of a DataFrame, and it is useful for performing calculations or transformations on our data.

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/DataSelectionGroup(["`Data Selection`"]) pandas(("`Pandas`")) -.-> pandas/DataManipulationGroup(["`Data Manipulation`"]) 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/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") pandas/DataManipulationGroup -.-> pandas/add_new_columns("`Adding New Columns`") pandas/DataCleaningGroup -.-> pandas/data_mapping("`Data Mapping`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") 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`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/select_columns -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} pandas/add_new_columns -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} pandas/data_mapping -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} python/lists -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} python/tuples -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} python/dictionaries -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} python/function_definition -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} python/lambda_functions -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} python/importing_modules -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} python/numerical_computing -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} python/data_analysis -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} python/build_in_functions -.-> lab-68582{{"`Pandas DataFrame Apply Method`"}} end

Import the necessary libraries

To begin, we need to import the Pandas library, which provides the DataFrame object and the apply() method. We can also import other libraries if needed for our calculations.

import pandas as pd

Create a DataFrame

Next, let's create a DataFrame to work with. We can define the data using a Python list or a dictionary. For simplicity, let's use a dictionary where the keys represent the column names and the values represent the data.

data = {
    'Name': ['John', 'Emma', 'David', 'Mary'],
    'Age': [25, 30, 35, 40],
    'Salary': [50000, 60000, 70000, 80000]
}
df = pd.DataFrame(data)
print(df)

Define a function

Now, let's define a function that we can apply to our DataFrame. This function will take a single parameter, which will be a row or a column of the DataFrame.

def double(x):
    return x * 2

Apply the function to a column

We can use the apply() method to apply our defined function to a column of the DataFrame. In this example, let's apply the double() function to the 'Salary' column.

df['Salary'] = df['Salary'].apply(double)
print(df)

Apply the function to a row

We can also apply our function to a row of the DataFrame by specifying axis=1 in the apply() method. In this example, let's apply the double() function to each row of the DataFrame.

df = df.apply(double, axis=1)
print(df)

Apply a lambda function

Instead of defining a separate function, we can use a lambda function inline with the apply() method. A lambda function is a small anonymous function that can take any number of arguments and return a result. In this example, let's apply a lambda function to the 'Age' column to calculate the square of each value.

df['Age'] = df['Age'].apply(lambda x: x ** 2)
print(df)

Summary

In this lab, we learned how to use the apply() method in Pandas to apply a function to each row or column of a DataFrame. We saw how to define a function and apply it to a column or a row, as well as how to use a lambda function inline with the apply() method. The apply() method is a powerful tool for performing calculations or transformations on our data, and it can help to simplify our data analysis tasks.

Other Python Tutorials you may like