In this section we will be focusing on how to drop one or more columns in pandas python when the column name contain number. We have explained on delete the column when the column name contains a specific number or any number. we have used functions like contains() 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 contains a specific number in pandas python
- Delete or drop the column when 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
Delete or drop the column when column name contains a specific number in pandas python
We will be dropping the column when the column name contains a specific number using contains() function in pandas python. We will pick up the column name to be dropped using contains() function and then will be excluding the column with df.loc[] as shown below
# Delete or drop the column that contains a specific number df1 = df.loc[:,~df.columns.str.contains('401')] df1
so the Column name starts with 401 is dropped as shown below.
Delete or drop the column when column name contains any number in pandas python
We will be dropping 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 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 contains 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 containing any number is dropped as shown below.