Data Analysis with PandasAI: An Intelligent Way to Explore Data
PandasAI revolutionizes data analysis by enabling natural language queries on pandas DataFrames. Instead of writing complex code, simply ask questions like, “What is the total revenue?” or “Which product has the highest sales?” and let PandasAI generate answers and visualizations.

Introduction to Data Analysis with PandasAI
PandasAI is an innovative Python library that enhances traditional data analysis with the power of natural language processing (NLP). It allows you to query pandas DataFrames using natural language queries, making data exploration more intuitive and accessible.
In this blog, you'll learn how to:
- Set up PandasAI in your environment.
- Create a Smart DataFrame.
- Perform basic and advanced data analysis using natural language.
- Visualize data insights with intelligent queries.
Let's dive in!
1. Installation and Setup
First, let's install the PandasAI library. You can do this using pip:
!pip install -qU pandasai pandas
We also need to install the pandas
library if it's not already installed:
pip install pandas
2. Creating a Smart DataFrame
To use PandasAI, you'll need a Smart DataFrame (a pandas DataFrame enhanced with AI capabilities). Here's a step-by-step guide to creating one.
Import Libraries
import pandas as pd from pandasai import PandasAI from pandasai.llm.openai import OpenAI
Initialize the PandasAI Engine
You'll need an OpenAI API key to use the language model. Initialize PandasAI with your key as follows:
llm = OpenAI(api_token="YOUR_OPENAI_API_KEY") pandas_ai = PandasAI(llm)
Sample Data
Let's create a simple DataFrame with sales data:
data = { "Product": ["Laptop", "Mouse", "Keyboard", "Monitor", "Headphones"], "Sales": [120, 340, 150, 80, 300], "Revenue": [120000, 3400, 15000, 8000, 30000] } df = pd.DataFrame(data) print(df)
Output:
Product Sales Revenue 0 Laptop 120 120000 1 Mouse 340 3400 2 Keyboard 150 15000 3 Monitor 80 8000 4 Headphones 300 30000
3. Basic Data Analysis with Natural Language
Now that we have a Smart DataFrame, let's perform some basic analysis using natural language queries.
Example Query 1: "What is the total revenue?"
pandas_ai.run(df, prompt="What is the total revenue?")
Output:
The total revenue is $180,400.
Example Query 2: "Which product has the highest sales?"
pandas_ai.run(df, prompt="Which product has the highest sales?")
Output:
The product with the highest sales is Mouse with 340 units sold.
4. Advanced Queries and Visualizations
PandasAI can also generate visualizations based on your queries.
Example Query: "Show a bar chart of sales by product."
pandas_ai.run(df, prompt="Show a bar chart of sales by product.")
Output:
A bar chart will be generated showing sales figures for each product.
Example Query: "What is the average revenue?"
pandas_ai.run(df, prompt="What is the average revenue?")
Output:
The average revenue is $36,080.
Conclusion
PandasAI simplifies data analysis by allowing you to interact with your datasets using natural language. This is especially useful for those who may not be familiar with Python or pandas syntax but still need to extract insights from data.
Key Takeaways
- Ease of Use: Natural language queries make data analysis accessible.
- Integration: Works seamlessly with pandas DataFrames.
- Visualization: Automatically generates charts based on queries.
Next Steps
- Experiment with your own datasets.
- Combine PandasAI with other libraries like
matplotlib
andseaborn
for enhanced visualizations. - Explore more complex queries and custom prompts.
Resources
--------------------------------------------------------------------------
Stay Updated:- Follow Build Fast with AI pages for all the latest AI updates and resources.
Experts predict 2025 will be the defining year for Gen AI implementation.Want to be ahead of the curve?
Join Build Fast with AI’s Gen AI Launch Pad 2025 - your accelerated path to mastering AI tools and building revolutionary applications.
👉 Limited Spots, join the waitlist now: www.buildfastwithai.com/genai-course
AI That Keeps You Ahead
Get the latest AI insights, tools, and frameworks delivered to your inbox. Join builders who stay ahead of the curve.
You Might Also Like

How FAISS is Revolutionizing Vector Search: Everything You Need to Know
Discover FAISS, the ultimate library for fast similarity search and clustering of dense vectors! This in-depth guide covers setup, vector stores, document management, similarity search, and real-world applications. Master FAISS to build scalable, AI-powered search systems efficiently! 🚀

Smolagents a Smol Library to build great Agents
In this blog post, we delve into smolagents, a powerful library designed to build intelligent agents with code. Whether you're a machine learning enthusiast or a seasoned developer, this guide will help you explore the capabilities of smolagents, showcasing practical applications and use cases.

Building with LLMs: A Practical Guide to API Integration
This blog explores the most popular large language models and their integration capabilities for building chatbots, natural language search, and other LLM-based products. We’ll also explain how to choose the right LLM for your business goals and examine real-world use cases.