How to perform Drop Rows by Index in Pandas DataFrame? By using the Pandas drop function we can drop/delete the row or list of rows by index labels or position. Using the drop function we can also drop multiple columns by index.
In this article, I will explain how to drop rows by index labels or position using the drop()
function and using different approaches with examples.
Key Points –
- The
drop()
method in Pandas DataFrame is used to remove rows or columns based on their index labels. - By default, the
drop()
method removes rows (axis=0
). You can specifyaxis=1
or column parameter to drop columns instead. - To specify the rows you want to drop, pass a list of index labels to the
index
parameter. - The
inplace=True
parameter can be used to modify the original DataFrame without creating a copy. Otherwise, the method returns a new DataFrame with the specified rows removed.
Quick Examples of Drop Rows by Index
If you are in a hurry, below are some quick examples of how to drop rows of DataFrame using an index.
# Quick examples of drop rows by index
# Example 1: Use DataFrame.iloc[]
# To drop row
df1 = df.iloc[1:]
# Example 2: Drop rows from DataFrame
# Using index
df1 = df.drop(index=df.iloc[3].name)
# Example 3: Drop the row
# Using index labels
df1 = df.drop('r2')
# Example 4: Drop multiple rows
# Using index labels
df1 = df.drop(['r1', 'r4', 'r5'])
# Example 5: Drop rows using index position
df1 = df.drop([df.index[1], df.index[2]])
# Example 6: Set inplace = True & Drop multiple rows
df.drop(['r1', 'r4', 'r5'], inplace = True)
# Example 7: Use DataFrame.iloc[]
# To drop row
df1 = df.iloc[1:]
# Example 8: Drop rows from DataFrame
# Using index
df1 = df.drop(index=df.iloc[3].name)
To run some examples of pandas drop rows by index, let’s create pandas DataFrame from Dictionary.
# Import pandas library
# Create DataFrame
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Python","pandas","Hadoop"],
'Fee' :[20000,25000,22000,24000,30000],
'Duration':['30day','40days','35days','60days','55days'],
'Discount':[1000,2300,2500,2000,3000]
}
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame(technologies, index = index_labels)
print("DataFrame:\n", df)
Yields below output.
Drop Row by Index label
We can drop the DataFrame row by index label using the drop()
function. For that we need to pass the index label (which we want to drop from the DataFrame) into this function, it will drop the specified row from the DataFrame. Let’s see with an example.
# Drop the single row using index labels
df1 = df.drop('r2')
print("Drop the row from DataFrame:\n", df1)
Yields below output.
As we can see row with the index 'r2'
has been dropped from the given DataFrame.
Drop Multiple Rows by Index Label
Moreover, using the drop()
function we can also drop the multiple rows by index labels from the DataFrame. For, that we need to pass a list of rows into this function, it will drop the specified rows and return the new DataFrame with the remaining rows. For example,
# Drop multiple rows using index labels
df1 = df.drop(['r1', 'r4', 'r5'])
print(df1)
Yields below output.
# Output:
Courses Fee Duration Discount
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 2500
Set Inplace = True
Set inplace
param as True
and pass it into the drop()
function along with a list of rows(which we want to drop from the DataFrame), it will update the existing DataFrame with the remaining rows rather than creating a new DataFrame. Let’s see what will happen,
# Set inplace = True & Drop multiple rows
df.drop(['r1', 'r4', 'r5'], inplace = True)
print(df)
# Output:
# Courses Fee Duration Discount
# r2 PySpark 25000 40days 2300
# r3 Python 22000 35days 2500
Pandas Drop Rows by Index Position
Alternatively, using the index position we can select the list of rows(which we want to drop from the DataFrame) from the DataFrame and then, apply the drop()
function, which will drop the specified rows and return the new DataFrame with the remaining rows.
# Drop rows using index position
df1 = df.drop([df.index[1], df.index[2]])
print(df1)
# Output:
# Courses Fee Duration Discount
# r1 Spark 20000 30day 1000
# r4 pandas 24000 60days 2000
# r5 Hadoop 30000 55days 3000
Pandas Drop the First Row using iloc[]
To drop the first row using iloc[]
, you can specify the index range from the second row onwards. For instance, use DataFrame.iloc[1:]
to select all rows from index position 1 (inclusive) onwards. By specifying [1:]
, you’re effectively excluding the first row from the DataFrame.
# Use DataFrame.iloc[] to drop rows
df1 = df.iloc[1:]
print(df1)
Yields below output.
# Output:
Courses Fee Duration Discount
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 2500
r4 pandas 24000 60days 2000
r5 Hadoop 30000 55days 3000
Drop the Row using Index & Name Property
Alternatively, we can drop the rows using name
property. For, that first, we select the row using the iloc[]
property based on its index position then call name
property, it will drop the specified row name using the name property.
# Drop rows from DataFrame using index
df1 = df.drop(index=df.iloc[3].name)
print(df1)
Yields below output.
# Output:
Courses Fee Duration Discount
r1 Spark 20000 30day 1000
r2 PySpark 25000 40days 2300
r3 Python 22000 35days 2500
r5 Hadoop 30000 55days 3000
FAQ on Pandas Drop Rows by Index
You can drop rows by index in a pandas DataFrame using the DataFrame.drop()
method or by using integer-based indexing with DataFrame.iloc[]
.
DataFrame.drop()
is a method that allows you to drop rows by their index labels or index positions. DataFrame.iloc[]
, on the other hand, is primarily used for integer-based indexing, allowing you to select rows and columns by their integer positions.
You can drop rows by index positions using DataFrame.iloc[]
and assign the result back to the original DataFrame variable.
It is possible to drop multiple rows by index positions using DataFrame.iloc[]
. You can specify a list of index positions corresponding to the rows you want to drop.
Conclusion
In this article, I have explained how to drop the rows by index from Pandas DataFrame using index labels and index position with the help of iloc[]
, and drop()
functions with examples.
Happy Learning !!
Related Articles
- Delete Last Row From Pandas DataFrame
- Pandas Drop Rows with NaN Values in DataFrame
- Pandas Drop List of Rows From DataFrame
- Pandas Drop Last N Rows From DataFrame
- Pandas Drop First N Rows From DataFrame
- How to drop first row from the Pandas DataFrame
- Pandas Drop First Three Rows From DataFrame
- How to drop duplicate rows from DataFrame?
- How to drop rows by index position?
- How to drop rows from Pandas DataFrame?
- Drop Pandas rows with condition
- How to add/insert row to Pandas DataFrame?
- Pandas get the number of rows from DataFrame
- Pandas Set Index to Column in DataFrame
- Pandas Drop Rows Based on Column Value
- Pandas.Index.drop_duplicates() Explained
- How to Rename Column by Index in Pandas
- Pandas set index name to DataFrame
- Convert Pandas Index to List
- Pandas Set Index to Column in DataFrame
- Pandas compare two DataFrames row by row
- Difference between two DataFrames
- Pandas remove elements from the Series