Introduction

    PandasAI, a Python library, extends the functionality of pandas, a widely used data analysis and manipulation tool, by introducing Generative AI capabilities. With PandasAI, pandas (as well as other popular data analysis libraries) become conversational, enabling you to interact with your data using natural language. For instance, you can instruct PandasAI to identify all rows in a DataFrame where a specific column’s value exceeds 5, and it will provide you with a DataFrame containing only those relevant rows. Moreover, PandasAI can also assist you in tasks such as creating graphs, data cleansing, handling missing values, and generating new features.

    Setup

    First, you’ll want to install the most recent version of PandasAI. Additionally, you’ll need to import SmartDataframe, a specialized kind of DataFrame that inherits all the attributes and functions of the standard pd.DataFrame while incorporating conversational capabilities.

    Importing from a Pandas DataFrame

    To import data from a pandas DataFrame, you’ll need to import the necessary pandas libraries and create a DataFrame instance. Here’s how you can do it:

    Large Language Model (LLM)

    Since PandasAI is powered by an LLM, you should import the LLM you’d like to use for your use case. Here, we will be using OpenAI, and you would need to import the relevant libraries and obtain an API token for OpenAI. Here are the steps to get an API token:

    1. Go to the OpenAI website: https://openai.com/api/
    2. Sign up using your email address or connect your Google Account.
    3. Once you’re logged in, navigate to “View API Keys” on the left side of your Personal Account Settings.
    4. Select “Create new Secret key” to generate a new API token.

    After obtaining your API token, you can use it to authenticate and access the PandasAI library’s features powered by OpenAI’s language model.

    Querying the Data

    Example 1

    Identify the top 5 countries based on their GDP.

    Example 2

    The sum of the GDP of the 2 unhappiest countries.

    Example 3

    Plot a chart of the GDP by Country.

    Example 4

    Plat a chart of the Happiness Index by country, and each country must have different colours