0
0
Pandasdata~20 mins

query() for fast filtering in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
   A  B  C
2  3  3  x
3  4  2  y
B
   A  B  C
0  1  5  x
1  2  4  y
CEmpty DataFrame\nColumns: [A, B, C]\nIndex: []
D
   A  B  C
2  3  3  x
3  4  2  y
4  5  1  x
Attempts:
2 left
💡 Hint
Remember that query() filters rows where both conditions are true.
data_output
intermediate
1: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))
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Check which rows have score >= 30 and passed is True.
🔧 Debug
advanced
1: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)
ASyntaxError
BValueError
CKeyError
DNo error, outputs filtered DataFrame
Attempts:
2 left
💡 Hint
Check the comparison operator syntax inside the query string.
🚀 Application
advanced
2: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
Adf.query('val > $threshold')
Bdf.query('val > threshold')
Cdf.query('val > @threshold')
Ddf.query('val > {threshold}')
Attempts:
2 left
💡 Hint
Use @ to refer to Python variables inside query strings.
🧠 Conceptual
expert
2:00remaining
Why use query() over boolean indexing?
Which is the main advantage of using pandas query() method instead of boolean indexing for filtering DataFrames?
Aquery() is always faster than boolean indexing in all cases
Bquery() supports filtering on columns with spaces without any special syntax
Cquery() automatically modifies the original DataFrame without creating a copy
Dquery() allows filtering using a string expression which can be more readable and concise
Attempts:
2 left
💡 Hint
Think about code readability and expression style.