PandasAI is a Python library that extends the capabilities of pandas by providing natural language processing (NLP) capabilities. It uses a large language model (LLM) to generate Python code to answer questions about data, perform data analysis, and generate visualizations. In this answer, we will learn how to use PandasAI for data analysis with an Excel file.
First, we’ll read the XLSX file using the read_excel
function of pandas and then see how our data looks using data.head()
.
import pandas as pd#Loading the "data.csv" filedata = pd.read_excel("/data.xlsx")a = data.head()print(a)
In order to use Excel files as a data source, we need to install the pandasai[excel]
extra dependency using the following command.
pip install pandasai[excel]
Here are the steps to use PandasAI with an Excel file.
from pandasai import SmartDataframe
SmartDataframe
object of our XLSX file. The SmartDataframe
object is what we’ll use to interact with our data in a natural language way.data = SmartDataframe("excel_file.xlsx")
SmartDataframe
object, we can start asking it questions about our data. For example, we can ask it the following question:answer = df.chat('Which movie has the best IMDb rating but a shorter runtime?')
Now, we’ll see the complete implementation of how to use PandasAI with an Excel file using an example.
Movie Title,Year,IMDb Rating,Runtime (minutes) The Shawshank Redemption,1994,9.3,142 The Godfather,1972,9.2,175 Pulp Fiction,1994,8.9,154 The Dark Knight,2008,9.0,152 Forrest Gump,1994,8.8,142 Inception,2010,8.8,148 Schindler's List,1993,8.9,195 The Matrix,1999,8.7,136 Fight Club,1999,8.8,139 The Lord of the Rings: The Fellowship of the Ring,2001,8.8,178
Note: Make sure to replace
OPENAI_API_KEY
with your actual OpenAI API key. For further details on how to get an OpenAI API key, check out our Answer on How to get API Key of GPT-3 answer.
Line 1: We import SmartDataframe
from pandasai
to answer our questions from a XLSX file.
Lines 2–3: We import and initialize the OpenAI
language model (referred to as llm
here) from the pandasai.llm
module.
Line 6: We instantiated a SmartDataframe
object to interact with it in natural language to answer questions about our data.xlsx
file.
Note: The above example shows how to use PandasAI to get insights from an XLSX file quickly and easily. We can use PandasAI to answer various questions about our data and generate reports and visualizations.
Unlock your potential: PandasAI series, all in one place!
To continue your exploration of PandasAI, check out our series of Answers below:
What is PandasAI?
Understand the basics of PandasAI and how it enhances data analysis with AI-driven capabilities.
How to use PandasAI with a CSV file
Learn how to integrate PandasAI with CSV files for efficient data processing and analysis.
How to use PandasAI with an Excel file
Discover how to leverage PandasAI to analyze and manipulate Excel files effortlessly.
How to implement the SmartDataframe of PandasAI
Explore the SmartDataFrame feature of PandasAI and how it simplifies complex data operations.
Free Resources