Selecting and filtering
Use loc, iloc and boolean masks to select exactly the rows you mean.
What you will be able to do
- Use df.loc[rows, columns] for label selection.
- Use df.iloc for positional selection.
- Wrap each condition in parentheses.
- Use .isin for membership tests.
In plain English
loc selects by label, iloc selects by position and a boolean mask selects by condition. Use .loc for assignment. Combine conditions with & and |, wrapped in parentheses.
import pandas as pd
df = pd.DataFrame({
'course': ['HTML', 'CSS', 'SQL', 'CSS'],
'minutes': [12, 18, 9, 22],
})
long_css = df[(df['course'] == 'CSS') & (df['minutes'] >= 20)]
print(long_css)
print(df.loc[df['minutes'].idxmax()])Your turn
EasyFilter with two conditions
Select rows where course is CSS and minutes are at least 15.
PYTHON · starter
df[df['course'] == 'CSS']Check your understanding before moving on.
Knowledge checkQuestion 1
Which accessor selects by label?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.