Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Boolean filtering in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Boolean Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Boolean filtering with pandas
What is the output of this code snippet filtering a DataFrame by a Boolean condition?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})
filtered = df[df['A'] > 2]
print(filtered)
A
   A  B
2  3  7
3  4  8
B
   A  B
0  1  5
1  2  6
C
   A  B
1  2  6
2  3  7
D
Empty DataFrame
Columns: [A, B]
Index: []
Attempts:
2 left
💡 Hint
Remember that filtering keeps rows where the condition is True.
❓ data_output
intermediate
1:30remaining
Count of rows after Boolean filtering
After applying this Boolean filter, how many rows remain in the DataFrame?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'score': [55, 70, 65, 80, 90]})
filtered = df[df['score'] >= 70]
print(len(filtered))
A5
B2
C4
D3
Attempts:
2 left
💡 Hint
Count how many scores are 70 or above.
🔧 Debug
advanced
2:00remaining
Identify the error in Boolean filtering code
What error does this code raise when trying to filter a DataFrame?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'X': [10, 20, 30]})
filtered = df[df['X' > 15]]
print(filtered)
ANo error, outputs filtered DataFrame
BKeyError: True
CSyntaxError: invalid syntax
DTypeError: '>' not supported between instances of 'str' and 'int'
Attempts:
2 left
💡 Hint
Look carefully at the condition inside the brackets.
🚀 Application
advanced
2:30remaining
Filter DataFrame rows with multiple Boolean conditions
Which option correctly filters rows where column 'age' is over 30 and 'income' is at least 50000?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'age': [25, 35, 40, 28], 'income': [40000, 60000, 50000, 45000]})
Adf[(df['age'] > 30) & (df['income'] >= 50000)]
Bdf[df['age'] > 30 and df['income'] >= 50000]
Cdf[(df['age'] > 30) | (df['income'] >= 50000)]
Ddf[df['age'] > 30 or df['income'] >= 50000]
Attempts:
2 left
💡 Hint
Use bitwise operators (&, |) for combining conditions in pandas.
🧠 Conceptual
expert
3:00remaining
Understanding Boolean indexing with missing values
Given a DataFrame with missing values, what does this Boolean filter return?
Data Analysis Python
import pandas as pd
import numpy as np

df = pd.DataFrame({'val': [1, np.nan, 3, np.nan, 5]})
filtered = df[df['val'] > 2]
print(filtered)
AAll rows including NaNs
BRows where 'val' is greater than 2, including NaNs
CRows where 'val' is greater than 2, excluding NaNs
DEmpty DataFrame because NaNs cause errors in filtering
Attempts:
2 left
💡 Hint
NaN compared with any number returns False in Boolean filtering.

Practice

(1/5)
1. What does boolean filtering do in data analysis with Python?
easy
A. Changes the data type of columns
B. Removes duplicate rows from the data
C. Sorts the data in ascending order
D. Selects rows where a condition is true

Solution

  1. Step 1: Understand boolean filtering purpose

    Boolean filtering is used to pick rows that meet a true condition.
  2. Step 2: Compare options to definition

    Only Selects rows where a condition is true describes selecting rows based on true conditions.
  3. Final Answer:

    Selects rows where a condition is true -> Option D
  4. Quick Check:

    Boolean filtering = Select rows true condition [OK]
Hint: Boolean filtering picks rows where condition is true [OK]
Common Mistakes:
  • Confusing filtering with sorting
  • Thinking it changes data types
  • Assuming it removes duplicates
2. Which of the following is the correct syntax to filter rows where column age is greater than 30 in a DataFrame df?
easy
A. df['age' > 30]
B. df[df['age'] > 30]
C. df.age[df.age > 30]
D. df[df['age'] < 30]

Solution

  1. Step 1: Identify correct boolean filtering syntax

    The correct way is to put the condition inside square brackets after the DataFrame.
  2. Step 2: Check each option

    df[df['age'] > 30] uses df[df['age'] > 30], which is correct syntax. df['age' > 30] is invalid syntax. df.age[df.age > 30] incorrectly filters only the age column, returning a Series not DataFrame. df[df['age'] < 30] filters less than 30, not greater.
  3. Final Answer:

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

    Correct syntax = df[df['age'] > 30] [OK]
