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.

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