Pandas DataFrame Assign Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the assign() method in the Pandas library in Python. The assign() method allows us to add new columns to a DataFrame and returns a new DataFrame object with all the original columns along with the new ones. We can assign new columns directly or by using functions or expressions.

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`"]) 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`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") 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-68586{{"`Pandas DataFrame Assign Method`"}} python/lists -.-> lab-68586{{"`Pandas DataFrame Assign Method`"}} python/tuples -.-> lab-68586{{"`Pandas DataFrame Assign Method`"}} python/dictionaries -.-> lab-68586{{"`Pandas DataFrame Assign Method`"}} python/lambda_functions -.-> lab-68586{{"`Pandas DataFrame Assign Method`"}} python/importing_modules -.-> lab-68586{{"`Pandas DataFrame Assign Method`"}} python/numerical_computing -.-> lab-68586{{"`Pandas DataFrame Assign Method`"}} python/data_analysis -.-> lab-68586{{"`Pandas DataFrame Assign Method`"}} python/build_in_functions -.-> lab-68586{{"`Pandas DataFrame Assign Method`"}} end

Import the required libraries

First, let's import the necessary libraries: pandas and numpy.

import pandas as pd
import numpy as np

Create a DataFrame

Next, let's create a DataFrame containing some sample data. We will use the pd.DataFrame() function to create a DataFrame from a dictionary.

data = {'A': [1, 2, 3, 4, 5], 'B': [6, 7, 8, 9, 10]}
df = pd.DataFrame(data)
print(df)

The output will be:

   A   B
0  1   6
1  2   7
2  3   8
3  4   9
4  5  10

Assign a new column to the DataFrame

Now, let's assign a new column to the DataFrame using the assign() method. We can assign a new column directly by specifying the column name and values.

df = df.assign(C=[11, 12, 13, 14, 15])
print(df)

The output will be:

   A   B   C
0  1   6  11
1  2   7  12
2  3   8  13
3  4   9  14
4  5  10  15

Assign a new column using a function

We can also assign a new column to the DataFrame by passing a function to the assign() method. This function will take the DataFrame as an input and perform a computation to generate the values for the new column. Let's assign a new column D that calculates the value of C plus 1.

df = df.assign(D=lambda x: x['C'] + 1)
print(df)

The output will be:

   A   B   C   D
0  1   6  11  12
1  2   7  12  13
2  3   8  13  14
3  4   9  14  15
4  5  10  15  16

Assign multiple columns to the DataFrame

We can assign multiple columns to the DataFrame by using the assign() method multiple times. Let's assign two new columns E and F to the DataFrame. The column E will calculate the value of A plus 1, and the column F will calculate the value of B minus 1.

df = df.assign(E=lambda x: x['A'] + 1).assign(F=lambda x: x['B'] - 1)
print(df)

The output will be:

   A   B   C   D   E   F
0  1   6  11  12   2   5
1  2   7  12  13   3   6
2  3   8  13  14   4   7
3  4   9  14  15   5   8
4  5  10  15  16   6   9

Summary

In this lab, we learned how to use the assign() method in Pandas to add new columns to a DataFrame. We can assign new columns directly or using functions or expressions. This method allows us to easily manipulate and modify our DataFrame without changing the original data.

Other Python Tutorials you may like