How can I select rows in a Pandas dataframe based on specific values in a column?

How can I select rows in a Pandas dataframe based on specific values in a column?

To select rows in a Pandas dataframe based on specific values in a column, one can use the .loc() function. This function allows for filtering of rows based on conditions set on a specific column. The syntax for using .loc() is dataframe.loc[dataframe[‘column’] == ‘specific value’]. This will return a subset of the original dataframe that meets the specified criteria. Additionally, one can use logical operators such as “and” or “or” to further refine the selection of rows. This method is useful for data analysis and manipulation as it allows for targeted extraction of relevant data from a dataframe.

Pandas: Select Rows Based on Column Values


You can use one of the following methods to select rows in a pandas DataFrame based on column values:

Method 1: Select Rows where Column is Equal to Specific Value

df.loc[df['col1'] == value]

Method 2: Select Rows where Column Value is in List of Values

df.loc[df['col1'].isin([value1, value2, value3, ...])]

Method 3: Select Rows Based on Multiple Column Conditions

df.loc[(df['col1'] == value) & (df['col2'] < value)]

The following example shows how to use each method with the following pandas DataFrame:

import pandas as pd

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

#view DataFrame
df

	team	points	rebounds blocks
0	A	5	11	 4
1	A	7	8	 7
2	B	7	10	 7
3	B	9	6	 6
4	B	12	6	 5
5	C	9	5	 8
6	C	9	9	 9
7	C	4	12	 10

Method 1: Select Rows where Column is Equal to Specific Value

The following code shows how to select every row in the DataFrame where the ‘points’ column is equal to 7:

#select rows where 'points' column is equal to 7
df.loc[df['points'] == 7]

	team	points	rebounds blocks
1	A	7	8	 7
2	B	7	10	 7

Method 2: Select Rows where Column Value is in List of Values

The following code shows how to select every row in the DataFrame where the ‘points’ column is equal to 7, 9, or 12:

#select rows where 'points' column is equal to 7
df.loc[df['points'].isin([7, 9, 12])]

        team	points	rebounds blocks
1	A	7	8	 7
2	B	7	10	 7
3	B	9	6	 6
4	B	12	6	 5
5	C	9	5	 8
6	C	9	9	 9

Method 3: Select Rows Based on Multiple Column Conditions

The following code shows how to select every row in the DataFrame where the ‘team’ column is equal to ‘B’ and where the ‘points’ column is greater than 8:

#select rows where 'team' is equal to 'B' and points is greater than 8
df.loc[(df['team'] == 'B') & (df['points'] > 8)]

	team	points	rebounds blocks
3	B	9	6	 6
4	B	12	6	 5

Notice that only the two rows where the team is equal to ‘B’ and the ‘points’ is greater than 8 are returned.

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

Cite this article

stats writer (2024). How can I select rows in a Pandas dataframe based on specific values in a column?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-select-rows-in-a-pandas-dataframe-based-on-specific-values-in-a-column/

stats writer. "How can I select rows in a Pandas dataframe based on specific values in a column?." PSYCHOLOGICAL SCALES, 11 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-select-rows-in-a-pandas-dataframe-based-on-specific-values-in-a-column/.

stats writer. "How can I select rows in a Pandas dataframe based on specific values in a column?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-select-rows-in-a-pandas-dataframe-based-on-specific-values-in-a-column/.

stats writer (2024) 'How can I select rows in a Pandas dataframe based on specific values in a column?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-select-rows-in-a-pandas-dataframe-based-on-specific-values-in-a-column/.

[1] stats writer, "How can I select rows in a Pandas dataframe based on specific values in a column?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I select rows in a Pandas dataframe based on specific values in a column?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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