⚡DevCheats
Cloudflare Edge
🐍Data & Backend â€ĸ Python & PandasVerified for Pandas 1.5+ & Pandas 2.2+âąī¸ 30 secondsđŸŽ¯ Beginner

How to Drop a Column in Pandas DataFrame

Drop single or multiple columns by name or index cleanly without KeyError warnings.

Alex Chen
Alex ChenReviewed by Expert

Staff DevOps & Infrastructure Architect

📖3 min read
â€ĸ
📝440 words
â€ĸ
📅Updated 2026-10-29

Verified Command (Single-Click Copy)

Updated: 2026-10-29
python
df = df.drop(columns=["unwanted_col", "temp_col"])
Share snippet:𝕏in

📋 Prerequisites

Safe to Run
  • ✓Python 3.8+
  • ✓pandas >= 1.5.0

💡 Primary Use Cases

Data Preprocessing for Machine Learning:

Remove leakage features or high-cardinality IDs.

# How It Works

Using the `columns=[...]` parameter is explicit, clean, and avoids ambiguity between row indexes and column labels.

Expected Terminal Output

# Before: Columns: ['id', 'name', 'unwanted_col']
# After:  Columns: ['id', 'name']
âš ī¸

Gotcha & Common Pitfall

Avoid using `inplace=True` in modern Pandas (2.0+). It does not improve memory and breaks method chaining.

⚡

Performance & Complexity

Time ComplexityO(N)
Space (Memory)O(N) (Copy-on-Write)
Mutates OriginalNo (Pure/Copy)

â„šī¸ Pandas 2.0+ uses Copy-on-Write (CoW) by default for zero-copy views.

✓

Environment & Version Support

Pandas
2.0+Verified
Polars
0.20+Verified
Recommended Tool â€ĸ DataCamp

Zero to Data Analyst in Python

Hands-on interactive Python, Pandas, and SQL tracks. 2,000+ real-world exercises and portfolio projects.

# Alternative Methods & Variations

Drop by axis parameter

Classic syntax using axis=1.

python
df = df.drop("unwanted_col", axis=1)

Drop only if column exists (Safe)

Avoids KeyError if the column might be missing.

python
df = df.drop(columns=["maybe_col"], errors="ignore")

🔍 People Also Ask (Related Quick Answers)

How to drop columns containing NaN in Pandas?df.dropna(axis=1, how="all")
How to rename columns in Pandas?df.rename(columns={"old": "new"})
?

Frequently Asked Questions about How to Drop a Column in Pandas DataFrame

How do I drop columns by index position instead of column name?â–ŧ

Use df.drop(df.columns[[0, 2]], axis=1) to drop columns by numerical positions.

How to drop columns if they exist without raising KeyError?â–ŧ

Add errors="ignore": df.drop(columns=["col"], errors="ignore").

đŸ’ŦWas this snippet helpful for your problem?
← Previous Snippet

How to Reverse a List in Python (Best Practices)

Next Snippet →

Git Undo Last Commit (Keep Changes Staged)

Tags:#python#pandas#dataframe#data science

More Python & Pandas Snippets

View full Python & Pandas cheatsheet →