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 the column when the column name contains any number in pandas python
  • Keep or select the column when the column name ends with a specific number
  • Keep or select the column when the column name ends with any number.
  • Keep or select the column when the column name contains a specific number
  • Keep or select the column when the column name contains any number

 

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-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 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 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

 

 

 

Keep or Select the column when the column name starts with a specific number

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

 

# Keep or select the column that starts with specific number


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

column name which starts with a specific number “101” is  selected , so the resultant dataframe will be.

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

 

 

Keep or select the column with column name starts with any number in pandas python

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

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


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

or

simply using a filter with regex as shown below

 
# Keep or select the column that starts with any number: Method 2

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

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

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

 

 

Keep or Select the column when the column name ends with a specific number

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

 
# Keep or select the column that ends with specific number

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

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

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

 

 

Keep or select the column with column name ends with any number in pandas python

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

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

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

or

Simply using a filter with regex as shown below

 

# Keep or select the column that ends with any number: Method 2

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

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

Keep-or-select-Columns-in-pandas-python-when-column-name-has-number-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.

    https://www.linkedin.com/in/sridhar-venkatachalam-74414252/ karaimadai@sg2plcpnl0204.prod.sin2.secureserver.net Venkatachalam Sridhar