Query Pandas DataFrames with Natural Language
LlamaIndex loader that answers natural-language questions over a pandas DataFrame.
Why it matters
Unlock insights from your Pandas DataFrames by asking questions in natural language. This asset leverages LLMs to interpret your queries and extract relevant information, acting as a bridge between human language and data analysis.
Outcomes
What it gets done
Translate natural language questions into executable Pandas operations.
Extract specific data points or summaries from DataFrames based on user queries.
Integrate with LLMs (like OpenAI) to power natural language understanding.
Load query results directly into LlamaIndex Document objects for further processing.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-reader-readers-pandas-ai | bash Overview
Pandas AI Loader
The Pandas AI Loader wraps the PandasAI package to answer natural-language questions about a pandas DataFrame, either returning a raw answer via run_pandas_ai or loading the result as Document objects via load_data. Use it when you need natural-language question answering over a pandas DataFrame. It requires an LLM (the example uses OpenAI) to drive the query.
What it does
The Pandas AI Loader is a light wrapper around the PandasAI Python package, letting you query a pandas DataFrame in natural language. You can either get the raw result of pandasai.run directly via run_pandas_ai, or load the result as LlamaIndex Document objects via load_data.
When to use - and when NOT to
Use it when you want to ask natural-language questions about a pandas DataFrame -- for example "Which are the 5 happiest countries?" against a DataFrame of countries, GDP, and happiness index -- and either get a direct answer or load it as a document. It requires an LLM (the example uses pandasai.llm.openai.OpenAI) to drive the query, so it is not usable without one configured.
Inputs and outputs
Install with:
pip install llama-index-readers-pandas-ai
Given a DataFrame and an LLM, query it directly or load the result as a document:
from pandasai.llm.openai import OpenAI
import pandas as pd
df = pd.DataFrame(
{
"country": [
"United States",
"United Kingdom",
"France",
"Germany",
"Italy",
"Spain",
"Canada",
"Australia",
"Japan",
"China",
],
"gdp": [
21400000,
2940000,
2830000,
3870000,
2160000,
1350000,
1780000,
1320000,
516000,
14000000,
],
"happiness_index": [7.3, 7.2, 6.5, 7.0, 6.0, 6.3, 7.3, 7.3, 5.9, 5.0],
}
)
llm = OpenAI()
from llama_index.readers.pandas_ai import PandasAIReader
loader = PandasAIReader(llm=llm)
response = reader.run_pandas_ai(
df, "Which are the 5 happiest countries?", is_conversational_answer=False
)
print(response)
docs = reader.load_data(
df, "Which are the 5 happiest countries?", is_conversational_answer=True
)
run_pandas_ai returns the raw PandasAI result directly; is_conversational_answer=False gives parsed output rather than a conversational one. load_data returns the result as Document objects instead, using PandasCSVReader under the hood, and also accepts is_conversational_answer to control the answer style.
Who it's for
Developers building LlamaIndex pipelines that need natural-language question answering over a pandas DataFrame, either as a direct answer or as a loadable document.
Source README
Pandas AI Loader
pip install llama-index-readers-pandas-ai
This loader is a light wrapper around the PandasAI Python package.
See here: https://github.com/gventuri/pandas-ai.
You can directly get the result of pandasai.run command, or
you can choose to load in Document objects via load_data.
Usage
from pandasai.llm.openai import OpenAI
import pandas as pd
### Sample DataFrame
df = pd.DataFrame(
{
"country": [
"United States",
"United Kingdom",
"France",
"Germany",
"Italy",
"Spain",
"Canada",
"Australia",
"Japan",
"China",
],
"gdp": [
21400000,
2940000,
2830000,
3870000,
2160000,
1350000,
1780000,
1320000,
516000,
14000000,
],
"happiness_index": [7.3, 7.2, 6.5, 7.0, 6.0, 6.3, 7.3, 7.3, 5.9, 5.0],
}
)
llm = OpenAI()
from llama_index.readers.pandas_ai import PandasAIReader
### use run_pandas_ai directly
### set is_conversational_answer=False to get parsed output
loader = PandasAIReader(llm=llm)
response = reader.run_pandas_ai(
df, "Which are the 5 happiest countries?", is_conversational_answer=False
)
print(response)
### load data with is_conversational_answer=False
### will use our PandasCSVReader under the hood
docs = reader.load_data(
df, "Which are the 5 happiest countries?", is_conversational_answer=False
)
### load data with is_conversational_answer=True
### will use our PandasCSVReader under the hood
docs = reader.load_data(
df, "Which are the 5 happiest countries?", is_conversational_answer=True
)
This loader is designed to be used as a way to load data into LlamaIndex.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.