How can I filter a Pandas DataFrame based on a specific index value?

How can I filter a Pandas DataFrame based on a specific index value?

To filter a Pandas DataFrame based on a specific index value, one can use the .loc function which allows for label-based indexing. This function takes in the specific index value as a parameter and returns a new DataFrame with only the rows that match the given index value. This is a useful tool for selecting and manipulating specific data within a DataFrame, making it easier to analyze and work with the data.

Pandas: Filter by Index Value


You can use the following basic syntax to filter the rows of a pandas DataFrame based on index values:

df_filtered = df[df.index.isin(some_list)]

This will filter the pandas DataFrame to only include the rows whose index values are contained in some_list.

The following examples show how to use this syntax in practice.

Example 1: Filter by Numeric Index Values

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

   points  assists  rebounds
0      18        5        11
1      22        7         8
2      19        7        10
3      14        9         6
4      14       12         6
5      11        9         5
6      20        9         9
7      28        4        12

Notice that the index values are numeric.

Suppose we would like to filter for rows where the index value is equal to 1, 5, 6, or 7.

We can use the following syntax to do so:

#define list of index values
some_list = [1, 5, 6, 7]

#filter for rows in list
df_filtered = df[df.index.isin(some_list)]

#view filtered DataFrame
print(df_filtered)

   points  assists  rebounds
1      22        7         8
5      11        9         5
6      20        9         9
7      28        4        12

Notice that the only rows returned are those whose index value is equal to 1, 5, 6, or 7.

Example 2: Filter by Non-Numeric Index Values

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]},
                   index=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])

#view DataFrame
print(df)

   points  assists  rebounds
A      18        5        11
B      22        7         8
C      19        7        10
D      14        9         6
E      14       12         6
F      11        9         5
G      20        9         9
H      28        4        12

Notice that the index values are character values.

Suppose we would like to filter for rows where the index value is equal to A, C, F, or G.

#define list of index values
some_list = ['A', 'C', 'F', 'G']

#filter for rows in list
df_filtered = df[df.index.isin(some_list)]

#view filtered DataFrame
print(df_filtered)

   points  assists  rebounds
A      18        5        11
C      19        7        10
F      11        9         5
G      20        9         9

Notice that the only rows returned are those whose index value is equal to A, C, F, or G.

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How can I filter a Pandas DataFrame based on a specific index value?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-filter-a-pandas-dataframe-based-on-a-specific-index-value/

stats writer. "How can I filter a Pandas DataFrame based on a specific index value?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-filter-a-pandas-dataframe-based-on-a-specific-index-value/.

stats writer. "How can I filter a Pandas DataFrame based on a specific index value?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-filter-a-pandas-dataframe-based-on-a-specific-index-value/.

stats writer (2024) 'How can I filter a Pandas DataFrame based on a specific index value?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-filter-a-pandas-dataframe-based-on-a-specific-index-value/.

[1] stats writer, "How can I filter a Pandas DataFrame based on a specific index value?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I filter a Pandas DataFrame based on a specific index value?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top