Skip to content
Learn Python with BNHR

A new edition: Round 3 arrives

On this page
  1. Overview
  2. 2.1 · Which edition is each code in?
  3. 3.1 · Load the crosswalk
  4. 3.2 · Round 3 in 2023Q4 geography

Every exercise in 30_new_edition.py, with the solution and why it works. Each answer stays closed until you swear you tried it first. Opening one changes nothing in your notebook; your check still has to go green from your own code. Read the whole notebook, with its lessons.

Exercise 2.1. Which edition is each code in?

The task

Why it matters: a typing error is in no edition; a code from a new edition is in that edition. That one fact decides what to fix.

Input → output: a list of codes, and a dictionary of editions ({"2023Q4": {...codes...}, "2024Q2": {...}}) → a table with one row per code: code, and editions, the editions it is in, joined with ", ", or "none".

code editions
0100105000 2023Q4, 2024Q2
0100306000 2024Q2
0600399000 none

For each code, keep the edition names whose set contains it: [name for name, codes in editions.items() if code in codes]. Build one dictionary per row and return pd.DataFrame(rows).

If the check is red: common causes

what you see the usual cause look at
editions holds a list, like ['2024Q2'] The names were not joined into one text. ", ".join(found)
An empty text instead of none No edition was found, and nothing replaced the empty result. ... if found else "none"
KeyError The dictionary is looked up by code instead of looping over it. for name, codes in editions.items()

Still stuck after ten minutes? Ask your table, then raise your hand.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 3.1. Load the crosswalk

The task

Why it matters: the crosswalk comes from the code list's publisher, inside the new workbook. Reading it with the same care as the code list keeps every code intact.

Input → output: the path of the 2024Q2 workbook → a table with code_2024q2 and code_2023q4, both as 10-character text, one row per 2024Q2 code.

Read it as in the Get module's Exercise 3.1 (header=3, dtype=str), keep the rows whose Code is made of digits, keep the columns Code and Code (2023Q4 edition), rename them to code_2024q2 and code_2023q4, pad both to 10 characters (.str.zfill(10)), and drop duplicate rows.

If the check is red: common causes

what you see the usual cause look at
KeyError: 'Code (2023Q4 edition)' The column name is not exactly the workbook's. copy it from the workbook's header row
Codes have 9 characters The workbook stores codes as numbers. .str.zfill(10) on both columns
More rows than codes Blank rows or the note under the table are still there. keep the rows whose Code is digits

Still stuck after ten minutes? Ask your table, then raise your hand.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 3.2. Round 3 in 2023Q4 geography

The task

Why it matters: with every round on the same codes, a municipality's numbers can be followed from round to round.

Input → output: a round's rows (with area_code in 2024Q2 codes) and the crosswalk → the same rows, with a new column area_code_2023q4. A code that is not in the crosswalk (an unknown code you decided to keep) keeps its own value.

  1. left-join the rows to the crosswalk: left_on="area_code", right_on="code_2024q2", with validate="many_to_one";
  2. area_code_2023q4 is the crosswalk's code_2023q4, or, where that is missing, the row's own area_code: joined["code_2023q4"].fillna(joined["area_code"]);
  3. drop the crosswalk's two columns and return. The number of rows must not change.

If the check is red: common causes

what you see the usual cause look at
The unknown code has an empty area_code_2023q4 Rows not in the crosswalk were not given their own code. .fillna(joined["area_code"])
MergeError The crosswalk lists a 2024Q2 code twice. drop_duplicates() in Exercise 3.1
Extra columns The crosswalk's columns are still there. drop code_2024q2 and code_2023q4

Still stuck after ten minutes? Ask your table, then raise your hand.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Something new

Identify results
LayerExplore a module
ModuleFunctions
AboutModule 5 of Python Essentials: parameters, return values and defaults, scope, sorting with a key, lambda, map and filter, and reading a class. Its sections, its notebooks to read on the web, and the companion's solutions and explanations.
CoursePython Essentials, free