top of page
Writer's pictureTawatchai

PandasAI, Streamlit, and Pandas: The Dream Team for Data Analysis

Updated: Jun 21, 2023




Greetings! 👋

Hey, buddy! Today, I wanted to talk to you about something I've been working with recently and found to be a game-changer - the trifecta of Pandas, Streamlit, and a new addition to the family: PandasAI. Together, they're making data analysis not just easier, but truly intuitive.


A New Spin on an Old Classic: PandasAI 🦸‍♂️


We've all heard of Pandas, right? It's the go-to Python library for data analysis. But here's where the hero of our story comes in: PandasAI. Imagine interacting with your data in a way that feels natural, almost conversational. That's what PandasAI offers! PandasAI is the superhero sidekick to Pandas, making data analysis accessible and intuitive. Source


The Power of Simplicity: Streamlit


How about sharing your findings with the world? That's where Streamlit steps in. This open-source Python library lets you quickly create web applications to showcase your data analysis. With a few lines of code, you can create interactive, beautiful dashboards, making your data story just as compelling as it deserves to be.


A Glimpse into the Action: Code Snippet


To give you a little taste of what we're talking about, here's a snippet of a demo I put together, showing how to use PandasAI in conjunction with Pandas, all wrapped up in a neat Streamlit web app.

# Import necessary modules
import pandas as pd
import streamlit as st 
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI

# Set the title of the Streamlit application
st.title("PandasAI Demo")

# Request user to input OpenAPI API Key
api_key = st.sidebar.text_input("Enter OpenAPI API Key", type="password")

# Check if the user has entered an API key
if api_key:
    # Create a Sample DataFrame with data about countries, their GDP, and happiness index
    df = pd.DataFrame({
        "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
        "gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 1607402389504, 1490967855104, 4380756541440, 14631844184064],
        "happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
    })

    # Instantiate a language model from OpenAI
    llm = OpenAI(api_token=api_key)

    # Pass the language model to PandasAI
    pandas_ai = PandasAI(llm)

    # Display the first three rows from the sample dataset
    st.write("The initial three rows from the sample dataset.")
    st.write(df.head(3))    
    
    # Request the user to enter a question
    prompt = st.text_area("Enter a question", "Which are the 5 happiest countries?")    
    
    # Check if the "Generate Answer" button is pressed
    if st.button("Generate Answer"):
        # Check if the user has entered a question
        if prompt:
            # While the answer is being generated, show a loading spinner
            with st.spinner("Generating response..."):
                # Generate the answer using pandas_ai
                answer = pandas_ai(df, prompt=prompt)
                # Display the generated answer
                st.write(answer)
        else:
            # Display a warning if the user hasn't entered a question
            st.warning("Please enter a question")
else:
    # Display a warning if the user hasn't entered an API key
    st.warning("Please enter OpenAPI API Key!")


Execute Streamlit App


To execute the Streamlit app:


1. Open your terminal or command prompt.


2. Navigate to the directory containing the Streamlit script.


3. Run the following command: `streamlit run [your_script_name].py`.


This will launch a browser window displaying your Streamlit app.




PandasAI Demo

The screenshot below demonstrates the result following code execution. To fully understand the power and functionality of this tool, run the provided code and observe the output for yourself.






Wrapping Up


Cool, right? When you dive deeper, you'll discover that these tools can do so much more. They've made my life easier, and I bet they'll do the same for you.

Feel free to reach out if you want to talk more about these technologies or need help integrating them into your workflow. With the right tools - like Pandas, PandasAI, and Streamlit - data analysis can be downright enjoyable.

Let's Connect!

If you want to explore these tools further or just chat about data analysis, I'm here to help! Book a consulting call with me, and let's take your data skills to the next level.


Until Next Time

That's it for now. Happy analyzing, my friend! Remember, with these tools in your arsenal, you're all set to make a big splash in the world of data. Reach out anytime - I'd love to hear about your journey. Until next time, keep exploring and keep innovating! 🚀




191 views0 comments

Recent Posts

See All

Comments


bottom of page