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

Selecting Rows Based on Column Values
You can use comparison operators like >, <, == , != , >=, <= to filter rows.
Example 1: Select rows where Percentage > 80 (basic method)
rslt_df = dataframe[dataframe['Percentage'] > 80]
Output

Explanation:
- dataframe['Percentage'] > 80: creates a boolean Series for filtering.
- dataframe[condition]: returns rows where the condition is True.
Example 2: Using .loc []
print(df.loc[df['Percentage'] > 80])
Output

Explanation:
- .loc[] accesses rows and columns by labels or boolean conditions.
- Returns the filtered DataFrame similar to boolean indexing.
Example 3: Using .query()
rslt_df = dataframe.query('Percentage > 80')
Output

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
options = ['Math', 'Commerce']
rslt_df = dataframe[dataframe['Stream'].isin(options)]
Output

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()
rslt_df = dataframe.loc[dataframe['Stream'].isin(options)]
Output

Example 3: Selecting Rows Where Stream Is Not in the List
rslt_df = dataframe.loc[~dataframe['Stream'].isin(options)]
Output

Explanation:
~ operator negates the boolean Series, selecting rows not in the list.
Example 4: Using .isin() with .query()
options = ['Math', 'Commerce']
rslt_df = dataframe.query('Stream in @options')
Output

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
rslt_df = dataframe[(dataframe['Age'] == 21) & (dataframe['Stream'].isin(options))]
Output

Example 2: using .loc[] with Multiple Conditions
rslt_df = dataframe.loc[(dataframe['Age'] == 21) & (dataframe['Stream'].isin(options))]
Output

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
options = ['Math', 'Commerce']
rslt_df = dataframe.query('Age >= 20 and Stream in @options')
Output

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.