Challenge - 5 Problems
Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of query() with multiple conditions
What is the output DataFrame after running this code?
Pandas
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3, 4, 5], 'B': [5, 4, 3, 2, 1], 'C': ['x', 'y', 'x', 'y', 'x'] }) result = df.query('A > 2 and B < 4') print(result)
Attempts:
2 left
💡 Hint
Remember that query() filters rows where both conditions are true.
✗ Incorrect
The query filters rows where A > 2 and B < 4. Rows with indices 2 and 3 satisfy both conditions.
❓ data_output
intermediate1:30remaining
Number of rows after query filtering
How many rows remain after applying this query filter?
Pandas
import pandas as pd df = pd.DataFrame({ 'score': [10, 20, 30, 40, 50], 'passed': [True, False, True, False, True] }) filtered = df.query('score >= 30 and passed == True') print(len(filtered))
Attempts:
2 left
💡 Hint
Check which rows have score >= 30 and passed is True.
✗ Incorrect
Rows with indices 2 and 4 have score >= 30 and passed True, so 2 rows remain.
🔧 Debug
advanced1:30remaining
Identify the error in query() usage
What error does this code raise when run?
Pandas
import pandas as pd df = pd.DataFrame({'x': [1, 2, 3]}) result = df.query('x => 2') print(result)
Attempts:
2 left
💡 Hint
Check the comparison operator syntax inside the query string.
✗ Incorrect
The operator '=>' is invalid syntax in query expressions; it should be '>='.
🚀 Application
advanced2:00remaining
Using query() with variables
Given variable threshold = 3, which option correctly filters rows where column 'val' is greater than threshold using query()?
Pandas
import pandas as pd df = pd.DataFrame({'val': [1, 2, 3, 4, 5]}) threshold = 3
Attempts:
2 left
💡 Hint
Use @ to refer to Python variables inside query strings.
✗ Incorrect
In pandas query(), variables from Python scope are referenced with @ prefix.
🧠 Conceptual
expert2:00remaining
Why use query() over boolean indexing?
Which is the main advantage of using pandas query() method instead of boolean indexing for filtering DataFrames?
Attempts:
2 left
💡 Hint
Think about code readability and expression style.
✗ Incorrect
query() lets you write filtering conditions as strings, making code cleaner and easier to read compared to complex boolean indexing.