🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.assign()

AI & DATA SCIENCE // df-assign

df.assign() returns a new DataFrame with one or more new (or modified) columns added, computed from a keyword argument, without mutating the original DataFrame.

Syntax

df.assign(**kwargs)

Deep Dive Course

Each keyword argument to assign() becomes a new column name, and its value, either a fixed value/Series or a function taking the DataFrame and returning a Series, becomes that column's data — passing a function specifically lets you reference other columns being computed in the same assign() call, evaluated in the order the keyword arguments are given. Unlike directly assigning to a new column on df, assign() always returns a brand-new DataFrame and never modifies the original in place, which makes it a natural fit for chaining a sequence of transformations together in one fluent expression.

1Understanding df.assign()

Each keyword argument to assign() becomes a new column name, and its value, either a fixed value/Series or a function taking the DataFrame and returning a Series, becomes that column's data — passing a function specifically lets you reference other columns being computed in the same assign() call, evaluated in the order the keyword arguments are given. Unlike directly assigning to a new column on df, assign() always returns a brand-new DataFrame and never modifies the original in place, which makes it a natural fit for chaining a sequence of transformations together in one fluent expression.

💡

Use a lambda inside assign() that receives the intermediate DataFrame as its parameter, instead of referencing the original df directly, when you need the new column to depend on another column also being created earlier in the same assign() call.

editor.html
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
new_df = df.assign(total=df["price"] * df["quantity"])
print(new_df)
localhost:3000

2Practical Example

Here is a real-world application of df.assign() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
result = df.assign(total=lambda d: d["price"] * d["quantity"], tax=lambda d: d["total"] * 0.08)
print(result)
localhost:3000

3Best Practices

Follow these guidelines when working with df.assign():

1. Use assign() when you want to add a computed column without mutating the original DataFrame, especially in the middle of a chained sequence of operations

2. Use a lambda referencing the DataFrame parameter, rather than the outer df variable, inside assign() when a new column depends on another one being created in the same call

3. Prefer plain direct column assignment for simple, one-off cases where the fluent, chainable style of assign() isn't actually needed

⚠️

Tip: Use a lambda inside assign() that receives the intermediate DataFrame as its parameter, instead of referencing the original df directly, when you need the new column to depend on another column also being created earlier in the same assign() call.

editor.html
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
new_df = df.assign(total=df["price"] * df["quantity"])
print(new_df)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
new_df = df.assign(total=df["price"] * df["quantity"])
print(new_df)
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
result = df.assign(total=lambda d: d["price"] * d["quantity"], tax=lambda d: d["total"] * 0.08)
print(result)

Best Practices

  • Use assign() when you want to add a computed column without mutating the original DataFrame, especially in the middle of a chained sequence of operations
  • Use a lambda referencing the DataFrame parameter, rather than the outer df variable, inside assign() when a new column depends on another one being created in the same call
  • Prefer plain direct column assignment for simple, one-off cases where the fluent, chainable style of assign() isn't actually needed

Interview Question

Why does the 'tax' column in the second example need to reference the intermediate DataFrame's 'total' column through a lambda, rather than directly referencing the outer df's 'total' column?

Hint: Think about whether the 'total' column actually exists on the original df at the moment assign() is called.

At the moment the whole assign() call is made, the original df doesn't have a 'total' column yet — it's only being created as part of this same assign() call, as one of its keyword arguments. Using a lambda that receives the intermediate DataFrame as its parameter lets assign() evaluate each keyword argument in order, passing along the progressively updated DataFrame, including columns created earlier in the same call, so referencing 'total' through that lambda parameter correctly sees the newly-created column, which referencing the original, unmodified outer df variable never would.

Exercises

MediumPractice using df.assign() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"price": [10, 20], "quantity": [3, 2]})
new_df = df.assign(total=df["price"] * df["quantity"])
print(new_df)

Frequently Asked Questions

Why does the 'tax' column in the second example need to reference the intermediate DataFrame's 'total' column through a lambda, rather than directly referencing the outer df's 'total' column?

At the moment the whole assign() call is made, the original df doesn't have a 'total' column yet — it's only being created as part of this same assign() call, as one of its keyword arguments. Using a lambda that receives the intermediate DataFrame as its parameter lets assign() evaluate each keyword argument in order, passing along the progressively updated DataFrame, including columns created earlier in the same call, so referencing 'total' through that lambda parameter correctly sees the newly-created column, which referencing the original, unmodified outer df variable never would.

Related Functions

df-evaldf-querylambda-functions