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.
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" filedata = pd.read_csv("/data.csv")a = data.head()print(a)
Here are the steps to use PandasAI with a CSV file.
from pandasai import SmartDataframe
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")
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 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
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.
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:
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