DEV Community

shashank ms
shashank ms

Posted on

Using LLM for Data Analysis: A Beginner's Guide

We are going to build a command-line data analyst that ingests any CSV file, describes its structure, and answers questions by generating Python code you can run immediately. We will power it with Oxlo.ai so that sending a large schema preview or a long question costs one flat request fee instead of scaling by tokens. If you are just getting started with data analysis, this gives you a reproducible way to explore files with plain English questions.

What you'll need

You will need Python 3.10 or newer, the OpenAI SDK, and pandas. You will also need an Oxlo.ai API key from the portal at https://portal.oxlo.ai. Oxlo.ai uses flat per-request pricing, so sending a large schema preview or a long question does not increase the cost the way token-based providers do. That makes it a practical choice for iterative data exploration where prompts naturally grow. Install the dependencies:

pip install openai pandas matplotlib

Step 1: Instantiate the client and test the connection

Start by creating an OpenAI client pointed at Oxlo.ai. We will send a tiny test prompt to confirm the key and network path are working before we touch any files.

from openai import OpenAI

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=[
        {"role": "user", "content": "Say 'Connection OK' and nothing else."},
    ],
)

print(response.choices[0].message.content)

Step 2: Load a CSV and summarize its schema

We do not send the entire file to the model. Instead, we read it with pandas and build a text summary that includes column names, data types, row count, and a short preview. This keeps the prompt focused and respects context limits.

import pandas as pd

def summarize_csv(path: str, preview_rows: int = 5) -> str:
    df = pd.read_csv(path)
    lines = [
        f"File: {path}",
        f"Shape: {df.shape[0]} rows, {df.shape[1]} columns",
        f"Columns: {list(df.columns)}",
        "Dtypes:",
        df.dtypes.to_string(),
        "Preview:",
        df.head(preview_rows).to_string(index=False),
    ]
    return "\n".join(lines)

context = summarize_csv("sales.csv")
print(context)

Step 3: Define the analyst system prompt

The system prompt is the only part of the agent you need to tweak to change its behavior. It instructs the model to answer directly when possible, but to output Python code inside a fenced markdown block whenever a calculation, transformation, or visualization is required.

SYSTEM_PROMPT = """You are a concise data analyst. Answer questions clearly.
If the user asks for a calculation, chart, table transformation, or statistical test, write a complete, self-contained Python script using pandas and matplotlib.
Place the code inside a single markdown block like this:



```python
# code here
```



After the code block, add one sentence explaining what the code does.
If no code is needed, just answer in plain text."""

Step 4: Send the schema and a question

Now we combine the system prompt, the CSV summary, and the user question into one chat completion. We use llama-3.3-70b because it handles tool-free instruction following reliably, but you can swap in qwen-3-32b or kimi-k2.6 if you prefer stronger reasoning.

question = "What is the average revenue by region?"

messages = [
    {"role": "system", "content": SYSTEM_PROMPT},
    {"role": "user", "content": f"Dataset context:\n{context}\n\nQuestion: {question}"},
]

response = client.chat.completions.create(
    model="llama-3.3-70b",
    messages=messages,
)

reply = response.choices[0].message.content
print(reply)

Step 5: Extract and execute generated Python code

When the model returns a Python block, we want to run it automatically against the original dataframe. The script below parses the markdown, injects the dataframe and common libraries into a restricted namespace, and executes the code with exec. This keeps the workflow in one terminal window.

import re
import pandas as pd
import matplotlib.pyplot as plt

def extract_python_code(text: str) -> str | None:
    match = re.search(r"

```python\n(.*?)```

", text, re.DOTALL)
    return match.group(1).strip() if match else None

def run_code(code: str, df: pd.DataFrame):
    namespace = {"pd": pd, "plt": plt, "df": df}
    exec(code, namespace)

df = pd.read_csv("sales.csv")
code = extract_python_code(reply)

if code:
    print("Running generated code...")
    run_code(code, df)
else:
    print("No code found; the model answered directly.")

Step 6: Assemble the complete CLI script

Here is the finished script. Save it as csv_analyst.py. It takes two arguments: the path to a CSV and a question string. It prints the model's explanation, and if code is generated, it runs it immediately.

import argparse
import re
import pandas as pd
import matplotlib.pyplot as plt
from openai import OpenAI

SYSTEM_PROMPT = """You are a concise data analyst. Answer questions clearly.
If the user asks for a calculation, chart, table transformation, or statistical test, write a complete, self-contained Python script using pandas and matplotlib.
Place the code inside a single markdown block like this:



```python
# code here
```



After the code block, add one sentence explaining what the code does.
If no code is needed, just answer in plain text."""

def summarize_csv(path: str, preview_rows: int = 5) -> str:
    df = pd.read_csv(path)
    lines = [
        f"File: {path}",
        f"Shape: {df.shape[0]} rows, {df.shape[1]} columns",
        f"Columns: {list(df.columns)}",
        "Dtypes:",
        df.dtypes.to_string(),
        "Preview:",
        df.head(preview_rows).to_string(index=False),
    ]
    return "\n".join(lines)

def extract_python_code(text: str) -> str | None:
    match = re.search(r"

```python\n(.*?)```

", text, re.DOTALL)
    return match.group(1).strip() if match else None

def run_code(code: str, df: pd.DataFrame):
    namespace = {"pd": pd, "plt": plt, "df": df}
    exec(code, namespace)

def main():
    parser = argparse.ArgumentParser(description="CLI CSV Analyst via Oxlo.ai")
    parser.add_argument("csv", help="Path to CSV file")
    parser.add_argument("question", help="Question to ask about the data")
    args = parser.parse_args()

    client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

    context = summarize_csv(args.csv)
    df = pd.read_csv(args.csv)

    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": f"Dataset context:\n{context}\n\nQuestion: {args.question}"},
    ]

    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=messages,
    )

    reply = response.choices[0].message.content
    print("\n--- Analysis ---\n")
    print(reply)

    code = extract_python_code(reply)
    if code:
        print("\n--- Running generated code ---\n")
        run_code(code, df)

if __name__ == "__main__":
    main()

Run it

Create a small sample file named sales.csv:

region,month,revenue
North,Jan,12000
North,Feb,15000
South,Jan,9000
South,Feb,11000
East,Jan,13000
East,Feb,14000

Now ask for a grouped calculation:

python csv_analyst.py sales.csv "Calculate average revenue per region and print the result"

Example output:

--- Analysis ---



```python
avg_revenue = df.groupby('region')['revenue'].mean()
print(avg_revenue)
```



This groups the dataframe by region and computes the mean revenue for each.

--- Running generated code ---

region
East    13500.0
North   13500.0
South   10000.0
Name: revenue, dtype: float64

For a visualization, try:

python csv_analyst.py sales.csv "Plot revenue by region as a bar chart and save it to chart.png"

The script will execute the generated matplotlib code and write chart.png to your working directory.

Next steps

Add a conversation loop that appends each exchange to the messages list so you can ask follow-up questions without resending the full schema every time. If you start processing wide datasets with dozens of columns, consider switching to Oxlo.ai models like deepseek-v3.2 or kimi-k2.6, which handle long contexts efficiently, and remember that Oxlo.ai charges per request rather than per token, so expanding your schema summary does not inflate your bill. You can compare plans at https://oxlo.ai/pricing.

Top comments (0)