Pandas & Data Analysis·Pandas from first Series to analysis project

Selecting and filtering

Use loc, iloc and boolean masks to select exactly the rows you mean.

Beginner 13 min read Lesson 5 of 300 Challenge included
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.

filter.pyPython
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

Filter with two conditions

Easy

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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: GroupBy
Lesson 5 of 300 · 0% of course