6.1 C
London
Saturday, December 21, 2024
HomePandas in PythonGeneral Functions in PythonPandas: How to Filter by Index Value

Pandas: How to Filter by Index Value

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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.

We can use the following syntax to do so:

#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.

Additional Resources

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

How to Insert a Row Into a Pandas DataFrame
How to Drop First Row in Pandas DataFrame
How to Drop Rows in Pandas DataFrame Based on Condition

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories