Sub topic 1
Choosing between pandas, Polars, DuckDB, and Spark
Prerequisites
You should know basic Python, rows and columns, common data types, and SQL clauses such as SELECT, WHERE, GROUP BY, and JOIN.
Workload types and data scale
Choose a tool based on the work, not only the file size. A small interactive analysis favors simplicity. Repeated batch transformations favor predictable execution and efficient file scans. Streaming, many users, or data larger than one machine may require a distributed system.
File size can mislead. A compressed 10 GB Parquet file may use much more memory after decompression and conversion into in-memory columns. Estimate memory needs using the expanded data, temporary results, joins, and several concurrent operations.
| Question | Why it matters |
|---|---|
| Does the data fit comfortably in memory? | Single-machine tools are usually simpler and faster to operate. |
| Is the work exploratory or scheduled? | Interactive work favors direct APIs; pipelines benefit from repeatable plans. |
| Does the task need a cluster? | Distribution helps scale, but adds network and operational costs. |
How the tools execute work
Eager execution performs an operation immediately. pandas usually reads data and runs transformations as your code reaches them. Lazy execution builds a plan first. Polars can inspect that plan, combine operations, and execute it later.
Single-node tools use one machine, even when they use multiple CPU cores. pandas, Polars, and DuckDB usually work this way. Spark distributes partitions across worker processes on a cluster. Distribution can process more data, but communication between workers can dominate simple tasks.
flowchart LR
A[Write transformations] --> B{Execution model}
B --> C[Eager: run now]
B --> D[Lazy: optimize then run]
C --> E[Single node or cluster]
D --> EColumnar formats: why Parquet and Arrow matter
Parquet stores each column together and supports compression and column selection. A query that needs two columns can avoid reading the others. Arrow defines an in-memory columnar representation shared by many tools, reducing conversion overhead.
> callout note > Use Parquet between pipeline stages when possible. It preserves types and avoids repeatedly parsing CSV text.
pandas: the default for in-memory analysis
pandas has a large ecosystem and familiar labeled DataFrames. It is a strong choice for data that fits comfortably in memory, especially notebooks, statistical work, and libraries built around pandas.
import pandas as pd
df = pd.read_parquet("sales.parquet", columns=["region", "revenue"])
result = df.groupby("region", as_index=False)["revenue"].sum()
print(result)Polars: a fast DataFrame engine with a lazy API
Polars uses a columnar engine and supports parallel execution. Its lazy API lets the optimizer push filters and column selection toward the file scan, reducing input work.
import polars as pl
result = (
pl.scan_parquet("sales.parquet")
.group_by("region")
.agg(pl.col("revenue").sum())
.collect()
)
print(result)Exercise
A compressed file is 8 GB, and the expanded data plus temporary results needs 30 GB. Which first choice is safer on a 16 GB machine: pandas or a lazy Polars query?
Show answer
Neither can guarantee success on that machine, but lazy Polars may reduce the data read. A larger machine or an out-of-core design is safer.