PandasAI: Chat with Your DataFrames in Python
What it is: PandasAI is a Python library that lets you ask questions about DataFrames in plain English — df.chat("Which product had the highest revenue?") — by having an LLM generate pandas code, executing it, and returning the answer as a number, table, or chart.
Quick answer: PandasAI adds a .chat() method to your DataFrames: you ask a question in natural language, an LLM writes pandas code against your DataFrame's schema, PandasAI executes it and returns the result. Install with pip install pandasai, bring an OpenAI, Anthropic, or local model key. It excels at quick exploratory questions; verify the generated code before trusting numbers in anything important.
How does PandasAI generate and run pandas code?
When you call .chat(), PandasAI sends the LLM your question plus DataFrame metadata — column names, dtypes, and a few sample rows, not the full dataset. The model returns Python/pandas code, which PandasAI executes locally against the real DataFrame and formats into an answer, plot, or new DataFrame. Failed code can be retried with the error message, so simple mistakes self-correct.
pip install pandasai
import pandasai as pai
from pandasai_openai import OpenAI
pai.config.set({"llm": OpenAI(api_token="sk-...")})
df = pai.read_csv("sales.csv")
df.chat("Top 5 customers by total revenue this year")
df.chat("Plot monthly revenue as a bar chart")
API details shift between major versions (v2 used SmartDataframe; v3 moved to the semantic-layer style above), so match examples to the version you install.
Is it safe to let an LLM execute code on my data?
Treat generated code as untrusted. PandasAI applies its own sanitization and restricted execution, but running LLM-written Python in your main process is inherently risky — especially with prompt-injection-shaped data or user-facing apps. The project ships a Docker sandbox extension (pip install pandasai-docker) that executes generated code in a container; use it for anything beyond your own laptop, and never point PandasAI at credentials-bearing environments casually.
Which LLMs does PandasAI work with?
Multiple providers via extension packages: OpenAI, Anthropic, Google, Azure, and local models through Ollama or LiteLLM-style integrations. Bigger models noticeably improve code quality on multi-step questions. Since only schema and sample rows go to the provider, cost per question is small — but note that sample rows do leave your machine unless you run a local model.
When does PandasAI beat writing pandas by hand?
It wins for exploratory questions where thinking up the groupby chain takes longer than asking — quick aggregations, "which rows look weird", one-off charts, or enabling less-technical colleagues to query a DataFrame. Writing pandas by hand wins for anything repeated, tested, or production-bound: real pipelines need deterministic, reviewable code. If you're still learning the underlying idioms, start with fundamentals like loc vs iloc and groupby so you can audit what the LLM writes.
How accurate is it, honestly?
Good on straightforward aggregations over clean, well-named columns; shakier on ambiguous questions, messy column names, dates, and multi-step logic — the same places junior analysts stumble. Expect roughly "competent first draft" quality: most simple questions come back right, but silent misinterpretation (wrong filter, wrong join key) does happen. Always display or log the generated code and spot-check totals against a known number before trusting results. Two cheap accuracy boosts: rename cryptic columns before chatting (rev_amt_usd → revenue_usd), and add column descriptions where your PandasAI version supports them — the model can only be as right as the schema it sees.
Author & links
Author: Sinaptik AI (Gabriele Venturi)
Repo: github.com/sinaptik-ai/pandas-ai
License: open-source core with a custom license for some server components — check the repo
Related skills
For natural-language questions against a SQL warehouse instead of DataFrames, see Vanna AI.
← Back to Data Agents