liveBook · Manning

Fetchdf

As explained in DuckDB in Action

fetchdf is a DuckDB result-fetching method that returns query results as a pandas DataFrame.

fetchdf is a DuckDB result-fetching method that converts a query result into a pandas DataFrame. It fits workflows that move between SQL filtering and pandas analysis, while cursors remain lighter for row-by-row iteration. section 6.3

Filter a pandas DataFrame with parameterized SQL

people = pd.DataFrame({"name": ["Michael Hunger", "Michael Simons", "Mark Needham"], "country": ["Germany", "Germany", "Great Britain"]}) params = {"country": "Germany"} result = duckdb.execute(""" SELECT * FROM people WHERE country <> $country """, params).fetchdf() The returned result is a pandas DataFrame ready for further analysis.

Use fetchdf when the next step benefits from pandas or an interactive table:

  • Query pandas DataFrames directly as DuckDB relations. section 6.3
  • Pass parameterized values into SQL and receive structured tabular results. section 6.3
  • Feed query results into Streamlit, where pandas DataFrames render as interactive tables. section 9.1.3
params = {"country": "Germany"}
duckdb.execute("""
SELECT *
FROM people
WHERE country <> $country
""", params).fetchdf()name   country
0  Mark   Great Britain

Shows the compact Python pattern for parameterized SQL followed by fetchdf. section 6.3

How DuckDB in Action covers it

How deep it goes
Explains result conversion, parameterized querying, and cursor trade-offs without detailing fetchdf internals.
Examples
Uses a people DataFrame and a parameterized country filter, then displays player-match results in Streamlit.
What it assumes
Assumes basic Python, SQL querying, and pandas DataFrame familiarity.
What you can do after
Apply fetchdf via DuckDB to produce pandas DataFrames for analysis or interactive Streamlit display.
How it connects
Ties DuckDB to pandas DataFrames, SQL parameterization, Python, and Streamlit.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of DuckDB in Action. The section links point to the parts of the book it draws on.