How to use PandasAI with a CSV file

PandasAI is a Python library that enhances pandas functionalities through the integration of natural language processing (NLP) capabilities. It utilizes a large language model (LLM) to automatically generate Python code for tasks such as answering data-related questions, conducting data analysis, and creating visualizations. In this answer, we will learn how to use PandasAI for data analysis with a CSV file.

Reading a CSV file

First, we’ll read the CSV file using the read_csv 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_csv("/data.csv")
a = data.head()
print(a)

Steps

Here are the steps to use PandasAI with a CSV file.

  1. The first step to use PandasAI with a CSV file is to install the PandasAI library. Once we have installed it, we can import it into our Python code as follows:
from pandasai import SmartDataframe
  1. Now, we can create a SmartDataframe object of our CSV file. The SmartDataframe object is what we’ll use to interact with our data in a natural language way.
data = SmartDataframe("csv_file.csv")
  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 a CSV 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 a csv 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.

Code explanation

Line 1: We import SmartDataframe from pandasai to answer our questions from a CSV 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.csv file.

Note: The above example shows how to use PandasAI to get insights from a CSV 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