AI Hints
Home›RAG Tutorial›Tables, Databases and Long Documents
RAG · Lesson 88 of 120

Tables and Numbers

Structured data.

⏱ 2 min read▶ Video at 1:26:36🎓 Free
Watch this lesson in the RAG courseWatch on YouTube ↗Full RAG course

Tables are hard for RAG. If we split a table by characters, a chunk might hold the numbers without the column names. The fix is to turn each row into a sentence that carries its own headers.

Tables and Numbers
From the video: Tables and Numbers
  • Tables split badly
  • Numbers lose headers
  • One row, one sentence

Rows into sentences

The table has a header row, a divider, and two models. We read the header names. Then each row becomes one sentence, pairing every value with its column name. Run it. Each line now makes sense on its own.

table_rows.pypython
table = """| Model | Price | Weight | Range |
|---|---|---|---|
| Volt | £1,899 | 23 kg | 60 to 100 km |
| Volt Pro | £2,499 | 21 kg | 80 to 120 km |"""

cells = lambda line: [x.strip() for x in line.strip("|").split("|")]
lines = table.splitlines()
names = cells(lines[0])

for line in lines[2:]:
    values = cells(line)
    print(values[0] + ": " + ", ".join(f"{n} {v}" for n, v in zip(names[1:], values[1:])) + ".")
Output
Volt: Price £1,899, Weight 23 kg, Range 60 to 100 km.
Volt Pro: Price £2,499, Weight 21 kg, Range 80 to 120 km.

Numbers and RAG

Embeddings find rows, but they can't do maths. For questions like, which is the lightest, or what's the average price, query the data directly, with SQL or pandas. Use RAG for text, and a database for numbers.

keyvalue
Rowsone per chunk
Headersin every row
Mathsuse SQL or pandas
RAGfor text

Summary

Turn each table row into a sentence with its headers. For maths on numbers, query the data directly. Next, two sources.

Key takeaways
  • Row sentences
  • Keep headers
  • SQL for maths
Next up: Two Sources — documents and a database