3.1 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonHow to Filter a Pandas DataFrame by Column Values

How to Filter a Pandas DataFrame by Column Values

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

The simplest way to filter a pandas DataFrame by column values is to use the query function.

This tutorial provides several examples of how to use this function in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'C'],
                   'points': [25, 12, 15, 14, 19],
                   'assists': [5, 7, 7, 9, 12],
                   'rebounds': [11, 8, 10, 6, 6]})

#view DataFrame 
df

        team	points	assists	rebounds
0	A	25	5	11
1	A	12	7	8
2	B	15	7	10
3	B	14	9	6
4	C	19	12	6

Example 1: Filter Based on One Column

The following code shows how to filter the rows of the DataFrame based on a single value in the “points” column:

df.query('points == 15')

     team   points    assists  rebounds
2    B      15        7        10

Example 2: Filter Based on Multiple Columns

The following code shows how to filter the rows of the DataFrame based on several values in different columns:

#return rows where points is equal to 15 or 14
df.query('points == 15 | points == 14')

     team   points    assists  rebounds
2    B      15        7        10
3    B      14        9         6

#return rows where points is greater than 13 and rebounds is greater than 6
df.query('points > 13 & points > 6')

     team   points    assists  rebounds
0    A      25        5        11
2    B      15        7        10

Example 3: Filter Based on Values in a List

The following code shows how to filter the rows of the DataFrame based on values in a list

#define list of values
value_list = [12, 19, 25]

#return rows where points is in the list of values
df.query('points in @value_list')

     team  points   assists    rebounds
0    A      25        5        11
1    A      12        7         8
4    C      19       12         6

#return rows where points is not in the list of values
df.query('points not in @value_list') 

     team   points    assists  rebounds
2    B      15        7        10
3    B      14        9         6

Additional Resources

How to Replace Values in Pandas
How to Drop Rows with NaN Values in Pandas
How to Drop Duplicate Rows in Pandas

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