16.6 C
London
Friday, July 5, 2024
HomePandas in PythonDataFrame Functions in PythonHow to Filter a Pandas DataFrame on Multiple Conditions

How to Filter a Pandas DataFrame on Multiple Conditions

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

Often you may want to filter a pandas DataFrame on more than one condition. Fortunately this is easy to do using boolean operations.

This tutorial provides several examples of how to filter the following pandas DataFrame on multiple conditions:

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 on Multiple Conditions Using ‘And’

The following code illustrates how to filter the DataFrame using the and (&) operator:

#return only rows where points is greater than 13 and assists is greater than 7
df[(df.points > 13) & (df.assists > 7)]

        team	points	assists	rebounds
3	B	14	9	6
4	C	19	12	6

#return only rows where team is 'A' and points is greater than or equal to 15
df[(df.team == 'A') & (df.points >= 15)]


        team	points	assists	rebounds
0	A	25	5	11

Example 2: Filter on Multiple Conditions Using ‘Or’

The following code illustrates how to filter the DataFrame using the or (|) operator:

#return only rows where points is greater than 13 or assists is greater than 7
df[(df.points > 13) | (df.assists > 7)]


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

#return only rows where team is 'A' or points is greater than or equal to 15
df[(df.team == 'A') | (df.points >= 15)]

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

Example 3: Filter on Multiple Conditions Using a List

The following code illustrates how to filter the DataFrame where the row values are in some list.

#define a list of values
filter_list = [12, 14, 15]

#return only rows where points is in the list of values
df[df.points.isin(filter_list)]

	team	points	assists	rebounds
1	A	12	7	8
2	B	15	7	10
3	B	14	9	6

#define another list of values
filter_list2 = ['A', 'C']

#return only rows where team is in the list of values
df[df.team.isin(filter_list2)]


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

You can find more pandas tutorials here.

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