When working with Pandas DataFrame, there may be situations where you need to remove specific rows based on their index labels. In this article, we will explore various methods to achieve this.
Method 1: Using the drop() method
The drop()
method in Pandas allows us to remove rows by specifying their index labels.
import pandas as pd data = {'Name': ['John', 'Emma', 'Peter', 'Olivia'], 'Age': [25, 30, 22, 35], 'City': ['New York', 'London', 'Paris', 'Sydney']} df = pd.DataFrame(data, index=['A', 'B', 'C', 'D']) # Drop rows by index labels df = df.drop(['B', 'C']) print(df)
The output will be:
Name Age City A John 25 New York D Olivia 35 Sydney
Method 2: Using the slice notation
Another way to drop rows by index labels is by using the slice notation [start:end]
.
import pandas as pd data = {'Name': ['John', 'Emma', 'Peter', 'Olivia'], 'Age': [25, 30, 22, 35], 'City': ['New York', 'London', 'Paris', 'Sydney']} df = pd.DataFrame(data, index=['A', 'B', 'C', 'D']) # Drop rows by index labels using slice notation df = df[:'A'].append(df['D':]) print(df)
The output will be:
Name Age City A John 25 New York D Olivia 35 Sydney
Method 3: Using the index attribute
Pandas DataFrame has an index
attribute that allows us to access and modify the index labels directly.
import pandas as pd data = {'Name': ['John', 'Emma', 'Peter', 'Olivia'], 'Age': [25, 30, 22, 35], 'City': ['New York', 'London', 'Paris', 'Sydney']} df = pd.DataFrame(data, index=['A', 'B', 'C', 'D']) # Drop rows by index labels using the index attribute df = df[df.index != 'B'] df = df[df.index != 'C'] print(df)
The output will be:
Name Age City A John 25 New York D Olivia 35 Sydney
Method 4: Using boolean indexing
You can use boolean indexing to filter out rows based on their index labels.
import pandas as pd data = {'Name': ['John', 'Emma', 'Peter', 'Olivia'], 'Age': [25, 30, 22, 35], 'City': ['New York', 'London', 'Paris', 'Sydney']} df = pd.DataFrame(data, index=['A', 'B', 'C', 'D']) # Drop rows by index labels using boolean indexing df = df[~df.index.isin(['B', 'C'])] print(df)
The output will be:
Name Age City A John 25 New York D Olivia 35 Sydney
Method 5: Using the drop() method with inplace=True
The drop()
method can also be used with the inplace=True
parameter to modify the DataFrame in-place without the need to assign the result back to the DataFrame variable.
import pandas as pd data = {'Name': ['John', 'Emma', 'Peter', 'Olivia'], 'Age': [25, 30, 22, 35], 'City': ['New York', 'London', 'Paris', 'Sydney']} df = pd.DataFrame(data, index=['A', 'B', 'C', 'D']) # Drop rows by index labels using inplace=True df.drop(['B', 'C'], inplace=True) print(df)
The output will be:
Name Age City A John 25 New York D Olivia 35 Sydney
Last Updated on May 17, 2023 by admin