Bird
Raised Fist0
Pandasdata~10 mins

Boolean indexing in Pandas - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select rows where the 'age' column is greater than 30.

Pandas
filtered_df = df[df['age'] [1] 30]
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will select rows with age less than 30.
Using '==' will select rows where age is exactly 30.
2fill in blank
medium

Complete the code to select rows where the 'score' column is equal to 100.

Pandas
perfect_scores = df[df['score'] [1] 100]
Drag options to blanks, or click blank then click option'
A==
B>=
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes syntax errors.
Using '>=' selects scores greater than or equal to 100, not just 100.
3fill in blank
hard

Fix the error in the code to select rows where 'height' is less than 170.

Pandas
short_people = df[df['height'] [1] 170]
Drag options to blanks, or click blank then click option'
A>
B=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '<' causes syntax errors.
Using '==' selects rows where height equals 170, not less than 170.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.

Pandas
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the word in the condition instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their scores only if the score is greater than 50.

Pandas
result = [1]: [2] for word, score in data.items() if [3]
Drag options to blanks, or click blank then click option'
Aword.upper()
Bscore
Cscore > 50
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase words as keys instead of uppercase.
Not filtering scores greater than 50.

Practice

(1/5)
1. What does boolean indexing in pandas do?
easy
A. Changes the data type of columns
B. Sorts the DataFrame by column values
C. Selects rows based on True/False conditions
D. Merges two DataFrames together

Solution

  1. Step 1: Understand boolean indexing

    Boolean indexing uses True/False values to filter rows in a DataFrame.
  2. Step 2: Identify its purpose

    It selects only rows where the condition is True, filtering data easily.
  3. Final Answer:

    Selects rows based on True/False conditions -> Option C
  4. Quick Check:

    Boolean indexing = row filtering [OK]
Hint: Boolean indexing filters rows with True conditions [OK]
Common Mistakes:
  • Confusing boolean indexing with sorting
  • Thinking it changes data types
  • Mixing it up with DataFrame merging
2. Which of the following is the correct syntax to select rows where column 'age' is greater than 30 in a DataFrame df?
easy
A. df['age' > 30]
B. df[df['age'] > 30]
C. df[df.age = 30]
D. df[df['age'] => 30]

Solution

  1. Step 1: Check correct boolean indexing syntax

    Use df[condition], where condition is df['age'] > 30.
  2. Step 2: Identify syntax errors in other options

    df['age' > 30] misplaces brackets, C uses assignment (=) instead of comparison, D uses invalid operator =>.
  3. Final Answer:

    df[df['age'] > 30] -> Option B
  4. Quick Check:

    Correct syntax uses df[condition] with comparison [OK]
Hint: Use df[condition] with correct comparison operators [OK]
Common Mistakes:
  • Using = instead of > for comparison
  • Wrong bracket placement
  • Using invalid operators like =>
3. Given the DataFrame df below:
   name  age  score
0  Anna   25     88
1  Bob    32     75
2  Cara   29     95
3  Dave   35     60
4  Ella   28     82

What will df[(df['age'] > 28) & (df['score'] > 70)] return?
medium
A. Rows with age > 28 and score > 70: Anna and Ella
B. Rows with age > 28 and score > 70: Bob and Dave
C. Rows with age > 28 and score > 70: Dave only
D. Rows with age > 28 and score > 70: Bob and Cara

Solution

  1. Step 1: Identify rows where age > 28

    Rows: Bob (32), Cara (29), Dave (35) satisfy age > 28.
  2. Step 2: From those, select rows with score > 70

    Bob (75), Cara (95), Dave (60). Dave fails score > 70, so only Bob and Cara remain.
  3. Final Answer:

    Rows with age > 28 and score > 70: Bob and Cara -> Option D
  4. Quick Check:

    Filter by both conditions = Bob and Cara [OK]
Hint: Check each condition separately, then combine results [OK]
Common Mistakes:
  • Including Dave who has score 60
  • Mixing up & and | operators
  • Ignoring parentheses around conditions
4. What is wrong with this code snippet for filtering rows where score is above 80?
df[df['score'] > 80 & df['age'] < 30]
medium
A. Missing parentheses around conditions causing operator precedence error
B. Using & instead of | for combining conditions
C. Incorrect column name 'score'
D. DataFrame df is not defined

Solution

  1. Step 1: Understand operator precedence in pandas boolean indexing

    Bitwise operators (&, |) require parentheses around each condition to avoid errors.
  2. Step 2: Identify missing parentheses

    Code lacks parentheses around df['score'] > 80 and df['age'] < 30, causing wrong evaluation.
  3. Final Answer:

    Missing parentheses around conditions causing operator precedence error -> Option A
  4. Quick Check:

    Use parentheses with & and | [OK]
Hint: Always wrap each condition in parentheses when using & or | [OK]
Common Mistakes:
  • Forgetting parentheses around each condition
  • Confusing & with logical and (and)
  • Assuming no error without parentheses
5. You have a DataFrame df with columns 'city', 'temperature', and 'humidity'. You want to select rows where city is either 'Paris' or 'London' and temperature is above 20. Which code correctly applies boolean indexing?
hard
A. df[((df['city'] == 'Paris') | (df['city'] == 'London')) & (df['temperature'] > 20)]
B. df[df['city'] == 'Paris' | df['city'] == 'London' & (df['temperature'] > 20)]
C. df[df['city'] == ('Paris' or 'London') & df['temperature'] > 20]
D. df[(df['city'] == 'Paris') | (df['city'] == 'London') & (df['temperature'] > 20)]

Solution

  1. Step 1: Understand condition grouping with parentheses

    Conditions combined with | must be wrapped in parentheses to group city checks correctly.
  2. Step 2: Check each option for correct syntax and logic

    df[((df['city'] == 'Paris') | (df['city'] == 'London')) & (df['temperature'] > 20)] correctly groups city conditions and combines with temperature condition using &.
  3. Final Answer:

    df[((df['city'] == 'Paris') | (df['city'] == 'London')) & (df['temperature'] > 20)] -> Option A
  4. Quick Check:

    Group OR conditions with parentheses before AND [OK]
Hint: Group OR conditions in parentheses before AND conditions [OK]
Common Mistakes:
  • Missing parentheses causing wrong condition order
  • Using Python 'or' instead of |
  • Trying to compare city to ('Paris' or 'London') directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard