Keep or select Column in pandas python when column name contains number

In This Section we will be focusing on how to select the column when the column name contains a number; keep or select the column when the column name contains a specific number, and also select the column when the column name contains any number in python pandas dataframe, There are multiple ways to do it, we have used functions like contains() 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 a specific number in pandas python
  • Keep or select the column when the column name contains any number in pandas python

 

Create Dataframe:

 

import pandas as pd

data = {'201product_Name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],
'101price': [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_country301':['India','India','US','US','UK'],
'name_of_customer401':['Krish','Ram','John','Joe','Chris']
}

df = pd.DataFrame(data)
df

The Resultant dataframe is

Keep or select Columns in pandas python when column name has number 1

 

 

Keep or Select the column when the column name contains a specific number

First lets select the column when the column name contains a specific number, we have used the contains() function in pandas python which will take the numeric input and we have selected the column that contains a specific number, in the below example we have selected the column with the column name containing specific number say  “401”

 
# Keep or select the column that contains a specific number

df1 = df.loc[:,df.columns.str.contains('401')]
df1

column name which contains a specific number “401” is selected, so the resultant dataframe will be.

Keep or select Columns in pandas python when column name contains number 2

 

 

 

Keep or select the column when column name contains any number in pandas python: Method 1

Select the columns which contains any number using simple regular expression with filter() function as shown below.

 

# Keep or select the column that contains any number: Method 1

df.filter(regex=('[0-9]'))

So the column name containing any number is selected as shown below.

Keep or select Columns in pandas python when column name contains number 3

 

 

Keep or select the column when column name containing any number in pandas python: Method 2

We will be keeping or selecting the column when the column name contains any number using regular expression inside the filter() function in pandas python.  Which will pick up all the columns which contains number and stored as a list and it will be selected in the next line as shown below

 

# keep or select the column that contains any number: Method 2

cols_to_keep =df.filter(regex=('[0-9]')).columns
df= df[[ col for col in df.columns if col in cols_to_keep ]]
df

So the column name containing any number is selected as shown below

Keep or select Columns in pandas python when column name contains number 4