Recall & Review
beginner
What does the
query() method do in pandas?The
query() method filters rows in a DataFrame using a string expression, making it easier and faster to select data based on conditions.Click to reveal answer
beginner
How do you write a condition inside
query() to select rows where column 'age' is greater than 30?You write:
df.query('age > 30') to select rows where the 'age' column has values greater than 30.Click to reveal answer
intermediate
Can you use variables inside
query()? How?Yes, use the
@ symbol before the variable name inside the query string. For example: df.query('age > @min_age') where min_age is a Python variable.Click to reveal answer
intermediate
What is one advantage of using
query() over traditional boolean indexing?query() is often faster and the code looks cleaner and easier to read, especially for complex conditions.Click to reveal answer
advanced
Which characters should you avoid using directly in
query() expressions?Avoid using spaces in column names or special characters like dots. If needed, use backticks around column names with spaces or special characters, e.g.,
df.query('`my column` > 5').Click to reveal answer
What does
df.query('score >= 80') do?✗ Incorrect
The query selects rows where the 'score' column has values greater than or equal to 80.
How do you include a Python variable
threshold inside a query()?✗ Incorrect
Use the '@' symbol before the variable name to include Python variables inside query strings.
Which of these is a valid
query() expression to filter rows where 'city' is 'Paris'?✗ Incorrect
Use double equals and quotes around string values inside the query string.
If a column name has a space, how do you reference it in
query()?✗ Incorrect
Backticks allow referencing column names with spaces or special characters inside query strings.
Which is a benefit of using
query() over boolean indexing?✗ Incorrect
query() often improves readability and performance for complex filtering.Explain how to use the
query() method to filter a DataFrame for rows where a column meets a condition.Think about how you write conditions in plain English but inside quotes.
You got /4 concepts.
Describe how to include a Python variable inside a
query() expression and why this is useful.Remember the special character before the variable name.
You got /3 concepts.