3-MINUTES PANDAS

Unlocking the Power of assign() in Pandas

Creating new columns efficiently using Pandas' assign() method

Yufeng
4 min readJul 20, 2023

--

Photo by A. L. on Unsplash

Data manipulation is an everyday task for data analysis. One of the most used manipulations is to create new columns based on the existing ones in a data frame.

In this post, I will introduce a build-in method in Pandas, assign() , that can not only do the task in an efficient way but also improve the code readability.

The usage example of the assign() function

The assign() function can be directly applied to a data frame in Pandas, which allows you to create new columns using an expression or a callable function.

I’ll use the following example to illustrate how assign() is used to create multiple columns at the same time.


import pandas as pd
import numpy as np

# Creating a simple dataframe
df = pd.DataFrame({
‘Col1’: range(1, 6),
‘Col2’: range(10, 60, 10),
‘Col3’: range(100, 600, 100)
})

# Using assign to create new columns in place
df = df.assign(
Sum_12 = df.Col1 + df.Col2, # the sum of Col1 and Col2
Product_13 = df.Col1 * df.Col3, # The product of Col1 and Col3
Sqrt_1 = np.sqrt(df.Col1) # The squared root of Col1
)

--

--

Yufeng
Yufeng

Written by Yufeng

Ph.D., Data Scientist and Bioinformatician. Support my writing by becoming one of my referred members: https://jianan-lin.medium.com/membership

Responses (2)