Table of contents
1.
Introduction
2.
Basic Syntax for Dropping Columns
3.
Examples of Dropping Columns
3.1.
Dropping a Single Column
3.2.
Python
3.3.
Python
3.4.
Dropping Multiple Columns
3.5.
Python
3.6.
Dropping Columns In-Place
3.7.
Python
3.8.
Key Features of Dropping Columns
4.
Handling Missing Data
4.1.
Python
5.
Advanced Techniques
5.1.
Dropping Columns Based on Condition
5.2.
Python
5.3.
Using filter() Method
5.4.
Python
6.
Frequently Asked Questions
6.1.
How do I drop columns from a DataFrame in Pandas? 
6.2.
Can I drop columns with missing values? 
6.3.
How do I make the change directly in the existing DataFrame? 
7.
Conclusion
Last Updated: Aug 14, 2024
Easy

Dropping Columns in Pandas

Author Pallavi singh
0 upvote

Introduction

Pandas being a powerful Python library is mainly used for data manipulation and analysis. A common task when working with data is dropping columns from a DataFrame. Dropping columns can help you clean your data and focus on the most relevant information. 

Dropping Columns in Pandas

This article will walk you through how to drop columns in Pandas, providing clear examples and explanations.

Basic Syntax for Dropping Columns

In Pandas, you can drop columns from a DataFrame using the drop() method. Here’s the basic syntax:

DataFrame.drop(labels, axis=1, inplace=False)

 

  • labels: Specifies the name(s) of the column(s) to be dropped.
     
  • axis: Set to 1 for columns (default is 0 for rows).
     
  • inplace: If True, modifies the DataFrame directly. If False, returns a new DataFrame without modifying the original.

Examples of Dropping Columns

Let’s go through some examples to understand how to use the drop() method effectively in python.

Dropping a Single Column

Suppose you have the following DataFrame:

  • Python

Python

import pandas as pd

df = pd.DataFrame({

   'Name': ['Alice', 'Bob', 'Charlie'],

   'Age': [25, 30, 35],

   'City': ['New York', 'Los Angeles', 'Chicago']

})

print("Original DataFrame:")

print(df)
You can also try this code with Online Python Compiler
Run Code


Output

      Name  Age         City
0     Alice   25     New York
1       Bob   30  Los Angeles
2   Charlie   35      Chicago


To drop the Age column

  • Python

Python

df_dropped = df.drop('Age', axis=1)

print("\nDataFrame after dropping 'Age' column:")

print(df_dropped)
You can also try this code with Online Python Compiler
Run Code

 

Output

      Name         City
0     Alice     New York
1       Bob  Los Angeles
2   Charlie      Chicago

Dropping Multiple Columns

If you want to drop multiple columns, pass a list of column names:

  • Python

Python

df_dropped = df.drop(['Age', 'City'], axis=1)

print("\nDataFrame after dropping 'Age' and 'City' columns:")

print(df_dropped)
You can also try this code with Online Python Compiler
Run Code


Output

      Name
0     Alice
1       Bob
2   Charlie

Dropping Columns In-Place

If you want to modify the original DataFrame, set inplace=True:

  • Python

Python

df.drop('City', axis=1, inplace=True)

print("\nOriginal DataFrame after dropping 'City' column (in-place):")

print(df)
You can also try this code with Online Python Compiler
Run Code


Output

      Name  Age
0     Alice   25
1       Bob   30
2   Charlie   35

Key Features of Dropping Columns

  1. Flexibility: You can drop one or multiple columns at once.
     
  2. In-Place Modification: Allows you to modify the DataFrame directly or return a new one.
     
  3. Handling Missing Data: Easily remove columns with missing values.

Handling Missing Data

Sometimes, you may need to drop columns that contain missing values. You can use the dropna() method for this purpose:

  • Python

Python

df = pd.DataFrame({

   'A': [1, 2, None],

   'B': [4, None, 6],

   'C': [7, 8, 9]

})

print("Original DataFrame with missing values:")

print(df)

df_cleaned = df.dropna(axis=1)

print("\nDataFrame after dropping columns with missing values:")

print(df_cleaned)
You can also try this code with Online Python Compiler
Run Code


Output

Original DataFrame with missing values:
     A    B  C
0  1.0  4.0  7
1  2.0  NaN  8
2  NaN  6.0  9


DataFrame after dropping columns with missing values:

   C
0  7
1  8
2  9

Advanced Techniques

Dropping Columns Based on Condition

You can drop columns based on their names using list comprehensions:

  • Python

Python

df = pd.DataFrame({

   'A': [1, 2, 3],

   'B': [4, 5, 6],

   'Unwanted_Column': [7, 8, 9]

})

# Drop columns that contain 'Unwanted' in their name

cols_to_drop = [col for col in df.columns if 'Unwanted' in col]

df_dropped = df.drop(cols_to_drop, axis=1)

print("\nDataFrame after conditionally dropping columns:")

print(df_dropped)
You can also try this code with Online Python Compiler
Run Code

 

Output

  A  B
0  1  4
1  2  5
2  3  6

Using filter() Method

You can also use the filter() method to select columns and exclude the rest:

  • Python

Python

df_filtered = df.filter(regex='^(?!Unwanted)')

print("\nDataFrame after filtering columns:")

print(df_filtered)
You can also try this code with Online Python Compiler
Run Code

Output

  A  B
0  1  4
1  2  5
2  3  6

Frequently Asked Questions

How do I drop columns from a DataFrame in Pandas? 

To remove columns from a DataFrame, use the drop() method. Set the axis parameter to 1 to specify that you want to drop columns. For instance, df.drop('column_name', axis=1) removes the column named 'column_name'. To drop multiple columns, pass a list of names, like df.drop(['col1', 'col2'], axis=1).

Can I drop columns with missing values? 

Yes, you can drop columns with missing values using the dropna() method. Set the axis parameter to 1 to drop columns. For example, df.dropna(axis=1) will remove any columns containing at least one NaN value. This is useful for cleaning data and removing incomplete columns.

How do I make the change directly in the existing DataFrame? 

To modify the original DataFrame directly without creating a new one, use the inplace parameter with the drop() method. For example, df.drop('column_name', axis=1, inplace=True) will remove the column and update the DataFrame in place, reflecting the changes immediately without returning a new DataFrame.

Conclusion

Dropping columns in Pandas is a pretty easy yet essential operation for data manipulation. By mastering this technique, you can clean your data, focus on important features, and enhance your data analysis workflows. Whether you're cleaning up data or preparing it for analysis, Pandas provides the tools you need to work on efficiently.

You can also check out our other blogs on Code360.

Live masterclass