Talk to your data just like a human, using PandasAI
Pandas is a Python package that provides fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. It aims to be the fundamental, high-level building block for doing practical, real-world data analysis in Python. (Ref: https://github.com/pandas-dev/pandas)
PandasAI is a Python library that adds Generative AI capabilities to pandas, the popular data analysis and manipulation tool. It is designed to be used in conjunction with pandas and is not a replacement for it.
Let's get hands-on now.
- Install required libraries
pip install pandas pandasai
2. Import required libraries and call Open API Key. Create your own key at https://platform.openai.com/account/api-keys. I had rate limit errors, so I went for paid subscription.
import pandas as pd
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI
OPENAI_API_KEY = "sk-4QyUSg229FWvkW9obxvsTXmF8K" #Put your own key here
llm = OpenAI(api_token=OPENAI_API_KEY)
3. We will import Nifty Index data from Yahoo Finance for analysis. We can also use CSV file here.
pip install yfinance
Now import Nifty data for a month
import yfinance as yf
indices = ['^NSEI']
for index in indices:
print(index)
dfdaily = yf.download(index, period="1mo", interval = "1d")
dfdaily.dropna(inplace=True)
4. Now invoke PandasAI
pandas_ai = PandasAI(llm)
Let’s try with a simple query.
df1 = pandas_ai.run(dfdaily, prompt='''
what is Nifty closing price on June 14th 2023?
''', is_conversational_answer=False)
df1
You will see below
18147.650390625
5. Let’s try plotting a chart without writing code.
df1 = pandas_ai.run(dfdaily, prompt='''
line chart a table that shows the Nifty closing price, date
''', is_conversational_answer=True)
X-axis labels do not look great. Let’s change it. I have added “and rotate x-axis labels”
df1 = pandas_ai.run(dfdaily, prompt='''
line chart a table that shows the Nifty closing price, date and rotate x-axis labels
''', is_conversational_answer=True)
This is game-changing. This will pave the way to complex ML problems like forecasting, segmentation, etc. Have fun coding!!!
Stay updated with the latest news and updates in the creative AI space — follow the Generative AI publication.