Pandas-Driven Data Agent Tools
Tool definitions for read_csv, groupby, merge, and describe operations.
Pandas Tools for Data Agents
Rather than generating arbitrary code, you can give a data agent a set of well-defined pandas tools. Each tool performs a specific data operation and returns a structured result.
This approach is safer and more predictable than free-form code generation — the agent selects and chains tools rather than writing code from scratch.
Tool: read_csv
The entry point for any data analysis session. read_csv loads a CSV file into memory and returns a description of its shape and columns so the agent knows what it's working with.
import pandas as pd
from typing import Optional
# In-memory dataframe store (shared across tool calls in one session)
dataframes = {}
def read_csv(path: str, df_name: str = 'df') -> dict:
'''Load a CSV file and return schema info.'''
df = pd.read_csv(path)
dataframes[df_name] = df
return {
'df_name': df_name,
'rows': len(df),
'columns': list(df.columns),
'dtypes': df.dtypes.astype(str).to_dict(),
'sample': df.head(3).to_dict(orient='records'),
'null_counts': df.isnull().sum().to_dict()
}
# Agent usage:
result = read_csv('orders.csv', 'orders')
print(f"Loaded {result['rows']} rows, columns: {result['columns']}")All lessons in this course
- Code Interpreter Pattern for Data Analysis
- Pandas-Driven Data Agent Tools
- Automated Chart and Visualization Generation
- Statistical Summary Agents