0
0
Pandasdata~5 mins

Selecting rows by condition in Pandas

Choose your learning style9 modes available
Introduction

We select rows by condition to find and work with only the data that matters to us.

You want to see all customers who spent more than $100.
You need to find all students who scored above 90 in a test.
You want to filter products that are in stock.
You want to analyze only data from a specific city.
You want to check records where a value is missing or zero.
Syntax
Pandas
filtered_df = df[df['column_name'] <condition> value]
Use square brackets [] to filter rows based on a condition.
The condition inside the brackets returns True or False for each row.
Examples
Selects rows where the 'age' column is greater than 30.
Pandas
filtered_df = df[df['age'] > 30]
Selects rows where the 'city' column is exactly 'New York'.
Pandas
filtered_df = df[df['city'] == 'New York']
Selects rows where 'score' is at least 80 and 'passed' is True.
Pandas
filtered_df = df[(df['score'] >= 80) & (df['passed'] == True)]
Selects rows where the 'name' column contains the text 'John'.
Pandas
filtered_df = df[df['name'].str.contains('John')]
Sample Program

This code creates a small table of people with their ages and cities. Then it selects only those people older than 30 and prints them.

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob', 'Charlie', 'David'],
        'age': [25, 35, 30, 40],
        'city': ['New York', 'Los Angeles', 'New York', 'Chicago']}

df = pd.DataFrame(data)

# Select rows where age is greater than 30
filtered_df = df[df['age'] > 30]

print(filtered_df)
OutputSuccess
Important Notes

Use & for AND and | for OR when combining multiple conditions. Remember to put each condition in parentheses.

String conditions can use methods like str.contains() for partial matches.

Filtering does not change the original data unless you assign it back.

Summary

Use square brackets with a condition to select rows.

Conditions return True or False for each row to keep or drop it.

You can combine multiple conditions with & (and) or | (or).