Open In App

How to drop rows in Pandas DataFrame by index labels?

Last Updated : 01 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pandas provide data analysts a way to delete and filter dataframe using the .drop() method. Rows can be removed using index labels or column names using this method. In this article, we will see how to drop rows in Pandas Dataframe by index labels.

Pandas Drop Rows by Index

Creating a Simple Pandas Dataframe.

Python3

# import pandas library
import pandas as pd

# dictionary with list object in values
details = {
    'Name': ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age': [23, 21, 22, 21],
    'University': ['BHU', 'JNU', 'DU', 'BHU'],
}

# creating a Dataframe object
df = pd.DataFrame(details, columns=['Name', 'Age', 'University'],
                  index=['a', 'b', 'c', 'd'])

df

Output

pandas-drop-row-1

Drop Rows by Index in Pandas DataFrame Examples

Delete a Single Row in DataFrame by Row Index Label

To remove a row from a DataFrame by its index label, the drop() function in pandas can be utilized. By passing the specific index label of the row we want to eliminate, this function removes that particular row from the DataFrame. An illustration of this process is demonstrated in the following example.

Python3

# import pandas library
import pandas as pd

# dictionary with list object in values
details = {
    'Name': ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age': [23, 21, 22, 21],
    'University': ['BHU', 'JNU', 'DU', 'BHU'],
}

# creating a Dataframe object
df = pd.DataFrame(details, columns=['Name', 'Age', 'University'],
                  index=['a', 'b', 'c', 'd'])

# return a new dataframe by dropping a
# row 'c' from dataframe
update_df = df.drop('c')

update_df

Output

      Name  Age University
a Ankit 23 BHU
b Aishwarya 21 JNU
d Shivangi 21 BHU

Delete Multiple Rows in DataFrame by Index Labels

The drop() function in pandas allows for the removal of multiple rows by providing a list of index labels. By passing a list containing the desired rows to be dropped, the function eliminates those specified rows from the DataFrame. The result is a new DataFrame that retains the remaining rows after the removal process. This versatile functionality enhances the flexibility of data manipulation in pandas.

Python3

# import pandas library
import pandas as pd

# dictionary with list object in values
details = {
    'Name': ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age': [23, 21, 22, 21],
    'University': ['BHU', 'JNU', 'DU', 'BHU'],
}

# creating a Dataframe object
df = pd.DataFrame(details, columns=['Name', 'Age', 'University'],
                  index=['a', 'b', 'c', 'd'])

# return a new dataframe by dropping a row
# 'b' & 'c' from dataframe
update_df = df.drop(['b', 'c'])

update_df

Output

      Name  Age University
a Ankit 23 BHU
d Shivangi 21 BHU

Delete a Multiple Rows by Index Position in DataFrame

In this example, we are using pandas library to construct a DataFrame ‘df’ from a dictionary named ‘details,’ organizing the data with specific columns and row indices. Subsequently, a new DataFrame ‘update_df’ is created by dropping rows with index positions 1 and 2 (representing ‘b’ and ‘c’) from the original DataFrame ‘df’. As a result, ‘update_df’ contains data solely from rows ‘a’ and ‘d,’ reflecting the exclusion of the specified rows based on their respective index positions.

Python3

# import pandas library
import pandas as pd

# dictionary with list object in values
details = {
    'Name': ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age': [23, 21, 22, 21],
    'University': ['BHU', 'JNU', 'DU', 'BHU'],
}

# creating a Dataframe object
df = pd.DataFrame(details, columns=['Name', 'Age', 'University'],
                  index=['a', 'b', 'c', 'd'])

# return a new dataframe by dropping a row
# 'b' & 'c' from dataframe using their
# respective index position
update_df = df.drop([df.index[1], df.index[2]])

update_df

Output

      Name  Age University
a Ankit 23 BHU
d Shivangi 21 BHU

Delete rows from dataFrame in Place

By setting the inplace parameter to True and including it in the drop() function alongside a list of rows to be removed from the DataFrame, the original DataFrame is modified directly. This means that the specified rows are dropped, and the existing DataFrame is updated to retain only the remaining rows. This in-place operation can be particularly useful when you want to modify the DataFrame without creating a new one.

Python3

# import pandas library
import pandas as pd

