TL;DR
DataFrame QA is a novel zero-shot framework that allows Large Language Models (LLMs) to answer questions about large datasets by generating Pandas queries using only table headers and metadata. By completely omitting the actual data values from the prompt, this approach guarantees data privacy, slashes token costs, and circumvents context window limitations while achieving up to a 97% accuracy rate with GPT-4.
What Problem Does This Paper Address?
When using LLMs for Table Question Answering (Table QA), the standard approach is to stuff the entire table into the prompt alongside the user's question. While this works fine for tiny spreadsheets, it quickly falls apart in real-world applications.
Large dataframes—like those tracking weather, traffic, or user logs—can easily exceed the context window limits of even modern LLMs. Furthermore, processing thousands of rows of data incurs massive computational costs, suffers from the "Lost in the Middle" phenomenon (where LLMs miss information buried deep in long prompts), and exposes sensitive data to the model provider. DataFrame QA addresses these bottlenecks by decoupling query generation from data exposure.
Methods
Instead of feeding the whole table to the LLM, the DataFrame QA framework operates in three distinct stages:
- Pandas Query Generation: The LLM is given a prompt containing only the system instructions, the dataframe column names (headers), the data types of each column (e.g.,
str,int,float), and the user's natural language question. The LLM then outputs a Python code snippet containing a Pandas query. - Code Execution: The generated query is executed automatically inside a secure, isolated sandbox environment restricted to essential libraries (
pandas,numpy, andmath). - Result Evaluation: The output of the executed code is compared against the ground-truth answer. Because Pandas queries often return complex Series or DataFrame objects rather than raw strings, the authors use a relaxed evaluation criterion that considers the answer correct if the target value is contained within the returned object.
Datasets
The authors evaluated their framework using two datasets:
- WikiSQL: A standard text-to-SQL benchmark containing 15,878 table-question pairs. The authors converted these tables into dataframes and standardized all strings to lowercase to mitigate formatting ambiguities. WikiSQL primarily features simple, single-step information retrieval and basic aggregations.
- UCI-DataFrameQA: A new, more complex dataset created by the authors using 11 diverse dataframes from the UCI Machine Learning Repository. Using GPT-4, they generated 547 realistic question-query pairs tailored to three distinct real-world roles: Data Scientists (complex statistical analysis), General Users (consumer-oriented queries), and Data Owners (business insights). 78% of these questions require advanced multi-step operations like grouping, sorting, and correlation analysis.
Key Insights and Ideas
- Privacy via Abstraction: The core "aha moment" is that an LLM does not need to see a single row of data to write the logic required to query it. Metadata is entirely sufficient for code generation.
- The Instruction vs. Training Bias Conflict: A fascinating finding emerged when analyzing open-source models like CodeLlama. Despite explicit prompt instructions stating that all string values in the dataframe were lowercased, larger CodeLlama models (13B and 34B) frequently failed by capitalizing proper nouns in their queries (e.g., writing
df['Player'] == 'Terrence Ross'instead of'terrence ross'). This reveals a strong pre-training bias where the model prioritizes standard grammar rules over explicit runtime instructions. - Pandas vs. SQL Complexity: Generating Pandas queries is inherently more complex than generating SQL because Pandas offers a much wider, less structured array of data manipulation operations.
Key Results
The framework was tested across multiple closed and open-source models, including Llama2, CodeLlama, GPT-3.5, and GPT-4.
- State-of-the-Art Performance: GPT-4 dominated the benchmarks, achieving a 85.52% pass@1 accuracy on WikiSQL and an exceptional 97.24% on UCI-DataFrameQA.
- Scaling Laws: For the standard GPT and Llama2 series, performance scaled predictably with model size.

- Error Profiles: As shown in the error distribution heatmap below, smaller open-source models struggled heavily with "String Errors" (case sensitivity and matching) and "Access Errors" (using incorrect indices or keys).
Limitations and Future Work
Currently, the framework is strictly limited to generating code for the Pandas library. It lacks the ability to seamlessly integrate broader data science libraries like scikit-learn for machine learning tasks or matplotlib for automated data visualization.
Moving forward, the authors plan to expand the framework into a multi-agent system capable of leveraging a wider variety of libraries to solve highly complex, multi-modal data analysis problems, including advanced numerical calculations and automated reporting.
Summary updated