0
0
Pandasdata~10 mins

query() for fast filtering in Pandas - Interactive Code Practice

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

Complete the code to filter rows where the 'age' column is greater than 30 using query().

Pandas
filtered = df.query('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 equals 30 only.
2fill in blank
medium

Complete the code to filter rows where the 'score' column is equal to 100 using query().

Pandas
perfect_scores = df.query('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 rows where score is not 100.
3fill in blank
hard

Fix the error in the query to select rows where 'height' is less than or equal to 170.

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

Fill both blanks to filter rows where 'age' is greater than 25 and 'score' is less than 80 using query().

Pandas
filtered = df.query('age [1] 25 and score [2] 80')
Drag options to blanks, or click blank then click option'
A>
B<
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the operators changes the filter logic.
Using '>=' or '<=' includes boundary values which may not be intended.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each 'name' to their 'score' only if 'score' is greater than 50 using query() filtering.

Pandas
result = {row['[1]']: row['[2]'] for _, row in df.query('[3]').iterrows()}
Drag options to blanks, or click blank then click option'
Aname
Bscore
Cscore > 50
Dage > 50
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'age > 50' filters wrong rows.
Swapping keys and values reverses the dictionary mapping.