PandasAI – Generative AI Capabilities to Pandas Dataframes

Posted by

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

To Work With PandasAI we need to install pandasai library,

pip install pandasai

Make sure you have Python already installed in your PC.

Then you need to create openai account from https://openai.com/ and generate a new API Key from there.

while using below code you can replace that Key with YOUR API KEY and then you can have capabilities of asking questions to the dataframe with the help of pandasai library.

Sample Code


import pandas as pd
from pandasai import PandasAI

#Create testing  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, conversational=False)

#Ask for Top 5 happiest countries
pandas_ai(df, prompt='Which are the 5 happiest countries?')

#Ask for Generate Histogram
pandas_ai(df,
    "Plot the histogram of countries showing for each the gpd, using different colors for each bar",)

It also has command line tool Pai.

Pai is the command line tool designed to provide a convenient way to interact with PandasAI through a command line interface (CLI).

pai -d "~/pandasai/test.csv" -t "OPENAPI TOKEN" -m "openai" -p "Describe columns in dataframe"

Options:

  • -d, –dataset: The file path to the dataset.
  • -t, –token: Your HuggingFace or OpenAI API token, if no token provided pai will pull from the .env file.
  • -m, –model: Choice of LLM, either openaiopen-assistant, or starcoder.
  • -p, –prompt: Prompt that PandasAI will run.

That’s all About this I hope you understood the concept of PandasAI library.

See you Next time 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *