How to filter by Index Value in Pandas

In Pandas, filtering by index value is done by using the .loc or .iloc accessor. The .loc accessor can be used to filter dataframes by row labels, while the .iloc accessor can be used to filter dataframes by row numbers. The syntax for both accessors is dataframe.loc[row_labels,column_labels] or dataframe.iloc[row_numbers,column_numbers] respectively. This can be used to return any subset of rows and columns from a given Pandas dataframe.


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.

x