Pandas
2 min read
Updated 4 Aug 2026
7. Working with Columns
df['bonus'] = df['salary'] 0.1 # create
df['bonus'] = df['salary'] * 0.1 # create
df['salary'] = df['salary'] + 1000 # update
df.drop(columns=['bonus']) # delete (returns copy)
df.insert(1, 'rank', [1, 2, 3]) # insert at position 1
# assign: chainable new columns
df = df.assign(net=df.salary - df.tax,
tier=lambda d: np.where(d.salary > 50000, 'high', 'low'))
df['name'] = df['name'].apply(str.upper) # apply a function per element
df['age2'] = df['age'].map(lambda x: x*2) # map (Series only)
df[['a','b']] = df[['a','b']].applymap(float) # elementwise on DataFrame
df.pipe(lambda d: d[d.age > 25]) # pipe for clean chaining
| Method | Scope | Use |
|---|---|---|
apply |
Series or DataFrame | apply function along axis / per element |
map |
Series only | element-wise map or dict lookup |
applymap |
DataFrame only | element-wise on every cell (now .map on DF) |
assign |
DataFrame | add columns in a chain |
pipe |
DataFrame | insert a custom function into a chain |
💡 Tip:
mapwith a dict is a fast lookup:df['code'].map({'A':1,'B':2}).
⚠️ Common Mistake:
applywith a Python function is slow. Prefer vectorized ops (df.a + df.b) ornp.wherewhen possible.
⭐ Interview Question:
applyvsmapvsapplymap?map= element-wise on a Series;applymap= element-wise on a whole DataFrame;apply= along an axis (row/column) or per element.