Translate Pandas To SQL And Back
Converts a data transformation faithfully between pandas and SQL while preserving exact semantics.
Prompt
ROLE: You are an analytics engineer fluent in both pandas and SQL who preserves exact semantics across the translation. CONTEXT: I have this transformation written in [SOURCE_LANGUAGE] (pandas or SQL) and want an equivalent in [TARGET_LANGUAGE]. Schema/dtypes: [SCHEMA]. Engine/pandas version notes: [ENVIRONMENT]. Source code: ``` [SOURCE_CODE] ``` TASK: 1. Describe what the source does as an ordered list of logical operations (filter, group, aggregate, window, merge, pivot). 2. Translate to [TARGET_LANGUAGE], matching results row-for-row. 3. Call out every place where the two languages differ in default behavior (NULL vs NaN handling, join key dtype coercion, group-by dropping NaN keys, ordering not guaranteed in SQL, index semantics) and how you reconciled it. 4. Provide a small equivalence check the user can run on sample data. OUTPUT FORMAT: Logical steps -> Translated code in fenced block -> Semantic-difference notes (bulleted) -> Equivalence test. CONSTRAINTS: Do not assume row order unless the source guarantees it; add an explicit ORDER BY or sort_values if order matters. Match NULL/NaN handling exactly. Preserve column names and dtypes. Flag any operation that has no clean equivalent and propose the closest faithful option.
How to use this prompt
- 1
Copy the prompt above and paste it into ChatGPT, Claude, or Gemini — or open it in the visual Studio to edit each part on a canvas and run it with your own key.
- 2
Replace any bracketed placeholders with your specifics. The more concrete your context and constraints, the sharper the result — see the 5-part prompt structure.
- 3
Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.
Techniques in this prompt
Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.
Learn this techniqueAsks the model to reason step by step before answering — ideal for multi-step, logical, or analytical tasks.
Learn this techniquePins the response to a defined structure so it drops straight into your workflow.
Learn this techniqueRecommended models
Build on this prompt
Open it in the visual Studio to wire it into a full workflow with your own API key — or learn the craft behind prompts like this.
More in Data Analysis & SQL
Translate Business Questions Into SQL
Turns a plain-English stakeholder question into a correct, well-commented SQL query against a known schema.
Optimize A Slow SQL Query
Diagnoses why a query is slow and rewrites it with targeted, explained optimizations and an index plan.
Debug A SQL Query That Returns Wrong Results
Systematically finds the logic error producing incorrect numbers and delivers a corrected, verified query.
Explain An Unfamiliar SQL Query In Plain English
Reverse-engineers a complex inherited query into a clear narrative, business meaning, and risk list.