Skip to content
Search

PandasAI

lastmod 2024-12-03created 2024-05-1411 edits

Chat with your database (SQL, CSV, pandas, polars, mongodb, noSQL, etc). PandasAI makes data analysis conversational using LLMs (GPT 3.5 / 4, Anthropic, VertexAI) and RAG.

Pandas AI is a Python library that integrates generative artificial intelligence capabilities into Pandas, making dataframes conversational

Brings generative AI to Pandas

Install

requirements.txt file:

pandasai==2.4.0
seaborn==0.13.2
PyYAML==6.0.2

If a Numpy version problem occurs, you need to keep Numpy at version 1.xx.

Use a constraints.txt file:

numpy=1.26.4

OpenAI example

Using PandasAI

WARNING

This is an old version. For the latest approach, use #SmartDataframe.

import pandas as pd
import numpy as np
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI

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]
})

OPENAI_API_KEY = "여러분들이 생성한 OpenAI API Key"
llm = OpenAI(api_token=OPENAI_API_KEY)

pandas_ai = PandasAI(llm, verbose=True)
pandas_ai.run(df, prompt='Which are the 5 happiest countries?')

Using SmartDataframe

from pandasai import SmartDataframe
from pandasai.llm import OpenAI

llm = OpenAI()  # no need to pass the API key, it will be read from the environment variable
pandas_ai = SmartDataframe("data.csv", config={"llm": llm})

SmartDataframe

The SmartDataframe class is the main class of pandasai. It is used to interact with a single dataframe. Below is simple example to get started with pandasai.

import os
import pandas as pd
from pandasai import SmartDataframe

# Sample DataFrame
sales_by_country = pd.DataFrame({
    "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
    "sales": [5000, 3200, 2900, 4100, 2300, 2100, 2500, 2600, 4500, 7000]
})

# By default, unless you choose a different LLM, it will use BambooLLM.
# You can get your free API key signing up at https://pandabi.ai (you can also configure it in your .env file)
os.environ["PANDASAI_API_KEY"] = "YOUR_API_KEY"

df = SmartDataframe(sales_by_country)
df.chat('Which are the top 5 countries by sales?')
# Output: China, United States, Japan, Germany, Australia

Large language models (LLMs)

PandasAI supports several LLMs (large language models). LLMs are used to generate code from natural language queries. The generated code is then executed to produce the result.

LangChain models

PandasAI has also built-in support for LangChain models.

In order to use LangChain models, you need to install the langchain package:

pip install pandasai[langchain]

Once you have installed the langchain package, you can use it to instantiate a LangChain object:

from pandasai import SmartDataframe
from langchain_openai import OpenAI

langchain_llm = OpenAI(openai_api_key="my-openai-api-key")
df = SmartDataframe("data.csv", config={"llm": langchain_llm})

PandasAI automatically detects that you are using a LangChain LLM and converts it into a PandasAI LLM.

See also

Favorite site

author Zer0 sitemap