Python Pandas

Pandas Filter by Index

The “Pandas” data analysis library provides various methods and functions to work with tabular data, such as DataFrames and Series. In Python, different data operations can be performed on Pandas, such as the addition of data, removing data, or extracting data. Filtering data by the specified index is a common task in Pandas that helps us access specific data, perform calculations, or apply transformations.

This guide presented an in-depth overview of how to filter Pandas DataFrame by index via the below content:

How to Filter DataFrame of Pandas by Index in Python?

The “DataFrame.filter()” method is utilized in Python to filter the Pandas DataFrame and retrieve only the columns or rows based on the specified index labels.

Syntax

DataFrame.filter(items=None, like=None, regex=None, axis=None)

In the above syntax:

  • The “items” and “like” parameters specify the axis labels list and string we wanted to filter.
  • The “regex” parameter specifies the axis labels regular expression we want to filter.
  • The “axis” parameter indicates the axis to filter on, such as “0” for the index and “1” for the column.

Example 1: Filter DataFrame of Pandas by Index to Select Single Row

Let’s take this code to filter the DataFrame by specific index value:

import pandas
data = {'Name': ['Joseph','Lily','Anna','Henry','Tim'], 'Age': [20, 23, 33, 15, 35]}
df = pandas.DataFrame(data)
print(df)
df = df.filter(items = [3], axis=0)
print('\n',df)

In the above code, the “pandas” library is imported. The “pd.DataFrame()” function is used to create a Pandas DataFrame, and the “df.filter()” method takes the specified index (list-like) as an argument and retrieves the row data of that index by filtering out others. The “axis=0” parameter represents the index axis to filter on.

Output

The specific row value has been retrieved by filtering out other indexes.

Example 2: Filter DataFrame of Pandas by Index to Select Multiple Row

Here is an example that filters Pandas DataFrame multiple indexes:

import pandas
data = {'Name': ['Joseph','Lily','Anna','Henry','Tim'], 'Age': [20, 23, 33, 15, 35]}
df = pandas.DataFrame(data)
print(df)
df = df.filter(items = [1,4], axis=0)
print('\n',df)

In the above code, the “df.filter()” method takes the list containing the multiple index labels as an argument to filter out and subset the selected rows.

Output

The specified rows have been filtered out from the DataFrame based on the index value.

Example 3: Filter DataFrame of Pandas by Non-Numeric Index

The non-numeric custom index can also be used to filter the Pandas DataFrame. Here is an example:

import pandas
data = {'Name': ['Joseph','Lily','Anna','Henry','Tim'], 'Age': [20, 23, 33, 15, 35]}
df = pandas.DataFrame(data, index=['A', 'B', 'C', 'D', 'E'])
print(df)
df = df.filter(items = ['B', 'E'], axis=0)
print('\n',df)

In this code, the “pd.DataFrame()” takes the dictionary data and custom non-numeric index as an argument to create DataFrame. The “df.filter()” takes the multiple non-numeric indexes as an argument and retrieves a subset of filtered rows.

Output

DataFrame has been filtered based on the index value for the specified rows.

Example 4: Filter DataFrame of Pandas by Index Using the “df.isin()” Function

Let’s overview the following code to filter Pandas DataFrame by index using the “df.isin()” Function:

import pandas
data = {'Name': ['Joseph','Lily','Anna','Henry','Tim'], 'Age': [20, 23, 33, 15, 35]}
df = pandas.DataFrame(data, index=['A', 'B', 'C', 'D', 'E'])
print(df)
df2 = df[df.index.isin(['B', 'E'])]
print('\n',df2)

In this code, the “df.index.isin()” function takes the specified index as an argument and returns the subset of filtered rows based on that index.

Output

The df.isin() function successfully returns the Pandas DataFrame by specified index.

Conclusion

In Python, the “DataFrame.filter()” method is utilized to filter the Pandas DataFrame by using the specified index label. The non-numeric index can also be passed to the function to filter DataFrame. Moreover, the df.isin() function can also be used in Python to filter the Pandas DataFrame. This tutorial presented a detailed guide on filtering Pandas DataFrame by index using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.