Hint: Use df[condition] with column in quotes for filtering [OK]
Common Mistakes:
  • Missing brackets around condition
  • Using wrong comparison operator
  • Not quoting column name
3. Given the DataFrame df below:
import pandas as pd
df = pd.DataFrame({
  'name': ['Anna', 'Bob', 'Cara', 'Dan'],
  'score': [85, 92, 78, 90],
  'passed': [True, True, False, True]
})

What will df[df['score'] > 80 and df['passed']] return?
medium
A. ValueError due to missing parentheses
B. Rows where score > 80 or passed is true
C. Rows where score > 80 and passed is true
D. Empty DataFrame

Solution

  1. Step 1: Analyze the boolean expression

    The expression uses and without parentheses around each condition, which causes a ValueError in pandas.
  2. Step 2: Understand pandas boolean filtering rules

    Each condition must be in parentheses when combined with & or |. So df['score'] > 80 and df['passed'] raises ValueError.
  3. Final Answer:

    ValueError due to missing parentheses -> Option A
  4. Quick Check:

    Use parentheses around conditions with & or | [OK]
Hint: Always wrap each condition in parentheses when using & or | [OK]
Common Mistakes:
  • Not using parentheses around conditions
  • Confusing & with and
  • Expecting output without fixing syntax
4. Consider this code snippet:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4]})
filtered = df[df['A'] > 2 or df['A'] < 4]

What is the error and how to fix it?
medium
A. ValueError: Use parentheses around conditions with or
B. SyntaxError: Missing colon after if
C. No error, code runs fine
D. ValueError: Cannot compare integers

Solution

  1. Step 1: Identify the error cause

    The expression df['A'] > 2 or df['A'] < 4 lacks parentheses around each condition, causing a ValueError.
  2. Step 2: Fix the error by adding parentheses

    Correct syntax is df[(df['A'] > 2) | (df['A'] < 4)] to ensure proper evaluation.
  3. Final Answer:

    ValueError: Use parentheses around conditions with or -> Option A
  4. Quick Check:

    Parentheses needed around each condition with | or & [OK]
Hint: Wrap each condition in parentheses when using | or & [OK]
Common Mistakes:
  • Forgetting parentheses around conditions
  • Using Python 'or' instead of |
  • Assuming no error with missing parentheses
5. You have a DataFrame df with columns age and income. You want to filter rows where age is between 25 and 40 inclusive, and income is above 50000. Which code correctly applies boolean filtering?
hard
A. df[(df['age'] > 25) | (df['age'] < 40) & (df['income'] > 50000)]
B. df[df['age'] >= 25 & df['age'] <= 40 & df['income'] > 50000]
C. df[(df['age'] >= 25) & (df['age'] <= 40) & (df['income'] > 50000)]
D. df[df['age'] >= 25 and df['age'] <= 40 and df['income'] > 50000]

Solution

  1. Step 1: Understand the filtering conditions

    We want age between 25 and 40 inclusive, so age >= 25 and age <= 40, and income > 50000.
  2. Step 2: Check correct syntax for combining conditions

    Conditions must be in parentheses and combined with &. df[(df['age'] >= 25) & (df['age'] <= 40) & (df['income'] > 50000)] correctly uses parentheses and & operators.
  3. Step 3: Identify errors in other options

    df[df['age'] >= 25 & df['age'] <= 40 & df['income'] > 50000] lacks parentheses around each condition, causing errors. df[(df['age'] > 25) | (df['age'] < 40) & (df['income'] > 50000)] uses | instead of & incorrectly. df[df['age'] >= 25 and df['age'] <= 40 and df['income'] > 50000] uses Python 'and' which does not work with pandas Series.
  4. Final Answer:

    df[(df['age'] >= 25) & (df['age'] <= 40) & (df['income'] > 50000)] -> Option C
  5. Quick Check:

    Use parentheses and & for multiple conditions [OK]
Hint: Use parentheses and & to combine multiple conditions [OK]
Common Mistakes:
  • Using Python 'and' instead of &
  • Missing parentheses around conditions
  • Mixing & and | incorrectly
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