Drop column in pandas python when column name ends with number

In this section we will be focusing on how to drop one or more columns in pandas python when the column name ends with number. We have explained on delete the column when the column name ends with a specific number or any number. we have used functions like endswith()  along with Regular Expressions. let’s look at each of these cases in pandas with an example for each

  • Delete or drop the column when column name ends with a specific number in pandas python
  • Delete or drop the column when 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

Drop-column-in-pandas-python-when-column-name-ends-with-number-1

 

 

Delete or drop the column when the column name ends with a specific number in pandas python

We will be dropping the column when the column name ends with a specific number using  endswith() function in pandas python.  We will pick up the column name to be dropped using endswith() function and then will be excluding the column with df.loc[] as shown below

 

## Delete or drop the column that ends with a specific number


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

so the Column name ends with 401 is dropped as shown below.

Drop-column-in-pandas-python-when-column-name-ends-with-number-2

 

 

Delete or drop the column when the column name ends with any number in pandas python

We will be dropping 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 excluded in the next line as shown below

 
# Delete or drop the column that ends with any number


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

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

Drop-column-in-pandas-python-when-column-name-ends-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