Pandas to DuckDB
An eight-check workbook for translating DataFrame operations into SQL.

What you will build
Eight checks pass: selection, filtering, grouped totals, sorting, a left join, date filtering, running totals and a Parquet round trip.
This workbook is for readers who can run a Python script. It includes a small local example, explanations, exercises and worked answers. The package was checked on Windows with Python 3.13.17. Pinned package versions and setup instructions are included; they are not a claim to be the latest releases.
Read a sample
The following two sections are taken from the practical download edition.
Use one dataset for both implementations
The supplied sales.csv has five sales with dates, customer IDs, categories and amounts. Two customer IDs have names in a lookup table; customer 99 intentionally has no match. Two sales also have the same amount. These small irregularities make joins and ordering worth checking.
compare.py reads the CSV with Pandas and explicitly parses the date column. It registers the resulting DataFrame and customer lookup with an in-memory DuckDB connection. This lets the same data feed both implementations without maintaining a second database import.
This workbook is about semantics, not a speed contest. Its input is tiny, and it deliberately converts query results back to Pandas to compare them. A large-data workflow may be better served by querying files directly and writing results without collecting them into a DataFrame.
Translate selection and filtering
Selecting sale_id and amount in Pandas corresponds to a SELECT list in SQL. A Pandas boolean condition corresponds to WHERE. The example passes the amount threshold as a query parameter rather than inserting it into a SQL string. Use parameters for values supplied at runtime.
SQL results have no guaranteed order without ORDER BY. Every comparison that depends on row order includes an explicit ordering. Resetting the Pandas index removes an incidental index difference; it does not sort the rows for you.
Parameter placeholders do not substitute arbitrary table names, column names or SQL keywords. If a user selects a sort column, map it to a fixed allowlist of known columns rather than concatenating unchecked text into a query.
Inside the complete guide
- Use one dataset for both implementations
- Translate selection and filtering
- Aggregate and join with the right row meaning
- Make time and ordering explicit
- Write, read back and compare
- Exercises and worked answers
Files in your download
The 6-page PDF includes the walkthrough and complete source listing. The examples folder contains:
compare.pysales.csvREADME.txtrequirements.txt
Scope and source material
This is a focused learning project with synthetic example data. It does not include hosted services or production deployment. This revised edition is based on previously published articles and uses replacement runnable examples; it is not a verbatim reprint.
Get the guide on Payhip — £4.99