Home »
Python »
Python Programs
Drop columns whose name contains a specific string from pandas DataFrame
Given a Pandas DataFrame, learn how to drop columns whose name contains a specific string?
Submitted by Pranit Sharma, on June 11, 2022
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 the data. The Data inside the DataFrame can be of any type.
Problem statement
Given a Pandas DataFrame, we have to drop columns whose name contains a specific string.
Drop DataFrame's columns that contains some specific string
To drop DataFrame's columns that contains some specific string, you can use the dataframe.filter() method by specifying the regex parameter with the regular expression. The regex (Regular Expression) is special format string is used for searching and filtering in pandas DataFrame rows. For example,
- 'K.*' : It will filter all the records which start with the letter 'K'.
- 'A.*' : It will filter all the records which start with the letter 'A'.
We will compare if the DataFrame consists of our specific regex or not, if yes, we will drop that particular column.
Let us understand with the help of an example,
Python program to drop columns whose name contains a specific string
# Importing pandas package
import pandas as pd
# Defining two DataFrames
df = pd.DataFrame(data = {
'Parle':['Frooti','Krack-jack','Hide&seek','Frooti'],
'Nestle':['Maggie','Kitkat','EveryDay','Crunch'],
'Dabur':['Chawanprash','Honey','Hair oil','Hajmola']
})
# Display DataFrame
print("Original DataFrame:\n",df,"\n")
# Dropping a column which consists a string 'Dabur'
result = df[df.columns.drop(list(df.filter(regex='Dabur')))]
# Display result
print("DataFrame after dropping the column which contains 'Dabur':\n",result)
Output
Python Pandas Programs »