GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||
GeezerVison is showing thepythoncode.com — SQL Analytics at Lightning Speed: Getting Started with DuckDB in Python - The Python Code Welcome! Meet our Python Code Assistant, your new coding buddy. Why wait? Start exploring now! If you work with data in Python, you've probably spent way too much time waiting for Pandas to grind through a CSV file. You know the drill — make coffee while Here's the thing: it doesn't have to be this way. In fact, by 2026, it really shouldn't be. DuckDB is an embedded analytical database that you install with a single In this tutorial, I'll walk you through everything you need to start using DuckDB effectively — from basic queries to hybrid workflows that combine the best of DuckDB, Pandas, and Polars. What Exactly Is DuckDB?DuckDB is often called "SQLite for analytics." That comparison works because both are embedded databases — they live inside your application process rather than running as a separate server. But while SQLite is built for transactional workloads (think: your phone's contact list), DuckDB is purpose-built for analytical queries — the kind you'd run in a data warehouse. Under the hood, DuckDB uses a columnar storage engine (data is stored column-by-column rather than row-by-row), which makes aggregations and scans dramatically faster. It's written in C++ with a Python client that's dead simple to use. And because it's vectorized and multi-threaded by default, it saturates your CPU cores without you lifting a finger. The real headline feature, though, is this: you can point DuckDB at a CSV or Parquet file sitting on your disk and run SQL against it without importing anything first. No InstallationNothing fancy here — just the usual pip install:
If you want to follow along with the hybrid workflow examples later, grab Pandas and Polars while you're at it:
That's it. You're ready. No Docker, no Postgres connection string, no cloud account. Let's write some queries. Getting Started: Your First DuckDB QueriesFire up a Python shell or Jupyter notebook and create a connection. The default
Let's create a small table from scratch to get a feel for the API:
The
Grouping and aggregation are just as straightforward:
So far, this looks like any SQL database. But the real magic starts when you stop importing data and start querying files directly. The Killer Feature: Query Files Directly — No Import RequiredThis is where DuckDB genuinely changes how you work. Let me show you what I mean. I generated a 500,000-row sales dataset — the kind of CSV that makes Pandas sweat a little. It's about 35 MB on disk with columns for order ID, region, product, category, quantity, unit price, and order date. Here's the DuckDB way to analyze it:
Notice what didn't happen here. I didn't call The result came back in 0.075 seconds:
This one capability — running SQL directly against files — eliminates the entire "load then query" workflow that dominates most Python data scripts. And it's not limited to CSV. DuckDB reads Parquet, JSON, and even queries files over HTTP/S3 natively. DuckDB vs Pandas: Let's Talk PerformanceI ran the exact same aggregation on the same 500K-row CSV file using both DuckDB and Pandas. Here's what happened:
That's a nearly 5x speedup on a modest dataset. On larger files — the kind where Pandas starts pushing against your RAM ceiling — the gap widens significantly. DuckDB's columnar engine only needs to scan the columns referenced in your query, while Pandas loads the entire DataFrame into memory before doing anything. Now, to be fair: if you're working with tiny datasets (a few thousand rows), the difference won't matter much. Pandas is perfectly fine for that. But the moment you're dealing with anything north of 100K rows, DuckDB starts pulling away fast. Parquet: Even Faster, Even SmallerIf you're still storing analytical data as CSV in 2026, let me gently suggest you switch to Parquet. It's a columnar file format that compresses better and reads faster — and DuckDB absolutely flies on it. That 35 MB CSV I mentioned earlier? As Parquet, it shrinks to 11 MB. And queries get even faster because DuckDB can skip entire columns it doesn't need:
This query ran in 0.015 seconds — 5× faster than the already-fast CSV query. DuckDB can do this because Parquet files contain embedded statistics (min/max values per column chunk), allowing the engine to skip entire sections of the file that can't possibly match your query. Window Functions: Ranking Without the HeadacheOne thing SQL has always done better than Pandas is window functions. Here's how to find the top 3 products by revenue in each region — a query that would take several chained Pandas operations:
The CTE (Common Table Expression) with The Hybrid Workflow: DuckDB + Pandas + PolarsHere's where things get interesting. DuckDB doesn't replace Pandas or Polars — it complements them. The sweet spot is using each tool for what it does best:
Here's a concrete example. First, DuckDB crunches through 500K rows and spits out a monthly summary:
Then Pandas takes over for a pivot table — something it handles beautifully on small data:
And if you need to squeeze out every last millisecond, Polars can take the baton:
The whole pipeline — 500K rows aggregated, pivoted, and polished — completes in under 0.07 seconds. Each tool handled exactly the part it excels at, and you didn't have to commit to any single framework for the entire workflow. Persistent Databases: When You Need to Keep the Data AroundSo far we've used in-memory connections. But DuckDB also supports file-based persistent databases — just pass a path to
The resulting This is incredibly useful for iterative analysis. Load once, query many times — and close the connection when you're done without losing anything. Exporting ResultsOnce you've done your analysis, DuckDB makes it trivial to export results in whatever format you need:
The When Should You Actually Use DuckDB?I'm not going to tell you to throw away Pandas. That would be bad advice. Here's my honest take on when each tool makes sense:
The pattern to internalize is this: use DuckDB to shrink your data, then hand off a manageable DataFrame to Pandas or Polars for the parts they excel at. Wrapping UpDuckDB solves a problem that's been nagging Python data folks for years: the awkward gap between "this file is too big for Pandas to handle comfortably" and "I don't want to set up a full database server." It's not a replacement for your production Postgres instance, and it won't help you train neural networks. But for the vast middle ground of data analysis — where you're wrangling CSV files, joining datasets, and building reports — it's genuinely transformative. The fact that you can Further Reading: Ready for more? Dive deeper into coding with our AI-powered Code Explainer. Don't miss it!
Sharing is caring!
CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! | |||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |