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

  1. Start from df
  2. Need a new derived column? Yes: df['new'] = expr or df.assign(new=...). No: Skip.
  3. Names need changing? Yes: df.rename(columns={'old': 'new'}). No: Keep names.
  4. Columns or rows not needed? Yes: df.drop(columns=...) or drop(index=...). No: Keep them.
  5. Assign the result back: df = ...
  6. 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

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic