Editor

Filter with Boolean Indexing

Intermediate · Write the code · from Pandas Indexing: loc, iloc, Boolean Indexing, and Selection

From the lesson: Pandas Indexing: loc, iloc, Boolean Indexing, and Selection

You have a DataFrame with thousands of rows. You need row 42. You need every customer from Texas. You need the salary in the third row, second column. Pandas gives you three different ways to grab exactly the data you want - and confusing them is one of the most common beginner mistakes.

Read the lesson

Filter the DataFrame to find all employees with a salary greater than 70000. Print the names of matching employees as a list.

import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'salary': [70000, 85000, 52000, 120000]
})
# Filter for salary > 70000 and print names

More Pandas Indexing exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz