Articles

drop specific row in pandas | drop multiple rows in pandas

drop specific row in pandas | drop multiple rows in pandas


In this pandas tutorial, we will discuss about:

  • drop row in pandas dataframe,
  • drop multiple rows in pandas

DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

 

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key.

 

Example: Create Dataframe

In this example, we will create dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: We get below dataframe as result

      college_id       college_name college_address  Total Staff
one        c-001  vignan university          guntur         1200
two        c-021               vvit          guntur         3422
three      c-002           RVR - JC          guntur         5644
four       c-004  Andhra University          guntur          670

We will use above dataframe to drop row in pandas dataframe.


drop row in pandas dataframe

Method 1drop row in pandas using drop() with index label

Here, we will use drop() function to remove/drop the rows from the given dataframe. We have to specify the index name/label to drop particular index.

 

So based on the label, it will drop particular row.

Syntax:

dataframe.drop('index_label')

where,

  1. dataframe is the input pandas DataFrame.
  2. index_label specifies the index name.

Example: drop row in pandas dataframe example

In this drop row in pandas dataframe example, we will display the dataframe by dropping one index.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the dataframe by dropping one index
print(data.drop('one'))

Output: drop specific row in pandas result

      college_id       college_name college_address  Total Staff
two        c-021               vvit          guntur         3422
three      c-002           RVR - JC          guntur         5644
four       c-004  Andhra University          guntur          670

From the above code, we are going to  display the dataframe by dropping one index

Hence, the output would be the rows except the first row

 

Thus we can drop specific row in pandas using above method.

 

drop multiple rows in pandas

We can also drop multiple rows in pandas or drop multiple indices at a time.

Just pass a list of indices to be removed inside drop() function.

Exampledrop multiple rows in pandas example is demonstrated below

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the dataframe by dropping one,two and four indices
print(data.drop(['one','two','four']))

Outputdrop multiple rows in pandas - row 1, 2 & 4 is dropped as per above instruction

      college_id college_name college_address  Total Staff
three      c-002     RVR - JC          guntur         5644

From the above code,we are going to  display the dataframe by dropping one, two amd four indices

Hence, the output would be only third row.


Method 2drop row in pandas using drop() with index position

Here, we will use drop() function to remove/drop the rows from the given dataframe. We have to specify the index position to drop particular index. index position starts with 0.

 

So based on the position, it will drop particular row. We can use dataframe.index() method to represent the index.

Syntax:

dataframe.drop(dataframe.index[index_position])

where,

  1. dataframe is the input pandas DataFrame.
  2. index_position parameter specifies the index position in index() method.

Example: drop row in pandas dataframe example

In this drop row in pandas dataframe example, we will display the dataframe by dropping one index.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the dataframe by dropping one index
print(data.drop(data.index[0]))

Outputdrop specific row in pandas - row 0 is dropped

      college_id       college_name college_address  Total Staff
two        c-021               vvit          guntur         3422
three      c-002           RVR - JC          guntur         5644
four       c-004  Andhra University          guntur          670

From the above code, we had dropped first row. Hence the output will be two, three and four rows.

 

This is how we can drop specific row in pandas.

 

drop multiple rows in pandas with index position

We can also drop multiple rows in pandas dataframe or drop multiple indices at a time. Just pass a list of indices positions to be removed inside drop() function.

 

Exampledrop multiple rows in pandas example is demonstrated below

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the dataframe by dropping one,two and four indices
print(data.drop([data.index[0],data.index[1],data.index[3]]))

Outputdrop multiple rows in pandas - row 0, 1 & 3 is dropped (remember, index starts at 0)

      college_id college_name college_address  Total Staff
three      c-002     RVR - JC          guntur         5644

From the above code, we had dropped first,second and forth row. Hence the output will be only third row..

 

Similarly, you can drop all rows in pandas dataframe by specying all indices as shown above. Thus we have seen drop specific row in pandas & drop multiple rows in pandas using two methods.

 


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Mar 15,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!