Course Overview All Courses Blog Reserve Bootcamp Seat
Data Analysis · Day 3 of 5 ~75 minutes

AI-Powered Analysis — Use Claude to Find Insights

Connect the Claude API to your data and get natural-language insights automatically. Turn raw numbers into executive narratives.

Day 1
Day 2
3
Day 3
4
Day 4
5
Day 5
What You'll Build Today

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.

1
Setup

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:

bash
pip install anthropic

Store your key as an environment variable — never hard-code it in your scripts:

bashMac/Linux
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
bashWindows PowerShell
$env:ANTHROPIC_API_KEY="sk-ant-your-key-here"
2
First API Call

Ask Claude about your data

pythonai_insights.py
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.

3
Batch Analysis

Analyze multiple segments automatically

pythonbatch_insights.py
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
Day 3 Done

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
Finished this lesson?