Drop column when column name contains string in pandas python

In This Section we will be focusing on how to drop or delete the column based on the column name in pandas python i.e. dropping the column when the column name starts with a string; Delete or Drop the column when the column name ends with a string or name, and also drop the column when the column name contains a specific string in pandas dataframe, There are multiple ways to do it, we have also handled the case sensitiveness,  we have used functions like startswith() , endswith() along with drop() and select() to achieve the same. let’s look at each of these cases in pandas with an example for each.

  • Drop the column when the column name contains certain string or name in pandas – case sensitive
  • Drop the column when the column name contains certain string or name in pandas – case In sensitive : All Cases.
  • Delete the column when the column name starts with a name or string in pandas – Case Sensitive
  • Delete the column when the column name starts with a name or string in pandas – Case In Sensitive: All Case
  • Delete the column when the column name ends with a name or string in pandas – Case Sensitive
  • Delete the column when the column name ends with a name or string in pandas – Case In Sensitive: All Case

 

Create Dataframe:

 

## create dataframe 

import pandas as pd 

data = {'product_Name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],
        'price': [1200, 150, 300, 450, 200],
        'location_name':['Mumbai','Delhi','California','Newyork','London'],
        'offer_percentage':[3,4,8,1,3],
        'Name_of_customer':['Krishna','Ram','Johnathan','Joel','Christy'],
        'name_of_country':['India','India','US','US','UK'],
        'nick_name_of_customer':['Krish','Ram','John','Joe','Chris']
        }

df = pd.DataFrame(data)

df

The Resultant dataframe is

Drop-or-Delete-the-column-based-on-column-name-with-string-pandas-python-1

 

 

 

Drop the column when the column name contains certain string or name in pandas – case sensitive

We will be deleting the column when the column name contains a specific string or character, using filter() function in pandas python which will filter based on the column name and have excluded the column that contains a specific string or character, in the below example we have dropped the column with the column name containing “name”

 

##### Delete the column that contains certain string or name : case sensitive

df1 = df[df.columns.drop((df.filter(regex='name').columns))]
df1

column name which contains “name” is  dropped .i.e. “location_name”, “name_of_country”  and “nick_name_of_customer” column is dropped , so the resultant dataframe will be.Drop-or-Delete-the-column-based-on-column-name-with-string-pandas-python-6

 

 

Delete the column when the column name contains certain string or name in pandas – case In sensitive: All Cases.

We will be deleting the column when the column name contains a specific string or character, using filter() function in pandas python which will filter based on the column name and have excluded the column that contains a specific string or character, in the below example we have dropped the column with the column name containing “name” or “Name” or “NAME”

 
##### Delete the column that contains certain string or name : case In sensitive : All Cases.

df1 = df.drop(df.filter(regex='name|Name|NAME').columns, axis=1)
df1

column name which contains with all the case “name”, “Name” or “NAME” is  dropped .i.e All the columns are dropped except  the “price” and “offer_percentage” , so the resultant dataframe will be.

Drop-or-Delete-the-column-based-on-column-name-with-string-pandas-python-7

 

 

 

Delete the column when the column name starts with a name or string in pandas – Case Sensitive

First lets drop or delete the column based on column name when the column name starts with a specific string or character, we have used the startswith() function in pandas python which will take the string or character as the input and we have excluded the column that starts with the string or character, in the below example we have dropped the column with the column name starting with “name”

 

# Delete the column name that starts with a name or string: case sensitive

df1 = df.loc[:,~df.columns.str.startswith('name')]
df1

column name which starts with only small case “name” is  dropped .i.e the “name_of_country” column is dropped , so the resultant dataframe will be.

Drop-or-Delete-the-column-based-on-column-name-with-string-pandas-python-2

 

 

Delete the column when the column name starts with a name or string in pandas – Case In Sensitive: All Case

We will be deleting the column when the column name starts with a specific string or character, using lower() function in pandas python which will filter based on the column name and have excluded the column that starts with the string or character, in the below example we have dropped the column with the column name starting with “name” or “Name” or “NAME”

 

# Delete the column that starts with a name or string : Case In Sensitive: All Case

cols = [d for d in df.columns if d.lower()[:4] != 'name']
df1=df[cols]
df1

column name which starts with all the case “name”, “Name” or “NAME” is  dropped .i.e the “name_of_country” and “Name_of_customer” column is dropped , so the resultant dataframe will be.

Drop-or-Delete-the-column-based-on-column-name-with-string-pandas-python-3

 

 

 

Delete the column when the column name ends with a name or string in pandas – Case Sensitive

To drop or delete the column based on column name when the column name ends with a specific string or character, we have used the endswith() function in pandas python which will take the string or character as the input and we have excluded the column that ends with the string or character, in the below example we have dropped the column with the column name ending with “name”

 

# Delete the column that Ends with a name or string : Case Sensitive

df1 = df.loc[:,~df.columns.str.endswith('name')]
df1

column name which ends with only small case “name” is dropped .i.e the “location_name” column is dropped , so the resultant dataframe will be.

Drop-or-Delete-the-column-based-on-column-name-with-string-pandas-python-4

 

 

Delete the column when the column name ends with a name or string in pandas – Case In Sensitive: All Case

We will be deleting the column when the column name ends with a specific string or character, using lower() function in pandas python which will filter based on the column name and have excluded the column that starts with the string or character, in the below example we have dropped the column with the column name ending with “name” or “Name” or “NAME”

 

# Delete the column that Ends with a name or string : Case In Sensitive: All Case


cols = [d for d in df.columns if d.lower()[-4:] != 'name']
df1=df[cols]
df1

column name which ends with all the case “name”, “Name” or “NAME” is  dropped .i.e the “product_Name” and “location_name” column is dropped , so the resultant dataframe will be.

Drop-or-Delete-the-column-based-on-column-name-with-string-pandas-python-5

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts