Home »
Python »
Python Programs
Quickly drop dataframe columns with only one distinct value
Learn, how to quickly drop dataframe columns with only one distinct value in Python?
By Pranit Sharma Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
We are given the data frame where we need to count the number of values in each column and the counting should stop if it reaches 2 different values. This means that we need to drop the columns with only one distinct value.
Dropping dataframe columns with only one distinct value
We will use series.unique() method to find all the unique elements in the column and for a column whose .unique() returns only one element, we will drop that column.
Let us understand with the help of an example,
Python program to quickly drop dataframe columns with only one distinct value
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating a DataFrame
df = pd.DataFrame([[1,2,3],[1,3,3],[1,2,3]])
# Display original df
# Display Original df
print("Original DataFrame:\n",df,"\n")
# Dropping columns
for col in df.columns:
if len(df[col].unique()) == 1:
df.drop(col,inplace=True,axis=1)
# Display result
print("Result:\n",df)
Output
The output of the above program is:
Python Pandas Programs »