Using Pandas AI with OpenAI for Conversational Data Analysis in Python
This article introduces Pandas AI, shows how to install it, connect to databases, run natural‑language queries and generate visualizations using OpenAI models, and explains how combining ChatGPT with Pandas transforms data interaction for Python developers and analysts.
Python Pandas is an open‑source library that provides data manipulation and analysis capabilities for Python, widely used by data scientists to manage structured data such as Series and DataFrame.
In AI workflows, Pandas is often employed for preprocessing steps, cleaning, reshaping, merging and aggregating data before feeding it to machine‑learning or deep‑learning models.
To use Pandas AI, first install it with pip install pandasai. Then import the library and an OpenAI LLM, create a DataFrame, and run natural‑language prompts such as “Which are the 5 happiest countries?” to obtain results.
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]
})
from pandasai.llm.openai import OpenAI
llm = OpenAI(api_token="your_API_key")
pandas_ai = PandasAI(llm)
pandas_ai.run(df, prompt='Which are the 5 happiest countries?')To query a relational database, create a PostgreSQL URI, read the SQL result into a DataFrame with pd.read_sql, and then interact with the data using Pandas AI prompts.
# creating the uri and connecting to database
pg_conn = "postgresql://YOUR URI HERE"
# Query sql database
query = """
SELECT *
FROM table_name
"""
# Create dataframe named df
df = pd.read_sql(query, pg_conn)More complex queries are possible, such as asking for the sum of GDPs of the two least happy countries, or generating plots directly from prompts.
pandas_ai.run(df, prompt='What is the sum of the GDPs of the 2 unhappiest countries?')
pandas_ai.run(df, "Plot the histogram of countries showing for each the gdp, using different colors for each bar")Combining ChatGPT’s natural‑language capabilities with Pandas AI enables users to interact with data through conversational prompts, reducing the need to write extensive code and making data analysis more accessible.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
