Pandas: How to 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)]

Method 4: Select Rows Based on String Pattern Matching

df.loc[df['col1'].str.contains('pattern')]

Method 5: Select Rows Based on Conditional Column Values

df.loc[df['col1'].apply(lambda x: condition)]

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.

Method 4: Select Rows Based on String Pattern Matching

The following code shows how to select every row in the DataFrame where the ‘team’ column contains the letter ‘B’:

#select rows where 'team' column contains 'B'
df.loc[df['team'].str.contains('B')]

	team	points	rebounds blocks
2	B	7	10	 7
3	B	9	6	 6
4	B	12	6	 5

This method is particularly useful when working with text data where you need to filter based on partial matches, regular expressions, or specific patterns within strings.

Method 5: Select Rows Based on Conditional Column Values

The following code shows how to select rows where the ‘points’ column is above the average of all points:

#calculate average points
avg_points = df['points'].mean()

#select rows where 'points' is above average using apply with lambda
df.loc[df['points'].apply(lambda x: x > avg_points)]

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

This approach allows you to filter rows based on calculations or more complex conditions that depend on the data itself. You can apply custom functions using lambda expressions to implement sophisticated filtering logic.

Wrapping Up

When working with pandas DataFrames, selecting and filtering rows based on column values is a common operation that you’ll use frequently. The five methods discussed above provide useful ways to filter your data:

  • Method 1 helps you filter for exact matches
  • Method 2 allows filtering for multiple specific values
  • Method 3 enables combining multiple conditions
  • Method 4 provides text-based pattern matching
  • Method 5 supports filtering based on calculated or dynamic conditions

Remember that you can chain these methods or combine them to create more complex filters. For performance on large datasets, consider using query() or Boolean indexing methods depending on your specific use case.

Additional Resources

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

How to Select Rows by Index in Pandas
How to Select Unique Rows in Pandas
How to Select Rows Where Value Appears in Any Column in Pandas

2 Replies to “Pandas: How to Select Rows Based on Column Values”

  1. Great Howto.

    But, If I want to select rows using multiple string values (eg. team “B” OR “C”), how it can be done? The “.isin” doesn’t work. I am a newbie dev and think this solution must be so simple that nobody put it in howto´s .

    Tnx

    1. Hi Rafael,

      Thank you for your question! You’re right that this is a common need when working with pandas.

      To select rows where a string column equals one of multiple values (like team “B” OR “C”), the `.isin()` method actually works perfectly. Here’s how you would do it:

      # Select rows where team is either ‘B’ OR ‘C’
      df.loc[df[‘team’].isin([‘B’, ‘C’])]

      This should give you all rows where the team is either B or C. If for some reason this isn’t working for you, there could be whitespace or case sensitivity issues with your data.

      Let me know if you have any other questions.

Leave a Reply

Your email address will not be published. Required fields are marked *