How can I select rows from a Pandas DataFrame based on multiple conditions using the loc method?

How can I select rows from a Pandas DataFrame based on multiple conditions using the loc method?

The loc method in Pandas allows for the selection of specific rows in a DataFrame based on multiple conditions. This can be achieved by using logical operators such as “and” and “or” to create complex conditional statements. The loc method provides a flexible way to filter data in a DataFrame, making it a powerful tool for data analysis and manipulation. By specifying the desired conditions within the loc method, users can easily extract the desired rows from a DataFrame, allowing for more targeted and precise data selection.

Select Rows by Multiple Conditions Using Pandas loc


You can use the following methods to select rows of a pandas DataFrame based on multiple conditions:

Method 1: Select Rows that Meet Multiple Conditions

df.loc[((df['col1'] == 'A') & (df['col2'] == 'G'))]

Method 2: Select Rows that Meet One of Multiple Conditions

df.loc[((df['col1'] > 10) | (df['col2'] < 8))] 

The following examples show how to use each of these methods in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'G', 'F', 'F', 'G', 'G', 'F', 'F'],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

	team	position assists rebounds
0	A	G	 5	 11
1	A	G	 7	 8
2	A	F	 7	 10
3	A	F	 9	 6
4	B	G	 12	 6
5	B	G	 9	 5
6	B	F	 9	 9
7	B	F	 4	 12

Method 1: Select Rows that Meet Multiple Conditions

The following code shows how to only select rows in the DataFrame where the team is equal to ‘A’ and the position is equal to ‘G’:

#select rows where team is equal to 'A' and position is equal to 'G'
df.loc[((df['team'] == 'A') & (df['position'] == 'G'))]

	team	position assists rebounds
0	A	G	 5	 11
1	A	G	 7	 8

There were only two rows in the DataFrame that met both of these conditions.

Method 2: Select Rows that Meet One of Multiple Conditions

The following code shows how to only select rows in the DataFrame where the assists is greater than 10 or where the rebounds is less than 8:

#select rows where assists is greater than 10 or rebounds is less than 8
df.loc[((df['assists'] > 10) | (df['rebounds'] < 8))]

	team	position assists rebounds
3	A	F	 9	 6
4	B	G	 12	 6
5	B	G	 9	 5

There were only three rows in the DataFrame that met both of these conditions.

Note: In these two examples we filtered rows based on two conditions but using the & and | operators, we can filter on as many conditions as we’d like.

Additional Resources

Cite this article

stats writer (2024). How can I select rows from a Pandas DataFrame based on multiple conditions using the loc method?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-select-rows-from-a-pandas-dataframe-based-on-multiple-conditions-using-the-loc-method/

stats writer. "How can I select rows from a Pandas DataFrame based on multiple conditions using the loc method?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-select-rows-from-a-pandas-dataframe-based-on-multiple-conditions-using-the-loc-method/.

stats writer. "How can I select rows from a Pandas DataFrame based on multiple conditions using the loc method?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-select-rows-from-a-pandas-dataframe-based-on-multiple-conditions-using-the-loc-method/.

stats writer (2024) 'How can I select rows from a Pandas DataFrame based on multiple conditions using the loc method?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-select-rows-from-a-pandas-dataframe-based-on-multiple-conditions-using-the-loc-method/.

[1] stats writer, "How can I select rows from a Pandas DataFrame based on multiple conditions using the loc method?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I select rows from a Pandas DataFrame based on multiple conditions using the loc method?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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