Keep or select Column in pandas python when column name starts with number

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

 

 

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

First lets Keep 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-starts-with-number-2

 

 

 

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

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

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

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-starts-with-number-3

 

 

Keep the column when column name starts with any number in pandas python: Method 2

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 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 starts with any number is selected as shown below.

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

 

 

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