PandasAI

3.0.0 · active · verified Thu Apr 16

PandasAI is a Python library that enhances data analysis by integrating Large Language Models (LLMs) with pandas DataFrames. It allows users to interact with their data using natural language prompts, supporting various data sources like SQL, CSV, and Excel. Currently at version 3.0.0, the library is actively developed with a frequent release cadence, often introducing alpha and beta versions before stable releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize PandasAI v3 with LiteLLM for conversational data analysis on a pandas DataFrame. It includes setting up the LLM globally and using the `pai.DataFrame` wrapper to query data in natural language.

import os
import pandas as pd
import pandasai as pai
from pandasai_litellm.litellm import LiteLLM

# Set your API key from environment variable
openai_api_key = os.environ.get('OPENAI_API_KEY', 'YOUR_OPENAI_API_KEY')

# Initialize LiteLLM with your desired model
# Ensure the model name is correct and supported by your LiteLLM setup/API key
llm = LiteLLM(model="gpt-4o-mini", api_key=openai_api_key)

# Configure PandasAI globally with the LLM
pai.config.set({"llm": llm})

# Sample DataFrame
data = {
    "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain"],
    "gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360],
    "happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4]
}
df = pd.DataFrame(data)

# Convert pandas DataFrame to PandasAI DataFrame
pai_df = pai.DataFrame(df)

# Chat with your data
response = pai_df.chat("Which are the top 3 countries by GDP?")
print(response)

view raw JSON →