Skip to content
Learn Python with BNHR

Module 2 · Intermediate

Get the tables: from regional databases to one flat table

Module 2 of Python for Survey Statistics: every regional database read in one DuckDB query, codes kept whole, the database export read right, and one flat table saved as Parquet. Its sections, its notebooks to read on the web, and the companion's solutions and explanations.

Module 2Level Intermediate
On this page
  1. Overview
  2. What you do
  3. The module’s notebooks
  4. Quizzes
  5. Certification and support

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.

  1. One regional database into pandas and polars
  2. Every region in one query: ATTACH, UNION ALL BY NAME, deleted rows filtered
  3. Codes kept whole: text types, zero-padding, null tokens, nullable integers
  4. The flat table saved as Parquet, with a row-count check
  5. The real thing: reading a MySQL server with DuckDB's mysql extension

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

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.

Saved in this browser only

Certification and support

We offer additional resources, certificates, ongoing support, and other perks to those who join our instructor-led training.

Join an upcoming training to take this as an instructor-led course, or contact us to arrange one for your team or organization.

Something new

Identify results
LayerExplore a module
ModuleStore: rules that hold in the database
AboutModule 4 of Python for Data Management: a database whose rules come from the contract, loaded all or nothing. Its sections, its notebooks to read on the web, and the companion's solutions and explanations.
CoursePython for Data Management, free