How to Drop Multiple Columns in Pandas using [name , index, and range] ?

Drop Multiple Columns in Pandas using [name , index, and range]

Pandas data frame is one of my favorite data structures. Actually, It makes data handling smooth and easy. There are so many helpful resources available online. Still, there are some gaps in information for a few topics. This article is also to fill these gaps. This article will explain to you – How to drop multiple columns in Pandas. Let’s see how easy are pandas dropping columns using the column index, name, and range.

[toc]

 1 – Dummy Dataframe Creation Example.

Let’s create a dummy pandas data frame for illustration.

import pandas as pd 
employee = {
'Name':['Sukesh', ' Monoj', 'Bob', 'Alica', 'Vanket'],
'Age' :['27', '33', '26', '32', '54'],
'Salary':['100000', '500001', '300000', '170000', '100000000'],
'Country':['India', 'India', 'UK', 'America', 'France'],
'Experience':['4', '5', '12', '9', '23'] }
df = pd.DataFrame(employee)

Output

dummy-data-frame-creation-padas
Dummy Data Frame Creation Pandas

2 -Dropping column in Dataframe

There are two different and popular ways.

2.1  Using drop() function-

This drop function needs a few arguments. It further creates subways to delete columns in the dataframe. Let’s see them in brief –

2.1.1 Pandas drop columns by name   –

Suppose you want to drop the “Salary” column from the above dataframe. Let’s see how to achieve –

df.drop(["Salary"],axis =1 )
drop() fun in pandas dataframe
drop() Fun in Pandas Dataframe

 

If you want to drop multiple columns in pandas dataframe. You may give names in the list as well –

df.drop(["Salary","Age"],axis =1 )

 

multiple column drop using drop()
Multiple column drop using drop()

 

Note –

1.When you want to make these changes happen in the same data frame, use inplace = True . Let’s see the full syntax –

df.drop(["Salary"], axis = 1, inplace = True)

2. If you need to extract a new data frame after dropping a certain column without changing the original dataframe, Refer below-

modified_df= df.drop(["Salary"], axis = 1)

3. Here axis=1 denotes column only. By default, axis is 0.

 

2.1.2  Pandas drop column by position –

If you want to delete the column with the column index in the dataframe. Please use the below code –

df.drop(df.columns[[1,2]], axis=1)

 

pandas dropping columns using the column index
Pandas dropping columns using the column index

 

In the above example, You may give single and multiple indexes of dataframe for dropping.

 

2.1.3 Using drop()  with column range-

You may define this column range either by index(position) or by name. Let’s see how –

2.1.3.1  Pandas drop columns by index range-

If the range is defined by index(position), Please refer the below code syntax –

df.drop(df.iloc[:, 0:2], axis = 1)
pandas dropping columns using column range by index
Pandas dropping columns using column range by index

 

In the above example, the column at index 0 and 1 are dropped. Because we have given the range [0:2].

2.1.3.2  Pandas drop columns by name range-

Suppose you want to drop the columns between any column name to any column name. Like in the above dataframe, you want to drop the column fro Age to Country. You may achieve like below –

df.drop(df.loc[:, 'Age':'Country'].columns, axis = 1) 
dropping column by name range
Dropping column by name range

 

2.1.4 Using drop()  with regex in column name-

Suppose you want to drop some column that has a certain pattern. Let’s understand with some examples. Suppose In the above dataframe, you need to drop the column which starts which the “A” letter.

df.drop(df.columns[df.columns.str.contains('^A')], axis=1)

 

deleting pandas column using regex in the column name
Deleting pandas column using regex in the column name

 

Here we have removed the column which starts with the letter “A”. You may alter this regex pattern as per your choice.

2.2 Dropping column by iteration –

This is another way of dropping certain columns from the dataframe. Let’s go through this method with a coding example-

for col in df.columns: 
    if 'A' in col: 
        del df[col]

 

column deletion using iteration
Column deletion using iteration

 

2.3 Dropping pandas column on custom condition –

There may be so many conditions where you need to drop the column in some custom conditions. For example, If you need to drop the column where 40 % values are null. Let’s see –

columns = df.columns[df.isnull().mean()>0.4]
df.drop(columns, axis=1)

To demonstrate this code, I need to create a fresh dummy dataframe and insert values accordingly.

pandas column removal on custom conditions
Pandas column removal on custom conditions

As the above example shows we have removed the column here more than 40 percent values are null. In the above example, It was the “Age” column.

2.4 Dropping Unnamed Column-

In some scenarios , We have unnamed columns in the dataframe. Here is the detail article to drop unnamed column in Pandas with coding examples. Please go through it.

 

 

Thanks 

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner