use chatgpt with pandas in python


Pandas AI is a Python library that enhances Pandas, a widely used tool for data analysis and manipulation, by incorporating generative artificial intelligence capabilities. It is specifically designed to complement Pandas, rather than serving as a substitute for it.

to install the package, you need to have python 3.9 or higher

pip install pandasai

PandasAI is specifically designed to work alongside Pandas, providing conversational abilities to the library. It enables you to inquire about your data and receive responses in the form of Pandas DataFrames. As an illustration, you can utilize PandasAI to identify all rows in a DataFrame where a column’s value surpasses 5, and it will yield a DataFrame solely comprising those rows.

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_TOKEN")

pandas_ai = PandasAI(llm, conversational=False)
pandas_ai(df, prompt='Which are the 5 happiest countries?')

the output should be like this:

6            Canada
7 Australia
1 United Kingdom
3 Germany
0 United States
Name: country, dtype: object

another cool feature is to plot:

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

the result looks like this:
plot from pandas ai


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC