How To Drop One Or More Columns In Pandas Dataframe

For this exercise, I am using College.csv data.

In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('College.csv')
In [3]:
df.head(1)
Out[3]:
Unnamed: 0 Private Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University Yes 1660 1232 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60

Pandas How To Drop One Column By Name

Lets say we want to drop column 'Private' using df.drop()

In [5]:
df.drop('Private',axis=1).head(1)
Out[5]:
Unnamed: 0 Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University 1660 1232 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60

With above command, we see that 'Private' column has been removed. axis=1 means remove the column. axis=0 means work on row.

With the above command data has not saved. To make the changes, use inplace=True option as shown below.

In [7]:
df.drop('Private',axis=1,inplace=True)

Lets check our dataframe again.

In [8]:
df.head(1)
Out[8]:
Unnamed: 0 Apps Accept Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University 1660 1232 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60

Pandas How To Drop Multiple Columns By Name

Similarly we can run the same command to drop multiple columns. Lets say we want to drop next two columns 'Apps' and 'Accept'

In [9]:
df.drop(['Apps','Accept'],axis=1).head(1)
Out[9]:
Unnamed: 0 Enroll Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University 721 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60

To remove multiple columns, we have provided list of columns to df.drop() as shown above. Again for making the change, we need to pass option inplace=True.

In [21]:
df.drop(['Apps','Accept'],axis=1,inplace=True)

Pandas How To Drop One Column By Index Number

We can also remove the column the index number. Let say we want to remove the column 'Enroll' which is index 1. We can get the index using df.columns[index].

In [25]:
df.drop(df.columns[1],axis=1,inplace=True)
In [26]:
df.head(1)
Out[26]:
Unnamed: 0 Top10perc Top25perc F.Undergrad P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University 23 52 2885 537 7440 3300 450 2200 70 78 18.1 12 7041 60

Pandas How To Drop Multiple Columns By Index Number

Lets say we want to drop column Top10perc which is index 1 and F.Undergrad which is index 3. We can do that passing index in a list.

In [55]:
df.drop(df.columns[[1, 3]], axis=1,inplace=True)
In [56]:
df.head(1)
Out[56]:
Unnamed: 0 Top25perc P.Undergrad Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University 52 537 7440 3300 450 2200 70 78 18.1 12 7041 60

Yes, the columns 'Top10perc' and F.Undergrad have been removed.

Pandas How To Drop Range Of Multiple Columns By Index

Now lets say we want to drop columns 'Top25perc', 'P.Undergrad' and 'Outstate' that are columns from index 1 to 3. We can do that by specifying the index range.

In [57]:
df.drop(df.columns[1:3],axis=1,inplace=True)
In [58]:
df.head(1)
Out[58]:
Unnamed: 0 Outstate Room.Board Books Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University 7440 3300 450 2200 70 78 18.1 12 7041 60

Note, we got only two columns removed, 1:3, 3 is not inclusive. so we have to mention 1:4. Lets do it again. Remove the columns 'Outstate', 'Room.Board' and 'Books' using range 1:4

In [59]:
df.drop(df.columns[1:4],axis=1,inplace=True)
In [60]:
df.head(1)
Out[60]:
Unnamed: 0 Personal PhD Terminal S.F.Ratio perc.alumni Expend Grad.Rate
0 Abilene Christian University 2200 70 78 18.1 12 7041 60

Wrap Up!

In this post, I have covered the basics of how to drop columns in Pandas dataframe. In the next post, I will cover how to drop rows of Pandas DataFrame.

Related Posts: