Retrieval Over Spreadsheets and CSVs: Parsing Rows, Chunking Tables and Answering Questions

Standard retrieval treats a CSV like plain text and breaks rows apart. Learn the full pipeline: parse rows, chunk by row, and answer questions well.

By Galactic News Editorial · Published 2026-11-24T11:22:15+00:00 · Updated 2026-11-24T11:22:15+00:00

Retrieval over spreadsheets CSVs chunking tables for question answering

Retrieval Over Spreadsheets and CSVs: Parsing Rows, Chunking Tables and Answering Questions

Spreadsheets hold the answers to most business questions. Sales by region, inventory by warehouse, support tickets by product. The problem is that standard retrieval treats a CSV like plain text. It splits rows in the middle, drops headers, and returns fragments that make no sense.

Retrieval over spreadsheets and CSVs needs a different pipeline. Parse rows as units, chunk by row or row group, and answer questions with the table structure intact. This tutorial walks through the full pipeline, from loading a CSV to evaluating the answers.

Step 1: Parse Rows, Not Characters

Start by loading the file with a real parser. Python pandas reads CSVs, Excel sheets, and Parquet files into structured frames. For Excel, use pandas read_excel with sheet_name set to None to process each sheet separately. For messy sheets, use openpyxl to inspect the first five to ten rows before parsing.

Handle the mess before it reaches the embedder. Detect the real header row, since many exports put titles and notes above it. Unmerge merged cells. Skip footers and total rows that repeat data. A footer that says Total can poison retrieval if it is indexed as a normal row.

LangChain offers CSVLoader for the simple case. It loads CSV data with a single row per document, formatting each row as key-value pairs such as Team: Nationals, Payroll (millions): 81.34, Wins: 98. It also supports a source_column argument so each document carries its row identity in metadata. UnstructuredCSVLoader adds an elements mode that keeps an HTML representation of the table in metadata.

Step 2: Chunk Tables by Row

Chunk by row, not by character. Each logical row, including its header context, should be a single chunk. For CSV files, that means one chunk per data row, prefixed with the column headers. For Excel, handle each sheet separately. For Parquet, use row groups if they align with your query patterns, but do not assume they do.

Format each row as a key-value string or a natural language sentence. A product row becomes Product: Widget A, Price: 29.99, Category: Electronics, Stock: 15. This preserves the attribute-value relationships that raw CSV text loses. Always include headers in each chunk, or the model cannot infer what the numbers mean.

Wide tables need a different split. Group related columns into separate chunks, such as all pricing fields together. Link every chunk back to its source row ID. You will need that link for attribution and reranking.

For very wide or very long tables, group rows by a logical key, such as customer ID, and concatenate the selected columns into one chunk under a token budget. Keep each chunk under roughly 200 tokens for tabular data. If you index millions of rows, overlap becomes expensive. Use a row-level chunker that treats each row as an atomic unit and rely on column-level embeddings to capture relationships.

Step 3: Choose the Retrieval Strategy

Semantic search alone struggles with tables. A question like which product costs the most is a computation, not a similarity match. Combine strategies instead.

Use vector search for fuzzy questions, such as which customers complained about delivery. Use metadata filters for exact constraints, such as region equals Europe. Use a text-to-SQL or pandas query path for aggregation questions, such as total sales by month. DuckDB is useful for querying Parquet and CSV files directly in the pipeline, sometimes better than loading everything into pandas for large files.

LangChain RecursiveCharacterTextSplitter handles free-form text well, with overlap built in. For tabular data, build the row chunks with pandas and a template first, then apply standard splitting only to the non-tabular parts. LlamaIndex offers SimpleDirectoryReader with built-in CSV loaders that preserve structure, and a pandas mode for row-wise chunking. Unstructured.io handles messy Excel and PDF-tabular hybrids by detecting tables and outputting structured elements.

Step 4: Answer Questions With Grounded Context

Retrieval is only half the job. The answer step must show its work. Pass the retrieved row chunks to the model with their headers and row IDs intact. Ask the model to answer only from the retrieved rows. Require it to cite the row IDs it used.

For numeric questions, compute first and narrate second. Run the aggregation in code, then let the model explain the result in words. A model that adds numbers from retrieved text will eventually hallucinate a total. Code does not.

Handle the failure modes explicitly. If no rows match, say so instead of guessing. If the question needs a column that was not indexed, say which data is missing. If two rows conflict, show both and let the user decide.

Step 5: Evaluate Chunk Quality and Retrieval Accuracy

Measure the pipeline, not just the answers. RAGAS scores chunk quality in the context of retrieval and generation. Track context precision, context recall, and faithfulness on a set of known questions with known answers.

Run a quick benchmark before tuning. Embed a sample of 1,000 chunks at different sizes, record average latency, and check recall against a ground-truth answer set. For most embedding models, 256 to 512 tokens is a workable range for text, with 10 to 20 percent overlap. Tabular rows need far smaller chunks, around 50 to 100 tokens per row group.

Watch latency as the vector count grows. A 10 to 15 percent latency increase per 10 percent growth in vector count is a real concern when serving hundreds of requests per second. Row-level chunking keeps the vector store small and fast.

Tools Compared for Retrieval Over Spreadsheets CSVs

ToolRoleTabular strength
LangChain CSVLoaderDocument loadingOne row per document with headers as key-value pairs
LlamaIndexIndexing and retrievalCSV loaders with pandas mode for row-wise chunking
pandasParsing and chunkingRow iteration, grouping, and template formatting
DuckDBDirect queryingSQL over CSV and Parquet without full loads
Unstructured.ioMessy file handlingTable detection for Excel and PDF hybrids
RAGASEvaluationContext precision, recall, and faithfulness scoring

Bottom Line

Retrieval over spreadsheets and CSVs works when rows stay intact. Parse with a real parser, chunk by row with headers attached, combine vector search with metadata filters and code-based aggregation, and evaluate with RAGAS. Treat the CSV as a table, not as text, and the answers will follow.