How to Drop a Column in Pandas DataFrame
Drop single or multiple columns by name or index cleanly without KeyError warnings.
Staff DevOps & Infrastructure Architect
Verified Command (Single-Click Copy)
Updated: 2026-10-29df = df.drop(columns=["unwanted_col", "temp_col"])đ Prerequisites
Safe to Run- âPython 3.8+
- âpandas >= 1.5.0
đĄ Primary Use Cases
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
âšī¸ Pandas 2.0+ uses Copy-on-Write (CoW) by default for zero-copy views.
Environment & Version Support
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.
df = df.drop("unwanted_col", axis=1)Drop only if column exists (Safe)
Avoids KeyError if the column might be missing.
df = df.drop(columns=["maybe_col"], errors="ignore")đ People Also Ask (Related Quick Answers)
df.dropna(axis=1, how="all")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").