Adding, Renaming & Dropping Columns
Python · Pandas Basics
Theory
The simplest way to add a column is assignment: df['gst'] = df['amount'] * 0.18 computes the value for every row at once. df.assign(gst=..., total=...) does the same but returns a new DataFrame, so it fits nicely in method chains, and a lambda lets one new column use another: assign(total=lambda d: d['amount'] + d['gst']). df.insert(1, 'col', values) places a column at a given position.
df.rename(columns={'amount': 'net_amount'}) renames selected columns (index= renames row labels), and df.columns = [...] replaces all names at once. df.drop(columns=['city']) removes columns and df.drop(index=[0, 2]) removes rows by label. These methods return a new DataFrame, so assign the result back: df = df.drop(columns='city'). Conditional columns use np.where(cond, a, b) or np.select for several cases, like SQL CASE WHEN.
Why we use it
Analysis needs derived fields - tax, totals, flags, categories - and tidy, consistent column names. These methods let you shape a table exactly as reports and joins need it.
When to use it
Use df['col'] = for quick one-off columns, assign() inside chains, rename() before a join so key names match, and drop() to remove helper or sensitive columns. Avoid inplace=True; assigning the result back is clearer.
How it works
- Start from df
- Need a new derived column? Yes: df['new'] = expr or df.assign(new=...). No: Skip.
- Names need changing? Yes: df.rename(columns={'old': 'new'}). No: Keep names.
- Columns or rows not needed? Yes: df.drop(columns=...) or drop(index=...). No: Keep them.
- Assign the result back: df = ...
- Reshaped column set
Syntax
df['new'] = expr
df.assign(new=expr)
df.insert(pos, 'name', values)
df.rename(columns={'old': 'new'})
df.drop(columns=[...]) / df.drop(index=[...])Example
df = df.assign(gst=df['amount'] * 0.18).rename(columns={'amount': 'net'})
df = df.drop(columns=['city'])Key points
- assign() returns a new DataFrame and works in method chains
- rename(columns={...}) changes only the names you list
- drop(columns=...) for columns, drop(index=...) for rows
- np.where / np.select give CASE WHEN-style conditional columns
- SQL equivalent: SELECT amount * 0.18 AS gst, amount AS net_amount ...