AI Meets Pandas: A First Look at PandasAI
PandasAI wraps a DataFrame so you can ask it questions in plain English and get back a number, a table or a chart. It sends your question and details about the data to a language model and runs the code the model writes. Convenient for quick checks, but weigh that before you use it on sensitive data.
Sample data
The test data is a small DataFrame of ten countries with their GDP and a happiness index. I answer the same three questions twice, first with plain pandas and then with PandasAI, to see whether the AI gets the same results.
import pandas as pd
# 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]
})
df.head()
The pandas way
With pandas, each question is one or two lines. nlargest returns the five happiest countries, nsmallest plus sum adds up the GDP of the two unhappiest, and a bar chart shows GDP per country.
happiest_countries = df.nlargest(5, 'happiness_index')['country'].tolist()
print(f"The 5 happiest countries are: {', '.join(happiest_countries)}")
The 5 happiest countries are: Canada, Australia, United Kingdom, Germany, United States
nlargest is one of the pandas functions I wish I knew earlier; it returns the top rows without sorting the whole DataFrame first.
unhappiest_gdp_sum = df.nsmallest(2, 'happiness_index')['gdp'].sum()
print(f"The sum of the GDPs of the 2 unhappiest countries is: {unhappiest_gdp_sum:,}")
The sum of the GDPs of the 2 unhappiest countries is: 19,012,600,725,504
The chart uses matplotlib with a seaborn color palette, one color per country:
import matplotlib.pyplot as plt
import seaborn as sns
# Create a color palette with as many colors as there are unique countries
colors = sns.color_palette('hsv', len(df['country'].unique()))
df.set_index('country')['gdp'].plot(kind='bar', figsize=(8, 4), color=colors)
plt.title("GDP by Country")
plt.xlabel("Country")
plt.ylabel("GDP")
plt.show()
Ask PandasAI
PandasAI 3 needs two packages, a model and a wrapped DataFrame. You set the model once with pai.config.set, turn the pandas DataFrame into a PandasAI one with pai.DataFrame, and ask with chat. The README lists Python 3.8 to 3.11 as supported.
pip install pandasai
pip install pandasai-litellm
The model connection goes through LiteLLM, so OpenAI and other providers use the same setup. An OpenAI API key is a paid account, separate from ChatGPT Plus.
import pandasai as pai
from pandasai_litellm.litellm import LiteLLM
llm = LiteLLM(model="gpt-4.1-mini", api_key="YOUR_OPENAI_API_KEY")
pai.config.set({"llm": llm})
sdf = pai.DataFrame(df)
print(sdf.chat("Which are the 5 happiest countries?"))
print(sdf.chat("What is the sum of the GDPs of the 2 unhappiest countries?"))
The video uses the older 1.x API, with a PandasAI object and its run method, which no longer exists. With that version, PandasAI returned the same five countries and the same GDP sum, 19012600725504, as the pandas code.
Charts work the same way. I asked for a histogram, and what PandasAI drew was a bar chart of GDP per country, like the matplotlib version above:
sdf.chat("Plot the histogram of countries showing for each the gdp, using different colors for each bar")
Privacy and cost
PandasAI does not answer by itself. It sends your question and information about the DataFrame to the model, receives Python code back and runs that code on your machine. For company data, find out exactly what leaves your computer before you start.
The version in the video sent a randomized head of the DataFrame and had an enforce_privacy option that shared only the column names. The current README no longer has that section; it offers a Docker sandbox for running the generated code instead, so check the current documentation for what is sent.

Every question is also a paid API call, billed by the model provider.
My take
I still prefer writing the pandas code myself, because I can see exactly what happens to the data. PandasAI is useful for a quick second opinion on a result, or for people who do not write pandas yet, as long as they check the answer.
Sketch is a similar library that answers questions about a DataFrame, and I compare the chat approach in how I use ChatGPT as a data analyst.
