Add or replace columns inside a method chain, without mutating the original data frame.
assign is how a chain grows new columns. Writing df['millions'] = ... modifies the frame in place and cannot appear inside a chain; assign returns a new frame with the column added, so the chain keeps flowing.
Official documentation: DataFrame.assign
The forms worth knowing
# One new column
df.assign(millions=pd.col('Hours Viewed') / 1_000_000)
# Several at once -- evaluated in order, left to right
df.assign(millions=pd.col('Hours Viewed') / 1_000_000,
rounded=pd.col('millions').round(0))
# Replace an existing column by assigning to its own name
df.assign(mag=pd.col('mag').round(1))
# A name that is not a valid Python identifier needs the ** form
df.assign(**{'Hours (millions)': pd.col('Hours Viewed') / 1_000_000})
That second form is worth pausing on: assign evaluates its keyword arguments in order, so a later column can refer to one created earlier in the same call. That is not true of a plain dictionary of columns, and it saves chaining two separate assign calls.
A worked example, on real data
Netflix published an engagement report listing every title watched over six months. Bamboo Weekly #45 used it.
Hours viewed run into the hundreds of millions, which is hard to read. Let's derive something friendlier, and a flag for worldwide availability:
import pandas as pd
url = ('https://www.bambooweekly.com/content/files/4cd45et68cgf/1HyknFM84ISQpeua6TjM7A/'
'97a0a393098937a8f29c9d29c48dbfa8/'
'what_we_watched_a_netflix_engagement_report_2023jan-jun.xlsx')
(
pd.read_excel(url, skiprows=5,
usecols=['Title', 'Available Globally?', 'Hours Viewed'])
.assign(millions=pd.col('Hours Viewed') / 1_000_000,
worldwide=pd.col('Available Globally?') == 'Yes',
rounded=pd.col('millions').round(0))
.sort_values('millions', ascending=False)
.head(4)
[['Title', 'worldwide', 'millions', 'rounded']]
)
Which gives:
Title worldwide millions rounded
The Night Agent: Season 1 True 812.1 812.0
Ginny & Georgia: Season 2 True 665.1 665.0
The Glory: Season 1 // 더 글로... True 622.8 623.0
Wednesday: Season 1 True 507.7 508.0
Three columns created in one call, and rounded is computed from millions, which did not exist a line earlier. The original data frame is untouched — nothing was assigned to a variable, and read_excel's result was never modified.
Three mistakes people make
Using df['new'] = ... inside what should be a chain. Bracket assignment returns None and mutates in place, so it cannot be chained and it destroys the original. assign exists precisely to avoid both.
Expecting assign to modify the original. It does not. df.assign(x=...) returns a new frame; if you do not keep the result, nothing happened. This surprises people coming from bracket assignment, and it is the whole point — the original stays clean.
Forgetting the ** form for awkward column names. Keyword arguments must be valid Python identifiers, so df.assign(Hours Viewed=...) is a syntax error and df.assign(2024=...) is too. Pass a dictionary instead: df.assign(**{'Hours Viewed': ...}).
Watch it
assign is one leg of the method-chaining style: Method chaining in Pandas: Cleaner queries with assign, loc, and lambda.
Practice it
Work through an assign exercise, with instant feedback and no signup required: practice.lernerpython.com/bamboo-weekly/assign/
Go deeper
Bamboo Weekly is the practice. If you want the structured version — full courses with downloadable Jupyter notebooks, plus live Pandas office hours when you get stuck — that is LernerPython+Data. A paid Bamboo Weekly subscription is included with it.
See it on real data
Below are the 111 Bamboo Weekly exercises that use assign on real-world data — try each one, then study the worked solution.
- Bamboo Weekly #182: Surveillance technology
- Bamboo Weekly #181: Housing costs
- Bamboo Weekly #180: Movies
- Bamboo Weekly #179: Krakow tourism
- Bamboo Weekly #178: Harmful algal bloom
- Bamboo Weekly #177: European Summer
- Bamboo Weekly #175: Inflation
- Bamboo Weekly #174: Vacation
- Bamboo Weekly #173: IPOs
- Bamboo Weekly #172: World Cup
- Bamboo Weekly #171: Hantavirus
- Bamboo Weekly #170: Port of Long Beach
- Bamboo Weekly #169: Press freedom
- Bamboo Weekly #168: US gas prices
- Bamboo Weekly #167: Oil prices
- Bamboo Weekly #166: Income tax
- Bamboo Weekly #165: Artemis II
- Bamboo Weekly #164: Fertilizer
- Bamboo Weekly #163: Daylight saving time
- Bamboo Weekly #162: Spotify and car accidents
- Bamboo Weekly #161: Missiles in Israel
- Bamboo Weekly #160: Strait of Hormuz
- Bamboo Weekly #159: State of the Union
- Bamboo Weekly #158: University endowments
- Bamboo Weekly #157: Government corruption
- Bamboo Weekly #156: Winter Olympics
- Bamboo Weekly #155: Gold
- Bamboo Weekly #154: University rankings
- Bamboo Weekly #153: Venezuela
- Bamboo Weekly #152: Congestion pricing
- Bamboo Weekly #151: PyPI in 2025
- Bamboo Weekly #150: Kalshi
- Bamboo Weekly #149: Flu season
- Bamboo Weekly #148: US Manufacturing
- Bamboo Weekly #147: Presidential pardons
- Bamboo Weekly #146: Thanksgiving travel
- Bamboo Weekly #144: Museum Heists
- Bamboo Weekly #143: Phones in school
- Bamboo Weekly #140: Stack Overflow survey
- Bamboo Weekly #137: UN Security Council
- Bamboo Weekly #136: Indian vehicles
- Bamboo Weekly #135: Airline seats
- Bamboo Weekly #134: Taiwan weather
- Bamboo Weekly #133: Wind power
- Bamboo Weekly #132: JetBrains survey
- Bamboo Weekly #129: Tom Lehrer
- Bamboo Weekly #128: Extreme heat
- Bamboo Weekly #126: EV sales
- Bamboo Weekly #125: Shrinking dollars
- Bamboo Weekly #124: NATO Spending
- Bamboo Weekly #123: Missiles
- Bamboo Weekly #122: Economic growth
- Bamboo Weekly #121: Research funding
- Bamboo Weekly #120: Pennies
- Bamboo Weekly #119: Python conferences
- Bamboo Weekly #118: Flight delays
- Bamboo Weekly #117: Electricity
- Bamboo Weekly #115: Sahm rule
- Bamboo Weekly #113: US airport traffic
- Bamboo Weekly #111: State taxes
- Bamboo Weekly #108: Measles
- Bamboo Weekly #107: Consumer confidence
- Bamboo Weekly #105: Federal employees
- Bamboo Weekly #104: Aviation accidents
- Bamboo Weekly #102: WordPress
- Bamboo Weekly #101: Los Angeles Fires
- Bamboo Weekly #100: Sports betting
- Bamboo Weekly #99: Literacy and numeracy
- Bamboo Weekly #96: Taylor Swift
- Bamboo Weekly #94: Strategic Wine Reserve
- Bamboo Weekly #93: Anti-politics
- Bamboo Weekly #91: Roller coasters
- Bamboo Weekly #89: Housing
- Bamboo Weekly #87: Nuclear power
- Bamboo Weekly #86: FEMA
- Bamboo Weekly #84: Central banks
- Bamboo Weekly #81: School
- Bamboo Weekly #79: Cyber attacks
- Bamboo Weekly #78: Stock markets
- Bamboo Weekly #77: Paris Olympics
- Bamboo Weekly #76: Aging legislators
- Bamboo Weekly #75: Refugees
- Bamboo Weekly #74: UK elections
- Bamboo Weekly #73: Avocado hand
- Bamboo Weekly #72: City travel
- Bamboo Weekly #71: Holidays
- Bamboo Weekly #70: Moon missions
- Bamboo Weekly #68: Dangerously hot weather
- Bamboo Weekly #67: Electric cars
- Bamboo Weekly #63: Ukraine aid
- Bamboo Weekly #61: Solar eclipse
- Bamboo Weekly #60: Iceland
- Bamboo Weekly #58: NATO
- Bamboo Weekly #56: Rent increases
- Bamboo Weekly #54: Household debt
- Bamboo Weekly #52: Border encounters
- Bamboo Weekly #51: Academy Awards
- Bamboo Weekly #49: Campaign finance
- Bamboo Weekly #48: Aviation accidents
- Bamboo Weekly #46: Pedestrians
- Bamboo Weekly #45: Netflix
- Bamboo Weekly #44: Global economics
- Bamboo Weekly #43: Financial protection
- Bamboo Weekly #42: Plant hardiness
- Bamboo Weekly #40: Sovereign Bonds
- Bamboo Weekly #36: Nobel Prize
- Bamboo Weekly #35: Terrorism
- Bamboo Weekly #34: House of Representatives
- Bamboo Weekly #31: Poverty
- Bamboo Weekly #30: Uncertainty
- Bamboo Weekly #28: Pret a Manger
Part of the Pandas Methods Index. See also practice by skill.