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

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

First lets select the column when the column name ends with a specific number, we have used the endwith() 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-ends-with-number-2

 

 

Keep or  Select column with column name ends with any number in pandas python: Method 1

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

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

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

 

 

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

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

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

 

 

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