0Pricing
AI Engineering Academy · Lesson

Building a Data Analysis Agent

Create an end-to-end data analysis agent that accepts a CSV file and a natural language question, writes pandas and matplotlib code iteratively, and produces a polished report.

The Data Analysis Agent Vision

A data analysis agent accepts a CSV file and a natural language question, then autonomously explores the data, cleans it, computes statistics, generates visualizations, and produces a polished report — all through iterative code generation and execution. This is one of the most practical applications of code agents and directly replicates the workflow of a data analyst, minus the human effort.

The Agent's Overall Architecture

The data analysis agent has four conceptual phases: Exploration (understand the data's shape, types, and quality), Cleaning (handle missing values, outliers, type conversions), Analysis (compute the metrics and statistics that answer the question), and Reporting (generate charts and summarize findings in prose). Each phase corresponds to 1-5 code execution iterations.

DATA_AGENT_SYSTEM_PROMPT = '''
You are a data analysis agent. Given a CSV file and a question, answer it through iterative Python code.

Phases to follow:
1. EXPLORE: Load the data, print shape, dtypes, head(), describe(), and check for nulls.
2. CLEAN: Handle missing values, fix dtypes, remove outliers.
3. ANALYZE: Compute statistics, groupbys, correlations - whatever answers the question.
4. REPORT: Generate a matplotlib chart saved to /workspace/chart.png and write a text summary.

Rules:
- Write code in ```python ... ``` blocks.
- Save intermediate results to /workspace/ for use in later steps.
- Print key findings after each computation so you can observe them.
- When all phases are done, say TASK COMPLETE and summarize the answer.
'''

All lessons in this course

  1. The Code Execution Loop
  2. Sandboxing with Docker and RestrictedPython
  3. State Management Across Execution Steps
  4. Building a Data Analysis Agent
← Back to AI Engineering Academy