1. Home
  2. Data Science
  3. Pandas
  4. df.query('price > @limit') is evaluated inside a function…

Added 30 Nov -0001

df.query('price > @limit') is evaluated inside a function that holds limit as a local variable. Why does this work?

  1. The @ prefix escapes the name, allowing identifiers that are not valid Python
  2. The @ prefix resolves the name in the caller's scope instead of as a column label Correct answer
  3. The @ prefix marks the referenced name as a pandas Series, so query can broadcast it
  4. The @ prefix disables parsing of the expression, passing it to Python's eval directly

Correct answer

B. The @ prefix resolves the name in the caller's scope instead of as a column label

17 views

MCQSEXAM

Explanation

Local variable interpolation in query relies on the @ sigil, which tells the expression evaluator to look the name up in the calling frame rather than among the frame's columns. This avoids building temporary strings and keeps evaluation vectorised. Column names that clash with Python keywords are wrapped in backticks instead.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Pandas MCQs

Practise all Pandas MCQs

Similar MCQs