Pandas DataFrame: Remove Constant Columns
Last Updated :
23 Jul, 2025
In data analysis, it is common to encounter columns in a dataset that have a constant value throughout (i.e., all rows in that column contain the same value). Such constant columns provide no meaningful information and can be safely removed without affecting the analysis.
Remove Constant Columns in Pandas
In this article, we will explore how to identify and remove constant columns in a Pandas DataFrame using Python.
Why Remove Constant Columns?
Constant columns offer no variability, meaning they do not contribute to distinguishing between different data points. In many machine learning models, these columns can negatively affect the performance by introducing redundant or irrelevant data. Therefore, it is often essential to remove constant columns to:
- Reduce the dimensionality of the dataset.
- Improve computational efficiency.
- Enhance model interpretability.
Step 1- Identifying Constant Columns in Pandas
Pandas offers several ways to identify and remove constant columns. We can check for columns where the number of unique values is exactly 1.
The .nunique() function is particularly useful for this purpose, as it returns the number of distinct elements in each column.
Python
import pandas as pd
# Sample DataFrame with constant and non-constant columns
data = {
'A': [1, 1, 1, 1],
'B': [2, 3, 4, 5],
'C': ['X', 'X', 'X', 'X'],
'D': [10, 11, 12, 13]
}
df = pd.DataFrame(data)
# Identify constant columns
constant_columns = [col for col in df.columns if df[col].nunique() == 1]
# Display constant columns
print("Constant columns:", constant_columns)
Output:
Constant columns: ['A', 'C']
In this case, Column A and Column C are identified as constant because they have only one unique value.
Step 2: Removing Constant Columns
Once we have identified the constant columns, we can easily drop them using the .drop() function in Pandas.
Python
# Drop constant columns
df_cleaned = df.drop(columns=constant_columns)
# Display the cleaned DataFrame
print(df_cleaned)
Output:
B D
0 2 10
1 3 11
2 4 12
3 5 13
Here, the cleaned DataFrame has removed the constant columns A and C.
Step 3: Removing Constant Columns in a Larger Dataset
Let’s consider a larger dataset where some columns may have constant values.
Python
import numpy as np
# Create a DataFrame with random and constant columns
data = {
'X1': np.random.randint(0, 100, size=100),
'X2': [5] * 100, # Constant column
'X3': np.random.randint(0, 100, size=100),
'X4': [3] * 100, # Constant column
}
df_large = pd.DataFrame(data)
# Remove constant columns in the larger dataset
constant_columns = [col for col in df_large.columns if df_large[col].nunique() == 1]
df_large_cleaned = df_large.drop(columns=constant_columns)
print("Original DataFrame Shape:", df_large.shape)
print(df_large.head())
print()
print("Cleaned DataFrame Shape:", df_large_cleaned.shape)
print(df_large_cleaned.head())
Output:
Remove constant columns from pandas dataframeIn this case, the constant columns X2 and X4 were removed, leaving only X1 and X3 in the cleaned DataFrame.
Handling Special Cases
- Empty DataFrames: If the DataFrame is empty, removing constant columns has no effect, and the function should return the original DataFrame.
- Columns with Missing Values: Columns with missing values (NA) can still be treated as constant if all the non-missing values are the same. You can fill missing values with a placeholder (e.g.,
fillna()) before identifying constant columns.
Conclusion
Removing constant columns from a dataset is a crucial step in data preprocessing, especially when dealing with large datasets in machine learning and data analysis. In this article, we have:
- Defined constant columns and explained their lack of significance in analysis.
- Shown multiple ways to identify and remove constant columns using Pandas.
- Provided examples, including removing constant columns in larger datasets and handling special cases like missing data.
By effectively removing these redundant columns, we can improve the performance of our models and streamline our analysis.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice