Data Analysis Agent with OpenClaw
A data analysis agent loads datasets from CSV files, databases, or APIs; computes descriptive statistics; detects anomalies and trends; runs Python code for custom transformations; and generates comprehensive written reports with chart-ready data.
Required Tools
# agents/analyst-agent.yaml
name: analyst-agent
model: gpt-4o
temperature: 0.1
tools:
- csv_reader
- sql_tool
- python_executor # sandboxed Python for pandas/numpy
- chart_generator # produces chart JSON for Chart.js
system: |
You are a data analyst agent. Think step by step.
- Load the data, inspect shape and dtypes first.
- Always state assumptions when data is ambiguous.
- Use Python for calculations — do not guess numeric results.
- Round all percentages to 1 decimal place.
- Highlight the 3 most important insights in every report.Loading and Inspecting Datasets
from openclaw import Agent
agent = Agent.from_config("agents/analyst-agent.yaml")
def inspect_dataset(path: str) -> str:
return agent.run(
f"Load the dataset at '{path}'. Report:\n"
"1. Shape (rows × columns)\n"
"2. Column names and dtypes\n"
"3. Missing values per column (count + %)\n"
"4. First 3 rows as a formatted table\n"
"5. Basic descriptive stats for numeric columns"
)Anomaly Detection
def detect_anomalies(path: str, column: str) -> str:
return agent.run(
f"In dataset '{path}', column '{column}':\n"
"1. Calculate mean and standard deviation.\n"
"2. Flag rows where the value is beyond 3 standard deviations.\n"
"3. Also flag values that are statistically improbable using IQR method.\n"
"4. Return a JSON list of anomalous row indices with their values and z-scores."
)Trend Analysis
def analyse_trend(path: str, date_col: str, value_col: str, period: str = "month") -> str:
return agent.run(
f"Dataset: '{path}'. Analyse the trend of '{value_col}' over time.\n"
f"Group by {period}. Calculate:\n"
"- Period-over-period growth rate (%)\n"
"- 3-period moving average\n"
"- Overall trend direction (up/down/flat)\n"
"- Seasonality if detectable\n"
"Return as JSON suitable for a line chart."
)Report Generation
import pathlib, datetime
def generate_report(path: str, output_dir: str = "./reports") -> str:
report_md = agent.run(
f"Perform a full analysis of '{path}'. Generate a Markdown report with:\n"
"# Executive Summary (3 bullet points)\n"
"## Data Overview\n"
"## Key Metrics Table\n"
"## Top 3 Insights\n"
"## Anomalies Found\n"
"## Trends and Forecast\n"
"## Recommendations\n"
"Use concrete numbers throughout."
)
output = pathlib.Path(output_dir)
output.mkdir(parents=True, exist_ok=True)
date_str = datetime.date.today().isoformat()
out_path = output / f"report-{date_str}.md"
out_path.write_text(report_md, encoding="utf-8")
return str(out_path)Time-Series Forecasting
def forecast_next_periods(path: str, date_col: str, value_col: str, periods: int = 4) -> str:
return agent.run(
f"Using data from '{path}' (date='{date_col}', value='{value_col}'):\n"
f"1. Fit a simple linear regression trend.\n"
f"2. Forecast the next {periods} periods.\n"
"3. Return JSON: {\"forecast\": [{\"period\": \"...\", \"predicted\": 0.0, "
"\"low_95\": 0.0, \"high_95\": 0.0}], \"r_squared\": 0.0}"
)Frequently Asked Questions
Which Python libraries should I preinstall for data analysis agents?
Essential: pandas (data manipulation), numpy (numerical computation), matplotlib + seaborn (visualization), scipy (statistics). For ML: scikit-learn (models), statsmodels (time series). For large datasets: polars (faster than pandas), dask (parallel processing). Install with pip install 'openclaw[data-science]' to get the full bundle.
How do I prevent the agent from running dangerous pandas operations?
Use a code sandbox: wrap exec() in a restricted namespace with only safe imports allowed. Block filesystem writes, network access, and subprocess calls. For production, execute code in Docker containers with resource limits (CPU, memory, timeout). Log all executed code for security audits. Alternatively, use the pandasai library which provides a safer query interface over raw code execution.
Can the agent handle datasets larger than memory?
Yes, with Dask (lazy evaluation) or Polars (streaming mode). Configure the agent to use dask.dataframe instead of pandas.DataFrame for chunked processing. For SQL databases, teach the agent to query with LIMIT clauses and pagination rather than loading full tables. Store intermediate results in a database rather than in-memory to prevent OOM crashes on multi-step analyses.
How accurate are LLM-generated data visualizations?
For standard charts (bar, line, scatter), GPT-4 and Claude Opus generate correct matplotlib code ~85% of the time. Accuracy drops for complex multi-panel figures or custom styling. Always validate chart outputs manually before including in reports. Use a review_plot() tool that shows the agent the rendered image and asks “Does this match the request?” to catch errors before returning to the user.