An AI insight generator that takes a pandas summary DataFrame, sends it to Claude, and prints a clean executive narrative with key findings, anomalies, and recommendations — automatically generated from your data.
Get your Claude API key
Go to console.anthropic.com, sign up for a free account, and create an API key. You get $5 in free credits — enough for hundreds of data analysis calls.
Install the Anthropic SDK:
pip install anthropic
Store your key as an environment variable — never hard-code it in your scripts:
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
$env:ANTHROPIC_API_KEY="sk-ant-your-key-here"
Ask Claude about your data
import pandas as pd
import anthropic
# Load and summarize your data
df = pd.read_csv("sales.csv")
summary = df.groupby("region").agg(
total_revenue=("revenue", "sum"),
avg_deal=("revenue", "mean"),
num_deals=("revenue", "count")
)
# Convert summary to a readable string for Claude
data_str = summary.to_string()
# Ask Claude to analyze it
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": f"""You are a data analyst. Analyze this sales data and provide:
1. Key findings (2-3 bullet points)
2. Any anomalies or concerns
3. One specific recommendation
Data:
{data_str}"""
}
]
)
print(response.content[0].text)
The key insight: You're not sending raw rows to Claude — you're sending a summary. For a 100,000-row dataset, you compute the summary with pandas (fast, cheap, local) and then send the 20-row summary to Claude. This keeps API costs low and responses focused.
Analyze multiple segments automatically
import pandas as pd
import anthropic
df = pd.read_csv("sales.csv")
client = anthropic.Anthropic()
def get_insight(segment_name, data_str):
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=512,
messages=[{
"role": "user",
"content": f"In 2-3 sentences, summarize the key insight from this {segment_name} data:\n{data_str}"
}]
)
return response.content[0].text
# Analyze each product line
insights = {}
for product in df["product"].unique():
product_data = df[df["product"] == product]
summary = product_data.groupby("region")["revenue"].sum().to_string()
insights[product] = get_insight(f"{product} sales", summary)
print(f"\n{product}:\n{insights[product]}")
Day 3 Complete
- Set up the Anthropic SDK and API key securely
- Sent a pandas summary to Claude and received natural-language insights
- Built a batch analysis function that loops over data segments
- Learned the key pattern: summarize locally, analyze with AI
Next: Visualization
Day 4 covers matplotlib — building bar charts, line charts, and heatmaps that communicate the answer before anyone reads a word.
Go to Day 4