How to use PandasAI with an Excel file

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.

Reading 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" file
data = pd.read_excel("/data.xlsx")
a = data.head()
print(a)

Installation

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]

Steps

Here are the steps to use PandasAI with an Excel file.

  1. The first step, after installing the PandasAI library and its extension with Excel, is to import it into our Python code as follows:
from pandasai import SmartDataframe
  1. Now, we can create a 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")
  1. Once we have created a 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?')

Implementation

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
Implementation with an Excel file

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.

Code explanation

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:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved