Practical data skills
DuckDB vs Pandas: Analyse the Same Sales Dataset
Run a small sales analysis in DuckDB SQL and pandas, compare matching results, and learn when each approach is useful.
16 September 2026 · Prakhar ShrivastavaDuckDB lets you analyse data using SQL, including querying a pandas DataFrame directly. Pandas provides a Python DataFrame interface. Choose based on the transformations you need and the language your team can maintain; neither is automatically faster for every workload.
A reproducible comparison
Install pandas and duckdb in a Python environment with python -m pip install pandas duckdb. The following synthetic orders store money as integer cents to avoid binary floating-point rounding in this example. All monetary amounts represent US dollars.
import pandas as pd
import duckdb
orders = pd.DataFrame({
"order_id": [1, 2, 3, 4],
"channel": ["Search", "Email", "Search", "Email"],
"status": ["completed", "completed", "cancelled", "completed"],
"amount_cents": [10000, 5000, 8000, 2500],
})
pandas_result = (
orders.loc[orders["status"].eq("completed")]
.groupby("channel", as_index=False)["amount_cents"].sum()
.sort_values("channel").reset_index(drop=True)
)
con = duckdb.connect()
con.register("orders_input", orders)
sql_result = con.execute("""
SELECT channel, SUM(amount_cents) AS amount_cents
FROM orders_input
WHERE status = 'completed'
GROUP BY channel
ORDER BY channel
""").df()
assert pandas_result["channel"].tolist() == sql_result["channel"].tolist()
assert pandas_result["amount_cents"].tolist() == sql_result["amount_cents"].tolist()
print(pandas_result)
con.close()
Both results should contain Email: 7,500 cents ($75) and Search: 10,000 cents ($100). The total is $175. The cancelled $80 order is excluded. The assertions compare values rather than requiring identical numeric dtypes, which can differ between the tools.
What the two versions express
In pandas, a Boolean mask filters the rows, groupby identifies groups, and sum combines their amounts. In SQL, WHERE, GROUP BY and SUM express the same operations. Sorting explicitly makes the result order deterministic for comparison.
DuckDB’s registration step gives the DataFrame a table name within the connection. The example does not upload your data to a cloud warehouse. It also does not benchmark performance: four rows are useful for checking logic, not for drawing speed conclusions.
When to choose each
Use pandas when your analysis already relies on Python libraries, custom transformations or plotting. Try DuckDB when you prefer relational SQL, have several joins and aggregations, or want to query supported file formats through SQL. They can be used together; a SQL result can become a DataFrame for charting.
For a fair performance comparison, record the versions, input size, hardware, memory use, read time and repeated execution time. Compare equivalent operations and separate loading data from running the transformation.
Two edge cases worth testing
Add an order with a missing channel. Pandas groupby drops missing group keys by default; use dropna=False if you want a missing-channel group. SQL groups null keys together. Also test missing amounts: decide whether an unknown amount should remain unknown or be treated as zero, then implement that policy in both versions.
Practice: Add a completed Email order worth $25. Email should become $100 and the overall total $200. Add a duplicate order_id next and make your validation reject it instead of silently increasing revenue.
See the Python guide and pandas merge validation example.
Sources and further reading
Found an error? Send a correction.