In this article we will discuss how to drop a list of rows from a Pandas DataFrame either by providing row index positions or row names in a list.
A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method.
Let’s create a dataframe with 4 rows and 4 columns
import pandas as pd # Create dataframe for students df=pd.DataFrame({'id':[58,59,60,61], 'name':['sravan','jyothika','preethi','srinadh'], 'age':[22,21,22,23], 'subjects':['java','php','sql','python']}) # Display the Dataframe print(df)
Output:
id name age subjects 0 58 sravan 22 java 1 59 jyothika 21 php 2 60 preethi 22 sql 3 61 srinadh 23 python
We are using dataframe.drop() function to remove list of rows from the dataframe. This function is used to drop the rows based on row index / row name in the given dataframe.
Syntax is as follows:
df.drop( axis=0, index=None, columns=None, level=None, inplace=False)
Where, df is the input dataframe and other parameters are,
Frequently Asked:
- Create an empty DataFrame with just column names
- Drop List of Rows from a Pandas DataFrame
- Replace Column values with Dictionary in Pandas Dataframe
- Pandas: Drop Rows with All NaN values
- axis = 0 specifies the row position
- index specifies row index , By default it is None
- columns are the column names to be specified.
- inplace is a parameter used to make changes in the dataframe if it is set to true.
It returns the new Dataframe with modified contents of inplace parameter is False, otherwise returns None.
Drop Single Row from Dataframe based on row numbers in a list
Here we are going to drop single row.In a list we are providing the row index
import pandas as pd # Create dataframe for students df=pd.DataFrame({'id':[58,59,60,61], 'name':['sravan','jyothika','preethi','srinadh'], 'age':[22,21,22,23], 'subjects':['java','php','sql','python']}) # Display dataframe print(df) # Delete the rows by index position given in list df = df.drop([1]) # Display dataframe print(df)
Output:
id name age subjects 0 58 sravan 22 java 1 59 jyothika 21 php 2 60 preethi 22 sql 3 61 srinadh 23 python id name age subjects 0 58 sravan 22 java 2 60 preethi 22 sql 3 61 srinadh 23 python
Here , the second row with index number 1 is removed and displayed the remaining rows.
Drop multiple rows from Dataframe by index positions in List
Here we are going to drop more than one row at a time. In a list we are providing row indices separated by comma. We are also providing axis parameter which sets to 0. It is an optional parameter.
import pandas as pd # Create Dataframe for students df=pd.DataFrame({'id':[58,59,60,61], 'name':['sravan','jyothika','preethi','srinadh'], 'age':[22,21,22,23], 'subjects':['java','php','sql','python']}) # Display the Dataframe print(df) print("---------------") # Drop first, second and third row df = df.drop( [0,1,2], axis=0) # Display the Dataframe print(df)
Output:
id name age subjects 0 58 sravan 22 java 1 59 jyothika 21 php 2 60 preethi 22 sql 3 61 srinadh 23 python --------------- id name age subjects 3 61 srinadh 23 python
Here , the first three rows got removed and displayed the remaining row.
Drop List of Rows from Dataframe using index() with drop()
In this case, we are using index() function inside drop() function to remove the rows based on the index.
Syntax:
df.drop(df.index[index_values])
It takes a list of index positions of rows. and selects those rows only. Then we pass that to drop() function, which deletes those rows in a copy of dataframe and returns that modified copy.
Drop single row
Here we are going to drop single row using index.
Example: In this example, we are going to drop first row
import pandas as pd # Create dataframe for students df=pd.DataFrame({'id':[58,59,60,61], 'name':['sravan','jyothika','preethi','srinadh'], 'age':[22,21,22,23], 'subjects':['java','php','sql','python']}) # Display dataframe print(df) print("---------------") # Drop first row df = df.drop(df.index[0]) # Display dataframe print(df)
Output:
id name age subjects 0 58 sravan 22 java 1 59 jyothika 21 php 2 60 preethi 22 sql 3 61 srinadh 23 python --------------- id name age subjects 1 59 jyothika 21 php 2 60 preethi 22 sql 3 61 srinadh 23 python
Drop multiple rows
Here we are going to drop multiple rows using index with slice operator
Syntax:
df.drop(df.index[start:stop])
where,
- start is the starting index of row.
- stop is the ending index of row.
Example:
In this example, we are going to drop from first row to third row
#import pandas module import pandas as pd # Create dataframe for students df=pd.DataFrame({'id':[58,59,60,61], 'name':['sravan','jyothika','preethi','srinadh'], 'age':[22,21,22,23], 'subjects':['java','php','sql','python']}) # Display dataframe print(df) print("---------------") # Drop from first row to third row by index df = df.drop(df.index[0:3]) # Display dataframe print(df)
Output:
id name age subjects 0 58 sravan 22 java 1 59 jyothika 21 php 2 60 preethi 22 sql 3 61 srinadh 23 python --------------- id name age subjects 3 61 srinadh 23 python
Drop Rows from Dataframe by List of Row names
Here we are going to use row names inside drop function. This will accept a list of row names to be removed.
Drop single row
Here we are going to specify only one row name inside drop() position.
Syntax:
df.drop(['row_name'])
Example: Here, we are going to drop third row.
import pandas as pd #create dataframe for students df=pd.DataFrame({'id':[58,59,60,61], 'name':['sravan','jyothika','preethi','srinadh'], 'age':[22,21,22,23], 'subjects':['java','php','sql','python']}, index=['row1','row2','row3','row4']) # display dataframe print(df) print("---------------") # drop third row print(df.drop(['row3']))
Output:
id name age subjects row1 58 sravan 22 java row2 59 jyothika 21 php row3 60 preethi 22 sql row4 61 srinadh 23 python --------------- id name age subjects row1 58 sravan 22 java row2 59 jyothika 21 php row4 61 srinadh 23 python
Drop multiple rows by List of Row names
Here we are going to specify only multiple row names inside drop() position.
Syntax:
df.drop(['row_name',......,'row_name'])
Example: Here, we are going to drop second and third row.
import pandas as pd # Create dataframe for students df=pd.DataFrame({'id':[58,59,60,61], 'name':['sravan','jyothika','preethi','srinadh'], 'age':[22,21,22,23], 'subjects':['java','php','sql','python']},index=['row1','row2','row3','row4']) # Display dataframe print(df) print("---------------") # Drop second and third row print(df.drop(['row2','row3']))
Output:
id name age subjects row1 58 sravan 22 java row2 59 jyothika 21 php row3 60 preethi 22 sql row4 61 srinadh 23 python --------------- id name age subjects row1 58 sravan 22 java row4 61 srinadh 23 python
Summary
We learned about different ways to delete list of rows from Dataframe by row index positions or labels.