Multiple Conditions
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 lessonFilter the DataFrame to find people who are older than 25 AND live in NYC. Print their names as a list.
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
'age': [28, 34, 22, 45, 30],
'city': ['NYC', 'LA', 'NYC', 'Chicago', 'NYC']
})
# Filter: age > 25 AND city == 'NYC'
More Pandas Indexing exercises
- Select with locIntermediate
- Select with ilocIntermediate
- Filter with Boolean IndexingIntermediate
- Fix the Bug: Boolean FilterIntermediate
- Update Values with locIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz