A Complete Information to PandasAI




Generative AI and Massive Language Fashions (LLMs) have introduced a brand new period to Synthetic Intelligence and Machine Studying. These massive language fashions are being utilized in varied functions throughout completely different domains and have opened up new views on AI. These fashions are educated on an enormous quantity of textual content information from all around the web and may generate textual content in a human-like method. Essentially the most well-known instance of an LLM is ChatGPT, developed by OpenAI. It will possibly carry out varied duties, from creating unique content material to writing code. On this article, we’ll look into one such utility of LLMs: the PandasAI library. Information to PandasAI will be thought-about a fusion between Python’s well-liked Pandas library and OpenAI’s GPT. This can be very highly effective for getting fast insights from information with out writing a lot code.

Studying Aims

  • Understanding the variations between Pandas and PandasAI
  • PandasAI and its Position in information evaluation and Visualization
  • Utilizing PandasAI to construct a full exploratory information evaluation workflow
  • Understanding the significance of writing clear, concise, and particular prompts
  • Understanding the restrictions of PandasAI

This text was revealed as part of the Information Science Blogathon.


PandasAI is a brand new instrument for making information evaluation and visualization duties simpler. PandasAI is constructed with Python’s Pandas library and makes use of Generative AI and LLMs in its work. In contrast to Pandas, wherein it’s important to analyze and manipulate information manually, PandasAI lets you generate insights from information by merely offering a textual content immediate. It’s like giving directions to your assistant, who’s expert and proficient and may do the be just right for you rapidly. The one distinction is that it isn’t a human however a machine that may perceive and course of info like a human.

On this article, I’ll overview the complete information evaluation and visualization course of utilizing PandasAI with code examples and explanations. So, let’s get began.

Arrange an OpenAI Account and Extract the API Key

To make use of the PandasAI library, it’s essential to create an OpenAI account (should you don’t have already got one) and use your API key. It may be accomplished as follows:

  1. Go to https://platform.openai.com and create a private account.
  2. Check in to your account.
  3. Click on on Private on the highest proper aspect.
  4. Choose View API keys from the dropdown.
  5. Create a brand new secret key.
  6. Copy and retailer the key key to a secure location in your laptop.

When you’ve got adopted the above-given steps, you might be all set to leverage the ability of Generative AI in your initiatives.

Putting in PandasAI

Write the command under in a Jupyter Pocket book/ Google colab or a terminal to put in the Pandasai package deal in your laptop.

pip set up pandasai

Set up will take a while, however as soon as put in, you’ll be able to immediately import it right into a Python surroundings.

from pandasai import PandasAI 

It will import PandasAI to your coding surroundings. We’re prepared to make use of it, however let’s first get the info.

Getting the Information and Instantiating an LLM

You need to use any tabular information of your liking. I shall be utilizing the medical fees information for this tutorial. (Observe: PandasAI can solely analyze tabular and structured information, like common pandas, not unstructured information, similar to photos).

The info seems like this.

Now with the info in place, we’ll want our Open AI API key to instantiate a Massive Language Mannequin. To do that, kind within the below-given code:

# Use your API key to instantiate an LLM
from pandasai.llm.openai import OpenAI
llm = OpenAI(api_token=f"{YOUR_API_KEY}")
pandas_ai = PandasAI(llm)

Simply enter your secret key created above instead of the YOUR_API_KEY placeholder within the above code, and you’ll be all good to go. Now we will analyze our information and discover some key insights utilizing PandasAI.

Analyzing Information with PandasAI

PandasAI primarily takes 2 parameters as enter, first the dataset and second a immediate which is the question or query requested. You could be questioning the way it works below the hood. So, let me clarify a bit.

Executing your immediate utilizing PandasAI sends a request to the OpenAI server on which the LLM is hosted. The LLM processes the request, converts the question into applicable Python code, after which makes use of pandas to calculate the reply. It returns the reply to PandasAI, then outputs it to your display screen.


Let’s begin with one of the crucial primary questions!

Query: What’s the dimension of the dataset?

immediate = "What's the dimension of the dataset?"
pandas_ai(information, immediate=immediate)

'1338 7'

It’s at all times greatest to verify the correctness of the AI’s solutions to make sure it understands our query accurately. I’ll use Panda’s library, which you have to be aware of, to validate its solutions. Let’s see if the above reply is right or not.

import pandas as pd

(1338, 7)


The output matches PandasAI’s reply, and we’re off to begin. PandasAI can be in a position to impute lacking values within the information. The info doesn’t comprise any lacking values, however I intentionally modified the primary worth for the costs column to null. Let’s see if it might detect the lacking worth and the column it belongs to.

immediate=""'What number of null values are within the information.
            Are you able to additionally inform which column incorporates the lacking worth'''
pandas_ai(information, immediate=immediate)

'1 fees'

This outputs ‘1 cost’, which tells that there’s 1 lacking worth within the fees column, which is completely right. Now let’s strive imputing the lacking worth.

immediate=""'Impute the lacking worth within the information utilizing the imply worth. 
            Output the imputed worth rounded to 2 decimal digits.'''
pandas_ai(information, immediate=immediate)


It imputes the lacking worth within the information and outputs 13267.72. Now the primary row seems like this.

 Source: AuthorNaNSource: Author</figcaption> </figure> <p> Let's check this using pandas.</p> <pre><code># Checking mean values of charges excluding the first value data['charges'].iloc[1:].mean() Output: 132667.718823</code></pre> <p>This too outputs the same value. This is some incredible stuff. You can just talk to the AI and it can solve your queries in just a matter of seconds. And this is just one of many things <pre><code>prompt=
Age Common  BMI06432.97613615232.93603425832.71820036132.54826146232.342609.
Typically, BMI values higher than 30 falls within the vary of the overweight class. Subsequently, the info exhibits that individuals of their 50s and 60s usually tend to be overweight than different age teams.

Query: Which area has the best variety of people who smoke?

immediate=""'Which area has the best variety of people who smoke and which has the bottom?
            Embrace the values of each the best and lowest numbers within the reply.
            Present the reply in type of a sentence.'''
pandas_ai(information, immediate=immediate)

'The area with the best variety of people who smoke is southeast with 91 people who smoke.'
'The area with the bottom variety of people who smoke is southwest with 58 people who smoke.'

Let’s enhance the problem a bit and ask a tough query.

Query: What are the common fees of a feminine dwelling within the north?

The area column incorporates 4 areas: northeast, northwest, southeast, and southwest. So, the north ought to comprise each northeast and northwest areas. However can the LLM have the ability to perceive this delicate however essential element? Let’s discover out!

immediate=""'What are the common fees of a feminine dwelling within the north area?
            Present the reply in type of a sentence to 2 decimal locations.'''
pandas_ai(information, immediate=immediate)

The common fees of a feminine dwelling within the north area are $12479.87

Let’s verify the reply manually utilizing pandas.

north_data = information[(data['sex'] == 'feminine') & 
                 ((information['region'] == 'northeast') |
                  (information['region'] == 'northwest'))]


The above code outputs a special reply (which is the right reply) than the LLM gave. On this case, the LLM wasn’t in a position to carry out properly. We will be extra particular and inform the LLM what we imply by the north area and see if it can provide the right reply.

immediate=""'What are the common fees of a feminine dwelling within the north area?
            The north area consists of each the northeast and northwest areas.
            Present the reply in type of a sentence to 2 decimal locations.'''
pandas_ai(information, immediate=immediate)

The common fees of a feminine dwelling within the north area are $12714.35

This time it offers the right reply. As this was a tough query, we have to be extra cautious about our prompts and embrace related particulars, because the LLM may overlook these delicate variations. Subsequently, you’ll be able to see that we will’t belief the LLM blindly as it might generate incorrect responses generally because of incomplete prompts or another limitations, which I’ll focus on later within the tutorial.

Visualizing Information with PandasAI

To this point, we’ve seen the proficiency of PandasAI in analyzing information; now, let’s take a look at it to plot some graphs and see how good it might do in visualizing information.

Correlation Heatmap

Let’s create a correlation heatmap of the numeric columns.

immediate = "Make a heatmap exhibiting the correlation of all of the numeric columns within the information"
pandas_ai(information, immediate=immediate)
That appears nice. Underneath the hood, PandasAI makes use of Python’s Seaborn and matplotlib libraries to plot information. Let’s create some extra graphs.

Distribution of BMI utilizing histogram

immediate = immediate = "Create a histogram of bmi with a kernel density plot."
pandas_ai(information, immediate=immediate)
The distribution of BMI values considerably resembles the traditional distribution plot with a imply worth close to 30.

Distribution of fees utilizing boxplot

immediate = "Make a boxplot of fees. Output the median worth of fees."
pandas_ai(information, immediate=immediate)

The median worth of the costs column is roughly 9382. Within the plot, that is depicted by the orange line in the midst of the field. It may be clearly seen that the costs column incorporates many outlier values, that are proven by the circles within the above plot.

Now let’s create some plots exhibiting the connection between a couple of column.

Area vs. Smoker

immediate = "Make a horizontal bar chart of area vs smoker. Make the legend smaller."
pandas_ai(information, immediate=immediate)

From the graph, one can simply inform that the southeast area has the best variety of people who smoke in comparison with different areas.

Variation of fees with age

immediate=""'Make a scatterplot of age with fees and colorcode utilizing the smoker values. 
            Additionally present the legends.'''
