In this section we will be focusing on how to drop one or more columns in pandas python when the column name contains a number. We also explained on dropping the column when the column name starts with a number; deleting the column with column name ends with a number. we have used functions like startswith() , endswith(), contains() along with Regular Expressions. let’s look at each of these cases in pandas with an example for each
- Drop column in pandas python when column name has number
- Delete or drop the column that starts with a specific number in pandas python
- Delete or drop the column that starts with any number in pandas python
- Delete or drop the column that ends with a specific number in pandas python
- Delete or drop the column that ends with any number in pandas python
- Delete or drop the column that contains a specific number in pandas python
- Delete or drop the column that 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 that starts with a specific number in pandas python
We will be dropping the column when the column name starts with a specific number using startswith() function in pandas python. We will pick up the column name to be dropped using startswith() function and then will be excluding the column with df.loc[] as shown below
# Delete or drop the column that starts with a specific number df1 = df.loc[:,~df.columns.str.startswith('101')] df1
so the Column name starts with 101 is dropped as shown below.
Delete or drop the column that starts with any number in pandas python
We will be dropping 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 excluded in the next line as shown below
## Delete or drop the column that starts 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 starts with any number is dropped as shown below.
Delete or drop the column that 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.
Delete or drop the column that 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.
Delete or drop the column that 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 contains 401 is dropped as shown below.
Delete or drop the column that 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 contains any 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.