In this section we will be focusing on how to drop one or more columns in pandas python when the column name starts with number. We have explained on delete the column when the column name starts with a specific number or any number. we have used functions like startswith() 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 starts with a specific number in pandas python
- Delete or drop the column when 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
Delete or drop the column when column name 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 when column name 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.