Skip to content
Queria Docs
Esc
↑↓navigate↵open⌘Jpreview
On this page

Connect from Python

ATTACH a dataset with the duckdb package and get the results as a pandas DataFrame

Use the duckdb package to ATTACH a dataset and get SQL results as a pandas DataFrame. The example below takes the 10 most populous prefectures from the e-Stat statistics data.

import duckdb

conn = duckdb.connect()
conn.execute("INSTALL ducklake; LOAD ducklake;")
conn.execute("""
    ATTACH 'ducklake:https://data.queria.io/e_stat/ducklake.duckdb'
    AS e_stat (READ_ONLY)
""")

df = conn.execute("""
    SELECT DISTINCT area_name, value AS population
    FROM e_stat.ssds.a_pref_population
    WHERE item_name = 'A1101_総人口'
      AND year = 2024
      AND area_name != '全国'
    ORDER BY value DESC
    LIMIT 10
""").fetchdf()

print(df.head())

fetchdf() returns a pandas DataFrame. fetch_arrow_table() returns an Apache Arrow table instead. You can turn the results into charts and maps with matplotlib or Plotly.

Was this page helpful?