Skip to content

Comprehensive Guide to Adding Columns in Pandas DataFrames with assign()

Updated: at 03:44 AM

The Python Pandas library is a popular tool for data analysis and manipulation. One common task when working with Pandas DataFrames is adding new columns. There are a few different ways to add columns to a DataFrame, but one of the easiest methods is using the assign() function.

The assign() function allows you to easily create a copy of a DataFrame with new columns added, leaving the original DataFrame unchanged. The new columns can be based on calculations on existing columns, scalar values, or even functions.

In this comprehensive guide, we will cover the basics of the assign() function and how to use it to add new columns to Pandas DataFrames. We will look at:

Table of Contents

Open Table of Contents

What is assign() and Why Use It?

The assign() method is a function on Pandas DataFrames that allows you to easily create a copy of the DataFrame with new columns added, leaving the original DataFrame unchanged.

Here is the basic syntax:

df.assign(new_column1=values, new_column2=values, ...)

This makes a copy of df and adds the new columns specified. The parameters are the names of the new columns, followed by the values to populate them with.

The assign() method is useful because:

Let’s look at some simple examples of adding new columns with assign().

Adding Columns with Scalar Values

One straightforward use of assign() is to add a new column using a scalar (single) value:

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [4, 5, 6]})


This adds a new column C with the value 1 populated for all rows.

We can also specify a list or NumPy array instead of a scalar value to populate a column:

import numpy as np

df.assign(C=[1, 2, 3])

values = np.array([1, 2, 3])

This allows us to add new columns with any values we need for our analysis.

Adding Columns with Calculations on Existing Columns

More powerful uses of assign() involve creating new columns based on calculations performed on existing columns in the DataFrame.

For example, we can create a new column by adding the values from two existing columns:

df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [4, 5, 6]})

df.assign(C=df['A'] + df['B'])

We can also use more complex expressions and functions when adding columns:

import numpy as np

df = pd.DataFrame({'A': [1, 2, 3],
                   'B': [4, 5, 6]})

df.assign(C=np.sqrt(df['A']) + df['B'])

This allows us to derive new data and metrics for our analysis while keeping the original data intact.

Adding Columns with Functions

In addition to calculations on columns, we can also use functions to generate the values for new columns in assign().

For example, we can add a new column using the Python len() function to get string lengths:

df = pd.DataFrame({'A': [1, 2, 3],
                   'B': ['a', 'abc', 'def']})


The apply() method runs the function len on each value in column ‘B’.

We can also define our own custom functions and add columns based on them:

def add_one(x):
    return x + 1


This allows unlimited flexibility to derive new data from existing columns.

Using assign() on a Copy vs. Chaining

One important thing to note about assign() is that by default it returns a copy of the DataFrame with the new columns added, leaving the original DataFrame unchanged.

For example:

df = pd.DataFrame({'A': [1, 2, 3]})

new_df = df.assign(B=df['A'] + 1)

   A  B
0  1  2
1  2  3
2  3  4

0  1
1  2
2  3

The original df is unchanged. This avoids accidental modification of your data.

However, we can chain assign() to modify the original DataFrame in place:

df = pd.DataFrame({'A': [1, 2, 3]})

df = df.assign(B=df['A'] + 1)

   A  B
0  1  2
1  2  3
2  3  4

So be aware - chaining assign() will modify the original DataFrame rather than returning a copy!

Reassigning and Modifying Columns

The assign() method can also be used to reassign or modify existing columns in a DataFrame.

To replace the values in a column, we simply assign the new values to the column name:

df = pd.DataFrame({'A': [1, 2, 3]})

df.assign(A=[10, 11, 12])

This will replace the existing values in column ‘A’.

We can also use calculations or functions to modify existing columns:

import numpy as np

df = pd.DataFrame({'A': [1, 2, 3]})

df.assign(A=df['A'] * 2)


This allows in-place modifications of columns using assign().

Combining assign() with Other DataFrame Methods

The assign() method can be combined with other Pandas DataFrame functions and methods to enable more complex logic and transformations.

For example, we can add a column based on a conditional expression using np.where():

import numpy as np

df = pd.DataFrame({'A': [1, 5, 3]})

df.assign(B=np.where(df['A'] > 3, 100, 0))

This will populate column ‘B’ with 100 when column ‘A’ is greater than 3, else 0.

We can also add columns based on aggregations using groupby():

df = pd.DataFrame({'A': ['x', 'y', 'x'],
                   'B': [1, 2, 3]})


This calculates sum of B grouped by A and adds a column with total sum.

Chaining assign() with other methods like map(), apply(), join(), etc. can allow for advanced programmatic manipulation of DataFrames.

Performance Considerations for assign()

While assign() provides an easy way to add columns to DataFrames, be aware that it creates a copy of the data under the hood. This can have performance implications when working with large datasets.

Some tips to improve performance with assign():

In practice, assign() works very well for most datasets. But for production jobs with massive data, take care to minimize copying overhead.

Here is the conclusion for the article:


The Pandas assign() method provides a convenient way to add new columns to a DataFrame based on scalar values, existing columns, or functions. Key takeaways include:

By mastering the assign() function, you can quickly perform key operations like feature engineering, data transformations, and adding metrics or aggregate calculations to your DataFrames for analysis. The ability to derivation new data columns without impacting the original dataset makes assign() a very useful tool for any Pandas user.

With this comprehensive guide, you now have a solid foundation for how to add new columns to Pandas DataFrames using assign(). The examples and techniques covered provide a toolkit for wrangling and shaping data programmatically for your Python data science and analytics applications.