The survey is kept in one database per region, and a script that loops over them is the usual first step. This module reads them all in one DuckDB query: it attaches each regional file, stacks the tables by column name, and leaves out the removed households inside each region, since a household id repeats from one region to the next.
Then it keeps the codes whole. One region stores its area codes as zero-padded text and the others as numbers; the database export is Windows-1252, writes every missing value as \N and pads its text with spaces. You read each the right way, give every column its type from the codebook, and save one flat table of fishing units as Parquet behind a row-count check. The last section reads the same tables from a MySQL server with DuckDB’s mysql extension, without putting the password in the code.
What you do
Section titled “What you do”- One regional database into pandas and polars
- Every region in one query: ATTACH, UNION ALL BY NAME, deleted rows filtered
- Codes kept whole: text types, zero-padding, null tokens, nullable integers
- The flat table saved as Parquet, with a row-count check
- The real thing: reading a MySQL server with DuckDB's mysql extension
The module’s notebooks
Section titled “The module’s notebooks”- 10_flat_table.pyGet the tables: from regional databases to one flat tableRead itCompanion · 8 exercises
The notebooks are in your course folder; here you can read them without running anything. For reference in any module: primer: from r to python and primer: survey sampling in one page and glossary.
Quizzes
- Check-inAt the end of the module. A short check on “Get the tables: from regional databases to one flat table”, and how ready you feel; its answers show as soon as you submit.Check-in: Get the tables: from regional databases to one flat table ↗
The quizzes are on BNHR Quizzes and count toward nothing. Learning on your own, a quiz opens as the self-paced one; while a training runs, it asks which training you are in.