📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials AI Agents and Automation Data Analysis Agents

Data Analysis Agents

5 min read Quiz at the end
Data analysis agents write and execute pandas code to answer natural language questions about datasets.

AI-Powered Data Analysis Agents

import anthropic, pandas as pd
from langchain_experimental.agents import create_pandas_dataframe_agent
from langchain_openai import ChatOpenAI

# LangChain Pandas Agent
df    = pd.read_csv("sales.csv")
llm   = ChatOpenAI(model="gpt-4o", temperature=0)
agent = create_pandas_dataframe_agent(
    llm, df, verbose=True, allow_dangerous_code=True
)

result = agent.invoke("Which product had the highest revenue growth this quarter?")

# Custom Anthropic data agent
client = anthropic.Anthropic()

def data_analyst_agent(question: str, df: pd.DataFrame) -> str:
    schema = df.dtypes.to_string()
    sample = df.head(3).to_string()
    stats  = df.describe().to_string()

    resp = client.messages.create(
        model="claude-opus-4-5", max_tokens=1000,
        system="You are a data analyst. Write Python pandas code to answer questions.",
        messages=[{"role":"user","content":f"Schema:
{schema}
Sample:
{sample}
Stats:
{stats}

Question: {question}

Write pandas code then execute it."}]
    )
    code = extract_code(resp.content[0].text)
    exec_globals = {"df": df, "pd": pd}
    exec(code, exec_globals)
    return str(exec_globals.get("result","Done"))
Topic Quiz · 1 questions

Test your understanding before moving on

1. What does a data analysis agent do when asked a question about a DataFrame?
💡 Data analysis agents generate code to answer questions, execute it in a sandbox, and return the output.