How to Build a DataFrame Chatbot using Python: Chat With Your CSV or Excel File!

In today’s data-driven world, we are drowning in spreadsheets — from sales reports and customer logs to academic scores and HR records. What if you could ask your data questions in plain English, and get the answers?

Welcome to the world of DataFrame Chatbots — smart assistant powered by Python, AI, and your CSV/Excel files.

In this blog you will learn how to:

  • Build a chatbot that talks to your spreadsheet
  • Use streamlit for a clean frontend
  • Process data with pandas
  • Answer natural language questions using LLM’s
  • Deploy the bot locally, without any API cost

Why a DataFrame Chatbot?

In today’s world, data is everywhere — sales report, employee records, customer logs, survey results — usually stored as spreadsheets. While tools like Excel and Pandas are powerful, they still require:

  • Technical Knowledge(e.g formulas, queries, scripting)
  • Manual exploration
  • Time-consuming filtering and analysis

Now Imagine this :

    That’s the power of DataFrame Chatbot. It lets you

    • Ask natural language questions about your data
    • Get summary answers, insights or even charts instantly
    • Do it all offline — no API, no Internet, no cloud dependency.
    • Save hours of time with automated data analysis

      Tools and Libraries Used:

      Full code to Build the Offline DataFrame Chatbot

      Save as app.py

      import pandas as pd
      import streamlit as st
      from langchain.agents import AgentType
      from langchain_experimental.agents import create_pandas_dataframe_agent
      from langchain_ollama import ChatOllama
      
      # Streamlit UI setup
      st.set_page_config(page_title="DataFrame Chat", page_icon="đź’¬", layout="centered")
      st.title("🤖 DataFrame ChatBot ")
      st.write("Developer Prem Kumar")
      
      # Read CSV or Excel
      def read_data(file):
          if file.name.endswith(".csv"):
              return pd.read_csv(file)
          else:
              return pd.read_excel(file)
      
      # Initialize session state
      if "chat_history" not in st.session_state:
          st.session_state.chat_history = []
      if "df" not in st.session_state:
          st.session_state.df = None
      
      # File upload
      uploaded_file = st.file_uploader("Choose a file", type=["csv", "xlsx", "xls"])
      if uploaded_file:
          st.session_state.df = read_data(uploaded_file)
          st.write("DataFrame Preview:")
          st.dataframe(st.session_state.df.head())
      
      # Show chat history
      for message in st.session_state.chat_history:
          with st.chat_message(message["role"]):
              st.markdown(message["content"])
      
      # User input
      user_prompt = st.chat_input("Enter your prompt.✨")
      
      if user_prompt:
          st.chat_message("user").markdown(user_prompt)
          st.session_state.chat_history.append({"role": "user", "content": user_prompt})
      
          # Load local LLM from Ollama
          llm = ChatOllama(model="gemma:2b", temperature=0)
      
          # Create agent with Pandas + LangChain
          pandas_df_agent = create_pandas_dataframe_agent(
              llm,
              st.session_state.df,
              verbose=True,
              agent_type=AgentType.OPENAI_FUNCTIONS,
              allow_dangerous_code=True
          )
      
          # Combine messages for agent context
          messages = [{"role": "system", "content": "You are a helpful assistant"}, *st.session_state.chat_history]
      
          # Get response
          response = pandas_df_agent.invoke(messages)
          assistant_response = response["output"]
      
          # Display and save assistant response
          st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
          with st.chat_message("assistant"):
              st.markdown(assistant_response)
      

      Try Sample Questions

      Once your file is uploaded and the app is running, ask:

      • What is the total sales per region?
      • Show me top 3 highest spendors?
      • Which month had most transactions?
      • Plot total profit by month?

      Real-World Use Cases

      SectorUse Case
      RetailChat with monthly sales reprot
      EducationAnalyze student performance spreadsheets
      HRInteract with employee data
      FinanceSummarize large accounting files
      NGOsTrack impact and donations via Excel

      Ready to Deploy?

      You can share this app internally or deploy it locally with:

      Conclusion

      With Langchain, Ollama and Stramlit, building a powerful, private AI chatbot for your spreadsheets is easier than ever — no cloud, no API keys, no limitations

      • Easy to use
      • Fully offline
      • Highly customizable
      • Secure by Design

      Prem Kumar
      Prem Kumar
      Articles: 19

      Leave a Reply

      Your email address will not be published. Required fields are marked *