pandas_ai(information, immediate=immediate)

Appears like age and fees comply with a linear relationship for non-smokers, whereas no particular sample exists for people who smoke.

Variation of fees with BMI

To make issues slightly extra advanced, let’s strive making a plot utilizing solely a proportion of the info as a substitute of the true information and see how the LLM can carry out.

immediate = "Make a scatterplot of bmi with fees and colorcode utilizing the smoker values. 
          Add legends and use solely information of people that have lower than 2 kids."
pandas_ai(information, immediate=immediate)
It did an excellent job making a plot, even with a fancy query. PandasAI has now unveiled its true potential. You’ve witnessed the true energy of Massive Language Fashions.


  • The responses generated by PandasAI can generally exhibit inherent biases as a result of huge quantity of information LLMs are educated on from the web, which might hinder the evaluation. To make sure truthful and unbiased outcomes, it’s important to grasp and mitigate such biases.
  • LLMs can generally misread ambiguous or contextually advanced queries, resulting in inaccurate or sudden outcomes. One should train warning and double-check the solutions earlier than making any essential data-driven resolution.
  • It will possibly generally be gradual to come back to a solution or fully fail. The server hosts the LLMs, and sometimes, technical points could stop the request from reaching the server or being processed.
  • It can’t be used for giant information evaluation duties as it isn’t computationally environment friendly when coping with massive quantities of information and requires high-performance GPUs or computational assets.


We’ve got seen the complete walkthrough of a real-world information evaluation process utilizing the outstanding energy of the PandasAI library. When coping with GPT or different LLMs, one can not overstate the ability of writing immediate.

Listed below are some key takeaways from this text:

  • PandasAI is a Python library that provides Generative AI capabilities to Pandas, clubbing it with massive language fashions.
  • PandasAI makes Pandas conversational by permitting us to ask questions in pure language utilizing textual content prompts.
  • Regardless of its wonderful capabilities, PandasAI has its limitations. Don’t blindly belief or use for classy use circumstances like massive information evaluation.

Thanks for sticking to the tip. I hope you discovered this text useful and can begin utilizing PandasAI on your initiatives.

Continuously Requested Questions (FAQs)

Q1. Is PandasAI a alternative for pandas?

A. No, PandasAI shouldn’t be a alternative for pandas. It enhances pandas utilizing Generative AI capabilities and is made to enhance pandas, not change them.

Q2. For what functions can PandasAI be used?

A. Use PandasAI for information exploration and evaluation and your initiatives below the permissive MIT license. Don’t use it for manufacturing functions.

Q3. Which LLMs do PandasAI assist?

A. It helps a number of Massive Language Fashions (LLMs) similar to OpenAI, HuggingFace, and Google PaLM. You’ll find the complete checklist right here.

This fall. How is it completely different from pandas?

A. In pandas, it’s important to write the complete code manually to carry out information evaluation whereas PandasAI makes use of textual content prompts and pure language to carry out information evaluation with out the necessity to write code.

Q5. Does PandasAI at all times give the right reply?

A. No, it might often output improper or incomplete solutions because of ambiguous prompts offered by the consumer or because of some bias within the information.

The media proven on this article shouldn’t be owned by Analytics Vidhya and is used on the Creator’s discretion.