Python para Data Science e Analytics
Python para Data Science e Analytics › Guides › DuckDB or pandas for data analysis: when to use each

DuckDB or pandas for data analysis: when to use each

Use pandas to manipulate and DuckDB to query. pandas loads data into memory and gives you an expressive API to clean, transform, join and visualize; DuckDB runs analytical SQL straight over files such as CSV and Parquet without loading everything into memory and without running a server. In most projects the two combine: DuckDB filters and aggregates, pandas transforms, visualizes and models.

Topics: duckdb, pandas, análise de dados

DuckDB or pandas: which one should you start with?

Two things decide it: the size of the data and the shape of the question. If the data fits in memory and you are testing hypotheses step by step, pandas is the shorter path. If the question is a repeated aggregation over many files on disk, DuckDB saves you from loading everything before you get an answer.

Three questions settle it in practice: does the data fit comfortably in the machine's memory? Is the query an aggregation that returns few rows? Are you more comfortable writing Python or SQL? The answers point to a tool, and you rarely have to pick only one.

When is pandas the better choice?

The pandas strength is interactive manipulation. In a notebook the loop of write, run and read the output is short: you create columns, handle nulls, fix types, join two or three tables and plot the result right after.

It is also the center of the ecosystem. It talks to NumPy, to visualization libraries and to scikit-learn, which makes it the natural bridge between cleaning the data and fitting the model. Replacing pandas at that stage usually costs more than it gains.

When does DuckDB work better?

DuckDB was built for analytical queries. It reads files straight from disk, uses columnar execution and returns a small result, which avoids pulling millions of rows into memory just to count, group or filter.

That changes the workflow when the data is larger than memory or when the same aggregation runs many times. Instead of loading everything, you write SQL and get the table already summarized, ready to move on.

Two honest caveats: DuckDB is not a server database for many users writing at once, and a query that returns many rows will still consume memory on the pandas side. It handles the heavy part of the query; it does not replace the rest of the workflow.

Can you use DuckDB and pandas together in the same project?

Yes, and that is usually the most productive combination. DuckDB returns a query result as a DataFrame, and pandas can hand data to DuckDB to query. The flow becomes: SQL to filter and aggregate, pandas to transform, visualize and model.

A typical pipeline: DuckDB reads several Parquet files and returns an aggregated slice; pandas receives that slice, handles nulls, creates features and feeds the model. You get performance in the heavy part and flexibility in the fine part, without rewriting the whole analysis in another language.

What should you learn to use both tools with confidence?

The foundation comes before the tool. Without Python, without a sense of types and without knowing what a DataFrame is, DuckDB SQL turns into trial and error. The order that usually works: basic Python, NumPy, pandas, cleaning and preparation, visualization, and only then analytical queries.

The Python for Data Science and Analytics course follows that logic, from zero to advanced, with three main modules — Basic, Intermediate and Advanced — plus bonus modules. It covers IDE and Jupyter Notebook setup, basic functions, loops and conditionals, NumPy and pandas, user-defined functions, data visualization, data cleaning and preparation, and outlier detection.

It then moves into machine learning: linear and logistic regression, K-Means, SVM, Naive Bayes, decision trees, Random Forest, Gradient Boosting, XGBoost, LightGBM, feature selection and cross-validation, closing with a final end-to-end exploratory analysis and machine learning project.

Format: high-quality recorded lessons watched at your own pace, guided exercises, real market projects for your portfolio, unlimited access with no expiry, a completion certificate and an exclusive community for networking. The price is R$ 470, or 12 installments of R$ 20,37 on a card, paid through the Hotmart checkout with card or boleto. You do not need to know how to program before starting. Details and enrollment are at python.paanalytics.net.

CriterionpandasDuckDB
Main strengthInteractive manipulation in memoryAnalytical queries over large files
Where the data livesLoaded into memoryRead from disk, including CSV and Parquet
Language you writePython APISQL
Typical outputA transformed DataFrameA small aggregated result
Best fit for data sizeFits comfortably in memoryLarger than memory, or repeated aggregations
Role in a pipelineClean, transform, visualize, modelFilter, join, aggregate
SetupInstall the library and import it in a notebookRuns in-process, no server to run

Criteria come from how each tool is designed: pandas is an in-memory library, DuckDB is an in-process analytical database that queries files directly. No benchmark numbers are claimed here.

Start with the foundation that makes pandas and DuckDB work

Secure payment via Hotmart, up to 12 installments.

Enroll now Product site

Frequently asked questions

Is DuckDB faster than pandas?

It depends on the workload. DuckDB is designed for analytical queries over large files and avoids loading everything into memory first, so aggregations on big files tend to run with less memory pressure. On small data that already fits in memory, speed is often not the deciding factor, and the interactive pandas workflow may get you to an answer sooner.

Do I need to know SQL to use DuckDB?

Yes, DuckDB is queried with SQL. If you already write SQL, you can start immediately. If not, the part you need for filtering, grouping and joining is a small and learnable subset, and knowing pandas first helps because you already understand the data you are querying.

Can I use DuckDB inside a Jupyter Notebook?

Yes. DuckDB runs in-process, so it works in a notebook the same way pandas does: you query files and get the result back as a DataFrame that you keep working with in pandas.

Should I learn pandas or DuckDB first?

pandas first. It teaches you how data is shaped, cleaned and checked, and that knowledge carries over to SQL. DuckDB then becomes a faster way to get the data ready rather than a different mental model.

Does the course cover DuckDB?

The course builds the Python, NumPy and pandas foundation that any DuckDB work depends on: types, DataFrames, cleaning, preparation and visualization. The SQL layer sits on top of that base, and the pandas skills you build are what you use to keep working with the results.

Ler em português →