how to drop a column in pandas
To drop a column in pandas, you typically use the DataFrame drop() method
with axis=1 or columns=..., and optionally inplace=True if you want to
modify the original DataFrame directly.
Quick Scoop
Here’s the core pattern you’ll use again and again:
python
import pandas as pd
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6],
"C": [7, 8, 9],
})
# Drop one column, return a new DataFrame
df_new = df.drop("B", axis=1)
# Or, more explicit
df_new = df.drop(columns=["B"])
# Drop multiple columns
df_new = df.drop(columns=["B", "C"])
# Drop in place (changes df directly, returns None)
df.drop(columns=["B"], inplace=True)
Main ways to drop a column
1. Using drop() (most common)
-
Single column by name :
python
df = df.drop("col_name", axis=1)
or
df = df.drop(columns=["col_name"])
-
Multiple columns :
python
df = df.drop(columns=["col1", "col2"])
-
In-place modification :
python
df.drop(columns=["col1"], inplace=True)
This alters df directly and returns None.
-
Avoiding errors if column might not exist :
python
df = df.drop(columns=["maybe_there"], errors="ignore")
2. Using del (Python statement)
This is short and destructive (no copy):
python
del df["col_name"]
- Removes the column from
dfimmediately. - Does not return a new DataFrame.
Use this when you are sure the column exists and you want to mutate the DataFrame.
3. Using pop() (drop and return the series)
python
col_series = df.pop("col_name")
- Removes the column from
df. - Returns the removed column as a Series so you can reuse it elsewhere.
4. Drop by position (index)
Sometimes you only know the index of the column:
python
# Drop the second column (index 1)
df = df.drop(df.columns[1], axis=1)
You can generalize:
python
cols_to_drop = [0, 2] # column positions
df = df.drop(columns=df.columns[cols_to_drop])
5. Dropping columns conditionally
A few handy patterns when cleaning real-world data:
-
Drop non-numeric columns :
python
non_numeric = df.select_dtypes(exclude=["int64", "float64"]).columns df_num = df.drop(columns=non_numeric)
-
Drop by name pattern (e.g., prefix) :
python
cols_to_drop = [c for c in df.columns if c.startswith("temp_")] df = df.drop(columns=cols_to_drop)
-
Drop a contiguous label range (via
loc+drop):python
df = df.drop(columns=df.loc[:, "B":"D"].columns)
Mini forum-style snippets
Q: “I tried
df.drop('col')and nothing changed. Why?”
A: You likely forgotaxis=1orcolumns=..., or you didn’t assign back / useinplace=True.drop()returns a new DataFrame by default.
Q: “What’s the safest way if I’m not sure a column exists?”
A:df.drop(columns=["col"], errors="ignore")won’t crash on missing columns.
Tiny storytelling example
Imagine you’ve pulled a fresh CSV of “latest news” article stats with dozens
of metadata fields, but you only care about title, author, and
published_at for a quick analysis. You might first load everything, then
strip away the noise:
python
cols_to_keep = ["title", "author", "published_at"]
df = df[cols_to_keep]
or inversely:
python
cols_to_drop = [c for c in df.columns if c not in cols_to_keep]
df = df.drop(columns=cols_to_drop)
Now your DataFrame is lean and focused, which matters when you’re chaining multiple transformations or working with large datasets.
Quick checklist
- Want a new DataFrame? → Use
df.drop(columns=[...])and assign. - Want to modify in place? →
df.drop(columns=[...], inplace=True). - Want to keep the removed column? →
df.pop("col"). - Sure about the column and like brevity? →
del df["col"]. - Nervous about missing columns? → Add
errors="ignore"todrop().
TL;DR: The go-to pattern is:
python
df = df.drop(columns=["col_to_remove"])
# or
df.drop(columns=["col_to_remove"], inplace=True)
Information gathered from public forums or data available on the internet and portrayed here.