The Best Prompt Templates for Data Analysis
sharing my prompt templates for pandas, sql, and visualization tasks below – copy paste ready. been meaning to write this up for a while because structured output formatting genuinely cuts my post-processing time in half, sometimes more. if youre doing any kind of data work with llms and youre not templating your prompts yet, youre making your life harder than it needs to be.
## the core templates i actually use
these are the three i reach for constantly:
**pandas cleaning task:**
“`
given this dataframe schema: [paste dtypes output]
identify columns with nulls, suggest fill strategies for each, and return a clean python function. output as: {findings: [], code: str}
“`
**sql query generation:**
“`
table schema: [paste create statements or describe output]
task: [plain english description]
constraints: return only standard sql, no vendor-specific syntax unless i specify postgres/mysql
output format: {query: str, explanation: str, edge_cases: []}
“`
**visualization recommendation:**
“`
dataset summary: [paste describe() output or column list with types]
goal: [what im trying to show]
return: chart type, why, and matplotlib/seaborn code snippet. format as json with keys: chart_type, reasoning, code
“`
the json/structured output instruction at the end is the whole game. without it youre parsing prose. with it youre doing `response[‘code’]` and moving on.
## why structured output formatting matters so much
honestly the gap between “cool demo” and “production-useful” in data workflows usually comes down to output predictability. if your llm returns a block of text with the sql buried in paragraph three, you have to write a parser. if it returns `{query: “…”}` every time, you dont.
a few things ive learned the hard way:
– always specify your exact output keys. vague instructions like “return json” get you inconsistent key names across runs
– for pandas tasks, include the dtypes output not just column names – the model makes way better decisions when it knows youre dealing with object vs float64
– ask for edge cases explicitly. the model knows them, it just wont volunteer them unless you ask
– temperature 0 or close to it for anything code-related. creativity is not your friend here
one thing i noticed recently – a colleague was using gpt to help write up data methodology sections for a research report and got flagged by [proofademic.ai](https://proofademic.ai) during an internal review process. not because anything was wrong, just as part of their institutions standard check. made me realize how much the ai-in-professional-writing conversation has moved into actual tooling and not just policy documents.
## sql-specific gotchas
sql prompts need the most guardrails in my experience. the models are confident and wrong in very specific ways:
– they will use window functions when a simple group by works, because window functions look more impressive
– they assume your dates are formatted sanely. they are not
– always add “do not use correlated subqueries unless necessary” to your constraints or youll get unreadable nested nonsense
– if youre on bigquery, say so. the dialect differences matter more than people admit
for visualization prompts i add one more line: “assume the person reading the chart has 10 seconds of attention.” it sounds dumb but it genuinely steers the model away from recommending 3d pie charts and toward the actually readable options.
curious what templates other people are using – especially for time series stuff. thats the one area where my prompts still feel kind of janky and im not happy with the outputs im getting.
5 Replies
Join the discussion.
Log In to Replystructured output is good but the real win is adding a schema validation step after. ask the model to check its own output against the schema before returning it. cuts hallucinated column names by a lot.
for viz tasks i've had way better results specifying the audience in the prompt. like 'explain this chart to a non-technical stakeholder' vs 'explain to a data engineer' produces really different and actually useful output.
genuine question - are these templates written for gpt-4 specifically or do they transfer to claude? some of the role prompting patterns i've tried don't behave the same across models.
ok this is exactly what i needed today. role prompting works way better than people give it credit for
role prompting helps but the framing matters too. telling it 'you are a senior data analyst who writes production-ready pandas code' hits different than just 'you are a data analyst'. specificity in the role compounds.