Bird
Raised Fist0
Pandasdata~20 mins

Boolean indexing in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Boolean Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Boolean Indexing with Multiple Conditions
What is the output of this code snippet using pandas Boolean indexing?
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [10, 20, 30, 40, 50],
    'B': [5, 15, 25, 35, 45]
})

result = df[(df['A'] > 20) & (df['B'] < 40)]
print(result)
A
    A   B
2  30  25
4  50  45
B
    A   B
1  20  15
2  30  25
3  40  35
C
    A   B
2  30  25
3  40  35
DEmpty DataFrame\nColumns: [A, B]\nIndex: []
Attempts:
2 left
💡 Hint
Remember that both conditions must be true for each row to be included.
❓ data_output
intermediate
1:30remaining
Number of Rows After Boolean Filtering
Given the DataFrame below, how many rows remain after applying the Boolean filter?
Pandas
import pandas as pd

df = pd.DataFrame({
    'score': [88, 92, 79, 93, 85],
    'passed': [True, True, False, False, False]
})

filtered = df[df['passed'] & (df['score'] > 90)]
print(len(filtered))
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Count rows where 'passed' is true and 'score' is greater than 90.
🔧 Debug
advanced
2:00remaining
Identify the Error in Boolean Indexing Code
What error does this code raise when executed?
Pandas
import pandas as pd

df = pd.DataFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]})

result = df[(df['X'] > 1) & (df['Y'] < 6)]
print(result)
ATypeError: 'and' operator cannot be used with Series
BSyntaxError: invalid syntax
CKeyError: 'X'
DNo error, prints filtered DataFrame
Attempts:
2 left
💡 Hint
Check how to combine multiple Boolean conditions in pandas.
🚀 Application
advanced
2:00remaining
Filter DataFrame Rows Based on String Condition
Which option correctly filters rows where the 'city' column contains the substring 'York'?
Pandas
import pandas as pd

df = pd.DataFrame({
    'city': ['New York', 'Los Angeles', 'Yorkshire', 'Boston'],
    'population': [8000000, 4000000, 500000, 700000]
})
Adf[df['city'].str.startswith('York')]
Bdf[df['city'] == 'York']
Cdf[df['city'].str.match('York')]
Ddf[df['city'].str.contains('York')]
Attempts:
2 left
💡 Hint
Use a method that checks if the substring appears anywhere in the string.
🧠 Conceptual
expert
2:30remaining
Understanding Boolean Indexing with Missing Values
Given this DataFrame with missing values, what is the output of the Boolean indexing operation?
Pandas
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'A': [1, 2, np.nan, 4],
    'B': [np.nan, 2, 3, 4]
})

result = df[df['A'] > 1]
print(result)
AEmpty DataFrame\nColumns: [A, B]\nIndex: []
B
     A    B
1  2.0  2.0
3  4.0  4.0
C
     A    B
1  2.0  2.0
2  NaN  3.0
3  4.0  4.0
D
     A    B
0  1.0  NaN
1  2.0  2.0
3  4.0  4.0
Attempts:
2 left
💡 Hint
Remember how comparisons with NaN behave in Boolean indexing.

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