# dictionary with list object in values
details = {
    'Name': ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age': [23, 21, 22, 21],
    'University': ['BHU', 'JNU', 'DU', 'BHU'],
}

# creating a Dataframe object
df = pd.DataFrame(details, columns=['Name', 'Age', 'University'],
                  index=['a', 'b', 'c', 'd'])

# dropping a row 'c' & 'd' from actual dataframe
df.drop(['c', 'd'], inplace=True)

df

Output

      Name  Age University
a Ankit 23 BHU
b Aishwarya 21 JNU

Pandas Drop the First Row using iloc[]

The DataFrame.iloc[] attribute enables the dropping of rows based on their index positions. This function selects a specific portion of the DataFrame, determined by the provided indices, while disregarding the remaining rows or columns. The indices specified within the iloc[] function correspond to the positions of the rows to be dropped, offering a precise method for row removal based on numerical positions in the DataFrame.

Python3

# import pandas library
import pandas as pd

# dictionary with list object in values
details = {
    'Name': ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
    'Age': [23, 21, 22, 21],
    'University': ['BHU', 'JNU', 'DU', 'BHU'],
}

# creating a Dataframe object
df = pd.DataFrame(details, columns=['Name', 'Age', 'University'],
                  index=['a', 'b', 'c', 'd'])

# use iloc to drop the 3rd row
update_df = df.drop('c')

print(update_df)

Output

      Name  Age University
a Ankit 23 BHU
b Aishwarya 21 JNU
d Shivangi 21 BHU


Improve
Next Article

Similar Reads

Drop rows from Pandas dataframe with missing values or NaN in columns
Pandas provides various data structures and operations for manipulating numerical data and time series. However, there can be cases where some data might be missing. In Pandas missing data is represented by two value: None: None is a Python singleton object that is often used for missing data in Python code. NaN: NaN (an acronym for Not a Number),
4 min read
Drop a list of rows from a Pandas DataFrame
Let us see how to drop a list of rows in a Pandas DataFrame. We can do this using the Pandas drop() function. We will also pass inplace = True and axis=0 to denote row, as it makes the changes we make in the instance stored in that instance without doing any assignment. Creating Dataframe to drop a list of rows C/C++ Code # import the module import
3 min read
Drop specific rows from multiindex Pandas Dataframe
In this article, we will learn how to drop specific rows from the multi-index DataFrame. First, let us create the multi-index DataFrame. The steps are given below: C/C++ Code import numpy as np import pandas as pd mldx_arrays = [np.array(['lion', 'lion', 'lion', 'bison', 'bison', 'bison', 'hawk', 'hawk', 'hawk']), np.array(['height', 'weight', 'spe
3 min read
Python | Delete rows/columns from DataFrame using Pandas.drop()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages which makes importing and analyzing data much easier. In this article, we will how to delete a row in Excel using Pandas as well as delete a column from DataFrame using Pandas. Pandas Data
4 min read
How to Drop Rows with NaN Values in Pandas DataFrame?
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results. In this article, we will
4 min read
How to drop a level from a multi-level column index in Pandas Dataframe ?
In this article, we will learn about how to drop a level from a multi-level column index. But before that, we need to know what is a multi-level index. A multi-level index dataframe is a type of dataframe that contains multiple level or hierarchical indexing. In this article, we will be creating a dataframe of our own choice with multiple column in
5 min read
Get minimum values in rows or columns with their index position in Pandas-Dataframe
Let’s discuss how to find minimum values in rows & columns of a Dataframe and also their index position. a) Find the minimum value among rows and columns : Dataframe.min() : This function returns the minimum of the values in the given object. If the input is a series, the method will return a scalar which will be the minimum of the values in the se
4 min read
How to get rows/index names in Pandas dataframe
While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names in order to perform some certain operations. Let's discuss how to get row names in Pandas dataframe. First, let's create a simple dataframe with nba.csv C/C++ Code # Import pandas package import pandas as pd # making data frame data = p
2 min read
Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc
Indexing in Pandas means selecting rows and columns of data from a Dataframe. It can be selecting all the rows and the particular number of columns, a particular number of rows, and all the columns or a particular number of rows and columns each. Indexing is also known as Subset selection. Creating a Dataframe to Select Rows & Columns in Pandas
8 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
Article Tags :
Practice Tags :