How to Exclude Columns in Pandas?
Last Updated :
11 Apr, 2025
Excluding columns in a Pandas DataFrame is a common operation when you want to work with only relevant data. In this article, we will discuss various methods to exclude columns from a DataFrame, including using .loc[]
, .drop()
, and other techniques.
Exclude One Column using .loc[]
We can exclude a column by its location using the .loc [] function. The code below demonstrates how to exclude a specific column by comparing column names.
Python
import pandas as pd
d = pd.DataFrame({'food_id': [1, 2, 3, 4],
'name': ['idly', 'dosa', 'poori', 'chapathi'],
'city': ['delhi', 'goa', 'hyd', 'chennai'],
'cost': [12, 34, 21, 23]})
# Exclude the 'cost' column
ex = d.loc[:, d.columns != 'cost']
print("DataFrame after excluding 'cost' column using .loc[]")
print(ex)
Output:
DataFrame after excluding 'cost' column using .loc[]
food_id name city
0 1 idly delhi
1 2 dosa goa
2 3 poori hyd
3 4 chapathi chennai
If we need to exclude multiple columns, we can use .isin()
function.
Python
# exclude name and food_id column
print(d.loc[:, ~d.columns.isin(['name', 'food_id'])])
Output
city cost
0 delhi 12
1 goa 34
2 hyd 21
3 chennai 23
Other methods to exclude columns in Pandas, apart from using DataFrame.loc[], are discussed below:
Excluding Columns Using .remove()
Although .remove() is not a direct Pandas method, we can use it with a list of column names before reassigning the DataFrame.
Python
# Convert columns to a list and remove the column 'cost'
columns = list(d.columns)
columns.remove('cost')
# Create a new DataFrame with the remaining columns
ex = d[columns]
print(ex)
Output
food_id name city
0 1 idly delhi
1 2 dosa goa
2 3 poori hyd
3 4 chapathi chennai
Excluding Columns by Name Using drop()
drop() method is one of the most common ways to exclude specific columns by name:
Python
# Exclude the 'cost' column
ex = d.drop(columns=['cost'])
print(ex)
Output
food_id name city
0 1 idly delhi
1 2 dosa goa
2 3 poori hyd
3 4 chapathi chennai
Exclude Columns by Data Type Using select_dtypes()
We can exclude columns based on their data type using the select_dtypes() method.
Python
# Exclude all numeric columns
ex = d.select_dtypes(exclude=['number'])
print(ex)
Output
name city
0 idly delhi
1 dosa goa
2 poori hyd
3 chapathi chennai
Excluding Columns Dynamically Using List Comprehensions
If we need to exclude columns dynamically based on a condition, we can use list comprehensions. For example, to exclude columns whose names start with a specific letter:
Python
# Exclude columns whose names start with 'c'
ex = d[[col for col in d.columns if not col.startswith('c')]]
print(ex)
Output
food_id name
0 1 idly
1 2 dosa
2 3 poori
3 4 chapathi
These techniques make column exclusion in Pandas flexible and straightforward, helping us focus on the data we need.
Similar Reads
How to Drop Index Column in Pandas? When working with Pandas DataFrames, it's common to reset or remove custom indexing, especially after filtering or modifying rows. Dropping the index is useful when:We no longer need a custom index.We want to restore default integer indexing (0, 1, 2, ...).We're preparing data for exports or transfo
2 min read
How to Drop Unnamed Column in Pandas DataFrame Pandas is an open-source data analysis and manipulation tool widely used for handling structured data. In some cases, when importing data from CSV files, unnamed columns (often labeled as Unnamed: X) may appear. These columns usually contain unnecessary data, such as row indices from previous export
5 min read
Drop Empty Columns in Pandas Cleaning data is an essential step in data analysis. In this guide we will explore different ways to drop empty, null and zero-value columns in a Pandas DataFrame using Python. By the end you'll know how to efficiently clean your dataset using the dropna() and replace() methods. Understanding dropna
3 min read
How to add Empty Column to Dataframe in Pandas? In Pandas we add empty columns to a DataFrame to create placeholders for future data or handle missing values. We can assign empty columns using different methods depending on the type of placeholder value we want. In this article, we will see different methods to add empty columns and how each one
2 min read
How to Show All Columns of a Pandas DataFrame? Pandas limit the display of rows and columns, making it difficult to view the full data, so let's learn how to show all the columns of Pandas DataFrame. Using pd.set_option to Show All Pandas ColumnsPandas provides a set_option() function that allows you to configure various display options, includi
2 min read