Selecting rows in pandas DataFrame based on conditions

Last Updated : 30 Oct, 2025

Filtering rows in a Pandas DataFrame means selecting specific records that meet defined conditions. Pandas provides several efficient ways to do this, such as boolean indexing, .loc[], .isin(), and .query().

Sample Dataframe

The code below creates a dataframe which we will be using in this example:

Python
import pandas as pd

record = {

 'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],
 'Age': [21, 19, 20, 18, 17, 21],
 'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
 'Percentage': [88, 92, 95, 70, 65, 78] }

dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])
print("Given DataFrame:\n", dataframe)

Output

s1
Preview of Dataframe

Selecting Rows Based on Column Values

You can use comparison operators like >, <, == , != , >=, <= to filter rows.

Example 1: Select rows where Percentage > 80 (basic method)

Python
rslt_df = dataframe[dataframe['Percentage'] > 80]

Output

s2

Explanation:

  • dataframe['Percentage'] > 80: creates a boolean Series for filtering.
  • dataframe[condition]: returns rows where the condition is True.

Example 2: Using .loc []

Python
print(df.loc[df['Percentage'] > 80])

Output

s3

Explanation:

  • .loc[] accesses rows and columns by labels or boolean conditions.
  • Returns the filtered DataFrame similar to boolean indexing.

Example 3: Using .query()

Python
rslt_df = dataframe.query('Percentage > 80')

Output

s3

Explanation:

  • The string 'Percentage > 80' is evaluated for each row. ---------------------------------------------------
  • Only rows where the condition is True are returned.

Selecting Rows Based on Membership (isin())

You can filter rows based on whether a column’s value exists in a list.

Example 1: Select rows where Stream is in a given list

Python
options = ['Math', 'Commerce']
rslt_df = dataframe[dataframe['Stream'].isin(options)]

Output

s4

Explanation:

  • .isin(list) checks if each value exists in the provided list.
  • Returns a boolean Series used to filter rows.

Example 2: using .loc[] with .isin()

Python
rslt_df = dataframe.loc[dataframe['Stream'].isin(options)]

Output

s5

Example 3: Selecting Rows Where Stream Is Not in the List

Python
rslt_df = dataframe.loc[~dataframe['Stream'].isin(options)]

Output

s6

Explanation:

~ operator negates the boolean Series, selecting rows not in the list.

Example 4: Using .isin() with .query()

Python
options = ['Math', 'Commerce']
rslt_df = dataframe.query('Stream in @options')

Output

s7

Selecting Rows with Multiple Conditions

You can combine multiple conditions using logical operators:

  • (&) AND
  • (|) OR
  • (~) NOT

Note: Always use parentheses around each condition.

Example 1: Select rows where Age == 21 AND Stream is in options

Python
rslt_df = dataframe[(dataframe['Age'] == 21) & (dataframe['Stream'].isin(options))]

Output

s8

Example 2: using .loc[] with Multiple Conditions

Python
rslt_df = dataframe.loc[(dataframe['Age'] == 21) & (dataframe['Stream'].isin(options))]

Output

s8

Explanation:

  • (dataframe['Age'] == 21): creates a boolean Series for age condition.
  • dataframe['Stream'].isin(options): creates a boolean Series for stream condition.
  • "&" combines both conditions.
  • .loc[] returns rows satisfying all conditions.

Example 3: Using .query() with Multiple Conditions

Python
options = ['Math', 'Commerce']
rslt_df = dataframe.query('Age >= 20 and Stream in @options')

Output

s8

Explanation:

  • .query() filters rows using a string expression.
  • Age >= 20 and Stream in @options selects rows where Age ≥ 20 and Stream is in the Python list options.
  • @options allows using a Python variable inside the query.
Comment