Python Drop Rows and Columns in Pandas

In this tutorial, we will learn the process of dropping rows and columns of a data frame in Pandas in Python. The dropping of rows and columns is an important process when dealing with data frames. Dropping is nothing but removing a particular row or column. By the end of this tutorial, you will learn various approaches to drop rows and columns. This tutorial will be very helpful in manipulating the data in data frames. So, let’s begin the tutorial.

Install Pandas

This is the prerequisite to proceed to use Pandas. If you have not installed it, you can install it by using the below command in the command prompt.

pip install pandas

You can create data frames using this tutorial create a data frame in Pandas

Dropping rows and columns in pandas

After creating a data frame, we can proceed to drop the rows and columns. For this, a method data frame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=’raise’) is used. We can make use of the required arguments for our purpose. Inplace is used to describe the operation. The level is used when various indices are used. Here is the sample data frame for demonstration purpose

import pandas as pd
da = {'Person': ['Rama', 'Sham', 'Raju', 'Ramu', 'Ravi'], 
        'Age': [20, 35, 16, 18, 26], 
        'Place': ['HYDERABAD', 'CHENNAI', 'KERELA', 'MUMBAI', 'PUNE']}
d1 = pd.DataFrame(da, index = ['no.1', 'no.2', 'no.3', 'no.4', 'no.5'])
print(d1)

OUTPUT:

     Person Age Place
no.1 Rama   20  HYDERABAD
no.2 Sham   35  CHENNAI
no.3 Raju   16  KERELA
no.4 Ramu   18  MUMBAI
no.5 Ravi   26  PUNE

Some methods to drop rows in Pandas – Python

We can mention a single label or list of labels to drop them. By default the axis value=0 ,i.e. a row. When deleting a row we don’t need to explicitly provide the axis parameter.

Dropping a single row using label:

print(d1.drop('no.1'))

or

print(d1.drop(['no.1']))

OUTPUT:

     Person Age Place
no.2 Sham   35  CHENNAI
no.3 Raju   16  KERELA
no.4 Ramu   18  MUMBAI
no.5 Ravi   26  PUNE

Dropping multiple rows using label:

print(d1.drop(['no.1','no.2']))

OUTPUT:

     Person Age Place
no.3 Raju   16  KERELA
no.4 Ramu   18  MUMBAI
no.5 Ravi   26  PUNE

Dropping a single row using the index:

print(d1.drop(index='no.1'))

or

print(d1.drop(d1.index[0]))

OUTPUT:

     Person Age Place
no.2 Sham   35  CHENNAI
no.3 Raju   16  KERELA
no.4 Ramu   18  MUMBAI
no.5 Ravi   26  PUNE

Dropping multiple rows using label:

print(d1.drop(index=['no.1','no.2']))

or

print(d1.drop(d1.index[[0,1]]))

or

print(d1.drop(d1.index[:2]))

OUTPUT:

     Person Age Place
no.3 Raju   16  KERELA
no.4 Ramu   18  MUMBAI
no.5 Ravi   26  PUNE

Some methods to drop columns in Pandas – Python

We can mention a single label or list of labels to drop them. By default the axis value=0 ,i.e. a row. So, when deleting a column we need to explicitly provide the axis parameter.  The value of the axis for column is 1. The dropping process is the same as a row. Instead of the index, we have a column here.

Dropping a single column using label:

print(d1.drop(('Person'),axis=1))

or

print(d1.drop(['Person'],axis=1))

or

print(d1.drop('Person',axis=1))

OUTPUT:

     Age Place
no.1 20  HYDERABAD
no.2 35  CHENNAI
no.3 16  KERELA
no.4 18  MUMBAI
no.5 26  PUNE

Dropping a single column using the column parameter:

print(d1.drop(columns='Person'))

OUTPUT:

     Age Place
no.1 20  HYDERABAD
no.2 35  CHENNAI
no.3 16  KERELA
no.4 18  MUMBAI
no.5 26  PUNE

Dropping multiple columns using label:

print(d1.drop(columns=['Person','Age']))

OUTPUT:

     Place
no.1 HYDERABAD
no.2 CHENNAI
no.3 KERELA
no.4 MUMBAI
no.5 PUNE

Dropping multiple columns using the column parameter:

print(d1.drop(d1.columns[[0,1]],axis=1))

or

print(d1.drop(columns=d1.columns[[0, 1]]))

or

print(d1.drop(d1.columns[:2],axis=1))

OUTPUT:

     Place
no.1 HYDERABAD
no.2 CHENNAI
no.3 KERELA
no.4 MUMBAI
no.5 PUNE

Putting all pieces of codes together, the final code is:

import pandas as pd
da = {'Person': ['Rama', 'Sham', 'Raju', 'Ramu', 'Ravi'], 
        'Age': [20, 35, 16, 18, 26], 
        'Place': ['HYDERABAD', 'CHENNAI', 'KERELA', 'MUMBAI', 'PUNE']}
d1 = pd.DataFrame(da, index = ['no.1', 'no.2', 'no.3', 'no.4', 'no.5'])
print(d1)
print(d1.drop('no.1'))
print(d1.drop(['no.1']))
print(d1.drop(index='no.1'))
print(d1.drop(d1.index[0]))
print(d1.drop(['no.1','no.2']))
print(d1.drop(index=['no.1','no.2']))
print(d1.drop(d1.index[[0,1]]))
print(d1.drop(d1.index[:2]))
print(d1.drop(('Person'),axis=1))
print(d1.drop(['Person'],axis=1))
print(d1.drop('Person',axis=1))
print(d1.drop(columns='Person'))
print(d1.drop(columns=['Person','Age']))
print(d1.drop(d1.columns[[0,1]],axis=1))
print(d1.drop(columns=d1.columns[[0, 1]]))
print(d1.drop(d1.columns[:2],axis=1))

So, in this tutorial, we covered various approaches to drop rows and columns.

Leave a Reply