Keep when column name contains a specific string in pandas python

In This Section we will be focusing on how to keep or select the column when the column name contains a specific string in pandas python, There are multiple ways to do it, we have also handled the case sensitiveness,  we have used functions like regex() , filter() along with select() to achieve the same. let’s look at each of these cases in pandas with an example for each.

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

 

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

Keep or select when column name contains a specific string in pandas python 1

 

 

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

We will be selecting 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 selected the column that contains a specific string or character, in the below example we have selected the column with the column name containing “name”

 

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

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

column name which contains “name” is selected.i.e. “location_name”, “name_of_country”  and “nick_name_of_customer” column is selected , so the resultant dataframe will be.

Keep or select when column name contains a specific string in pandas python 2

 

 

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

We will be selecting 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 selected the column that contains a specific string or character, in the below example we have selected the column with the column name containing “name” or “Name” or “NAME”

 
##### select the column that contains certain string or name : case In sensitive : All Cases.
df1 = df[df.filter(regex='name|Name|NAME').columns]
df1

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

Keep or select when column name contains a specific string in pandas python 3