Home › Data analysis › Write analysis code: how to do it with AI

Write analysis code: how to do it with AI

Last checked

From official vendor pages; selected tasks also include dated logged-out free-plan trials. Report an error.

Intermediate Risk: Medium 📊 Data analysis

Ask an assistant for analysis code (pandas, SQL or R) instead of a one-off answer, and you get something you can rerun next month and check line by line. The code is only as good as your description of the data, so share the column names and types.

Who it’s for: Analysts, researchers, students and operations staff who repeat the same analysis on new data.

Why medium risk: Code can run without errors and still compute the wrong thing, for example by joining tables incorrectly or dropping missing values silently.

Best pick

ChatGPT has the highest score on our published rubric (94/100) of the 4 tools listed for this task. Lists spreadsheet creation and data analysis. Its free plan includes limited data analysis (verified on its pricing page).

Step by step

  1. Describe the data: file type, columns and types, and a few fake sample rows.
  2. State the question in one sentence and the output you want (a table, a chart, a CSV).
  3. Ask for commented code that prints row counts at each step.
  4. Run it on your data and check totals against a number you already know.
  5. Ask it to explain any step you don't understand.
  6. Save the code in a notebook or script with a note of the date and data source.

AI prompts to write data analysis code

Replace the [brackets] with your details. See all prompt examples

Write the analysis
Write [pandas / SQL] code to answer: [question]. Data: [columns and types]. Print row counts after each filter or join, and comment each step.
Check my code
Review this analysis code for mistakes: wrong joins, silently dropped rows, wrong aggregations. Explain each issue in plain English.

[code]
Make it reusable
Turn this notebook into a script that takes the file path and month as arguments and writes the result to CSV. Add basic checks that fail loudly if columns are missing.

Illustrative example

You paste

Question: average order value by month for 2026, from orders.csv (order_id, date, amount, status).

A good answer looks like

Code outline: read CSV; keep status == 'paid' and print how many rows remain; parse dates; group by month; mean of amount. Check: January's total matches the finance report before trusting the rest.

Illustrative example written for this guide, not output captured from a specific tool.

Which tool to pick, and why

Reasons come from the capabilities, free-plan details and prices we've checked on official pages (separate from any dated trial above). Sources: claude.com/pricing · chatgpt.com/pricing · cursor.com/pricing · github.com/features/copilot/plans

Common mistakes

Privacy tip

Describe the schema with fake rows rather than uploading personal data, or use your organisation's approved plan.

Claude (incl. Claude Code) setting · ChatGPT (incl. Work & Codex) setting · Cursor setting · Sources: privacy.claude.com/en/articles/12109829-how-do-i-change-my-model-improvement-privacy-settings · help.openai.com/en/articles/7730893-data-controls-in-chatgpt · cursor.com/help/security-and-privacy/privacy

Related tasks

Report an error on this page