Skip to the content.

Ever wanted to interact with your pandas data frame using natural language instead of specific syntax? You’re not alone! Storytelling with data could be vastly streamlined if we could just converse with our notebook, providing context as needed. Now, that’s possible with PandasAI.

PandasAI utilizes the OpenAI API, offering an interface for posing questions in natural language and receiving answers in the form of a pandas data frame.

Usage Instructions

1. Installation

To install the library, use the following pip command:

pip install pandasai

2. Obtain an OpenAI API Key

To use this library, you’ll need an OpenAI API key.

3. Instantiation

Instantiate the OpenAI class with your API key:

from pandasai.llm.openai import OpenAI

llm = OpenAI(api_token=your_API_key)

PandasAI also gives you tools for setting the API key via environment variables or secrets so that you aren’t directly inputing your api credential into raw code.

4. Model Initialization

from pandasai import PandasAI

pandas_ai = PandasAI(llm)

5. Development

The following example, borrowed from the README, demonstrates usage:

import pandas as pd
from pandasai import PandasAI

# Sample DataFrame
df = pd.DataFrame({
    "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
    "gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 1607402389504, 1490967855104, 4380756541440, 14631844184064],
    "happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
})

# Instantiate a LLM
from pandasai.llm.openai import OpenAI
llm = OpenAI(api_token=your_API_key)

pandas_ai = PandasAI(llm)
result = pandas_ai.run(df, prompt='Which are the 5 happiest countries?')
result
6            Canada
7         Australia
1    United Kingdom
3           Germany
0     United States
Name: country, dtype: object

Visualization Example

pandas_ai.run(
    df,
    "Plot the histogram of countries showing for each the gpd, using different colors for each bar",
)

The output of this prompt is the following matplotlib visual:

Open Source Contribution

This is a new and evolving library. If you’re interested in contributing, please follow the guidelines provided here.

Learn More

To find out more about PandasAI, you can check out the following resources:

Back to Home