How to Drop a Column From Pandas DataFrame?

pandas drop column

Imagine you’re working with a massive dataset, but half the columns are useless clutter—extra details you don’t need, slowing down your analysis. Maybe you’re merging CSV files, cleaning up data for machine learning, or just trying to make sense of messy information. You need a clean, structured dataset—and that means dropping unnecessary columns. Let’s dive into the best ways to remove them efficiently in Pandas!

Learn how to delete one or more columns from a Pandas DataFrame in Python.

Table of Contents

Remove Columns From Pandas DataFrame

The primary method to remove a column in Pandas is by using the drop() function. Before dropping columns, you might first want to retrieve all column names from your dataset. Learn how to do this in How to Get Column Names from a Pandas DataFrame?.

Drop a Single Column in Pandas

Use the drop() method with axis=1 to remove a column:

Syntax

DataFrame.drop("column_name", axis=1, inplace=False)
  • "column_name" – Name of the column to remove.
  • axis=1 – Specifies that you’re deleting a column (not a row).
  • inplace=False – By default, this creates a new DataFrame without modifying the original.

Example Code

Here, the “Age” column is removed, but the original DataFrame remains unchanged.

import pandas as pd

# Creating a sample DataFrame
data = {"Name": ["Alice", "Bob", "Charlie"], 
        "Age": [23, 36, 56], 
        "Location": ["New York", "London", "Berlin"]}

df = pd.DataFrame(data)

# Dropping the "Age" column
new_df = df.drop("Age", axis=1)

print(new_df)

Output

    Name    Location
0  Alice    New York
1    Bob     London
2 Charlie     Berlin

Drop Multiple Columns in Pandas

To remove multiple columns, pass a list of column names to drop():

Syntax

DataFrame.drop(["col1", "col2"], axis=1, inplace=False)

Example Code

This removes both the “Age” and “City” columns at once.

# Creating a DataFrame with multiple columns
data = {"Name": ["Alice", "Bob", "Charlie"], 
        "Age": [23, 36, 56], 
        "City": ["New York", "London", "Berlin"]}

df = pd.DataFrame(data)

# Dropping multiple columns
new_df = df.drop(["Age", "City"], axis=1)

print(new_df)

Output

    Name
0  Alice
1    Bob
2 Charlie

If you’re working with CSV file imports, you might end up with extra columns that need cleaning. Before deleting columns, you may first need to merge multiple CSV files into a single DataFrame. Learn how to do it efficiently in Combine Multiple CSV Files in Python (3 Ways with Examples).

Handling Unnamed Columns in Pandas

Sometimes, datasets imported from CSV files contain unnamed columns due to extra separators or empty fields. These columns usually serve no purpose and clutter your DataFrame.

💡 Think of it like extra chairs at an event—if they’re not occupied, they take up space unnecessarily!

remove multiple columns pandas

Example Code

By removing “Unnamed: 0”, the DataFrame becomes cleaner and easier to work with.

# Creating a DataFrame with an unnamed column
data = {"Unnamed: 0": [1, 2, 3], 
        "Name": ["Alice", "Bob", "Charlie"], 
        "Score": [85, 90, 88]}

df = pd.DataFrame(data)

# Dropping the unnamed column
cleaned_df = df.drop(["Unnamed: 0"], axis=1)

print(cleaned_df)

Output

    Name  Score
0  Alice     85
1    Bob     90
2 Charlie     88

Using inplace=True: Modify DataFrame Directly

Instead of creating a new DataFrame, you can modify the existing one using inplace=True.

Syntax

DataFrame.drop(labels=None, axis=1, inplace=True)

Example Code

Here, "Age" is permanently removed from the original DataFrame.

⚠️ Be careful! Once you use inplace=True, the changes cannot be undone.

# Creating a sample DataFrame
data = {"Name": ["Alice", "Bob", "Charlie"], 
        "Age": [25, 30, 35], 
        "City": ["New York", "Paris", "Berlin"]}

df = pd.DataFrame(data)

# Dropping a column permanently
df.drop("Age", axis=1, inplace=True)

print(df)

Output

    Name      City
0  Alice  New York
1    Bob     Paris
2 Charlie   Berlin

Understanding Axes in Pandas

In Pandas, axes define directions:

  • axis=0 → Refers to rows (vertical).
  • axis=1 → Refers to columns (horizontal).

Always specify axis=1 when removing a column, otherwise, Pandas assumes you’re referring to rows.

Example Code

By explicitly setting axis=1, we ensure only the “Grade” column is removed.

# Creating a sample DataFrame
data = {"Name": ["Alice", "Bob", "Charlie"], 
        "Score": [85, 90, 88], 
        "Grade": ["A", "B", "B+"]}

df = pd.DataFrame(data)

# Dropping the "Grade" column using axis=1
new_df = df.drop("Grade", axis=1)

print(new_df)

Output

    Name  Score
0  Alice     85
1    Bob     90
2 Charlie     88

Handling NaN Values Before Dropping Columns

Before removing columns, check for NaN (missing values), as they might affect data quality.

Check for NaN values:

df.isnull().sum()

If a column contains mostly NaN values, consider dropping it using:

df.dropna(axis=1, inplace=True)

Conclusion

Dropping unnecessary columns in Pandas helps keep your DataFrame structured, clean, and easy to analyze. Using drop(), along with parameters like axis=1 and inplace=True, allows for efficient data manipulation.

For more interesting articles, follow our Python tutorial series by SyntaxScenarios.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top