---
title: Connect from Python
description: ATTACH a dataset with the duckdb package and get the results as a pandas DataFrame
sidebar:
  label: Python
---

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.

```python
import duckdb

conn = duckdb.connect()
conn.execute("INSTALL ducklake; LOAD ducklake;")
conn.execute("""
    ATTACH 'ducklake:https://data.queria.io/e_stat/metadata.db'
    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.

:::note
With `queria.connect()` from the `queria` package, you don't write the extension loading or ATTACH. It attaches the datasets your SQL names. See the [Python API reference](/en/reference/python-api).
:::
