import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Start%20here%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20names%20every%20other%20cell%20uses.%0A%20%20%20%20import%20duckdb%20%20%23%20a%20database%20in%20one%20file%2C%20read%20with%20SQL%3A%20the%20survey's%20regional%20databases%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20polars%20as%20pl%20%20%23%20the%20same%20table%20work%20as%20pandas%2C%20written%20as%20expressions%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20where%20the%20files%20are%2C%20the%20survey's%20tables%2C%20the%20readiness%20check%2C%20the%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20REGIONS%2C%20UNIT_KEY%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20%23%20The%20survey's%20ten%20tables%3A%20the%20household%2C%20its%20selection%20row%20and%20members%2C%20its%20fishing%20units%20and%20their%20six%20child%20tables%0A%20%20%20%20TABLES%20%3D%20%5B%22household%22%2C%20%22selection%22%2C%20%22member%22%2C%20%22fishing_unit%22%2C%20*course.CHILD_PREFIX%5D%0A%20%20%20%20%23%20The%20catch%20table's%20two%20key%20parts%2C%20as%20the%20export%20names%20them%2C%20and%20the%20unit's%20names%20for%20them%0A%20%20%20%20CATCH_KEY%20%3D%20%7B%22catch_operator_line%22%3A%20%22operator_line%22%2C%20%22catch_unit_no%22%3A%20%22unit_no%22%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Start%20here%0A%0A%20%20%20%20**Where%20this%20is%20in%20processing%20a%20survey.**%20Before%20the%20first%20processing%20step.%20Every%20step%20after%20it%2C%20from%20bringing%20the%20regions%20together%20to%20publishing%20tables%2C%20rests%20on%20what%20this%20module%20meets%3A%20the%20survey's%20tables%20and%20keys%2C%20what%20its%20variable%20names%20say%2C%20the%20codebook%2C%20and%20three%20verbs%20R%20scripts%20use%20all%20the%20time%20(%60filter%60%2C%20%60mutate%60%2C%20%60inner_join%60)%2C%20in%20pandas%20and%20polars.%0A%0A%20%20%20%20Welcome%20to%20**Python%20for%20Survey%20Statistics**.%20In%20Arrays%20%26%20DataFrames%20you%20cleaned%2C%20combined%20and%20summarized%20one%20survey's%20tables%20in%20pandas%20and%20polars.%20This%20course%20takes%20a%20household%20survey%20through%20its%20processing%2C%20from%20its%20regional%20databases%20to%20published%20tables%2C%20and%20then%20looks%20for%20the%20records%20that%20don't%20belong.%20Each%20step%20of%20that%20processing%20has%20a%20module%20here%3A%0A%0A%20%20%20%20%7C%20module%20%7C%20what%20you%20do%20%7C%20the%20main%20tools%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%202%20%7C%20read%20the%20regional%20databases%20into%20one%20flat%20table%20%7C%20DuckDB%2C%20pandas%2C%20polars%2C%20Parquet%20%7C%0A%20%20%20%20%7C%203%20%7C%20match%20the%20keys%20of%20parent%20and%20child%20tables%20%7C%20%60merge%60%2C%20anti-joins%2C%20transactions%20%7C%0A%20%20%20%20%7C%204%20%7C%20write%20edit%20rules%20and%20find%20duplicates%20%7C%20rules%20as%20data%2C%20a%20failure%20report%20%7C%0A%20%20%20%20%7C%205%20%7C%20recode%20and%20tabulate%20%7C%20mapping%20tables%2C%20%60np.select%60%2C%20%60groupby%60%20%7C%0A%20%20%20%20%7C%206%20%7C%20estimate%20under%20the%20survey%20design%20%7C%20svy%20%7C%0A%20%20%20%20%7C%207%20%7C%20publish%20the%20tables%20%7C%20openpyxl%2C%20xlsxwriter%2C%20a%20re-runnable%20script%20%7C%0A%20%20%20%20%7C%208%20%7C%20find%20anomalies%20by%20rule%20and%20by%20statistics%20%7C%20pandas%2C%20polars%20%7C%0A%20%20%20%20%7C%209%20%7C%20find%20anomalies%20with%20machine%20learning%20%7C%20scikit-learn%2C%20PyOD%2C%20shap%20%7C%0A%0A%20%20%20%20After%20module%209%2C%20the%20stretch%20S4%2C%20%5Bagents%20for%20your%20pipeline%5D(%2F%3Ffile%3D86_stretch_agents.py)%2C%20is%20a%20showcase%20of%20what%20AI%20agents%20can%20do%20for%20a%20pipeline%20like%20this%20one%2C%20and%20of%20what%20each%20kind%20sends%20away.%0A%0A%20%20%20%20**One%20running%20case.**%20The%20Survey%20of%20Fishing%20Households%202025%20runs%20through%20the%20whole%20course%3A%20a%20sample%20survey%2C%20invented%20for%20the%20course%2C%20of%20households%20that%20fished%20in%202025%2C%20interviewed%20between%202%20March%20and%2015%20May%202026.%20It%20is%20kept%20as%20many%20surveys%20are%3A%20one%20database%20per%20region%2C%20for%20three%20regions%20(Region%20I%2C%20Region%20IV-A%20and%20Region%20VII%2C%20two%20provinces%20each)%2C%20with%20the%20same%20ten%20tables%20in%20each.%20The%20region%2C%20province%20and%20city%20or%20municipality%20codes%20and%20names%20are%20**real**%2C%20from%20the%20Philippine%20Standard%20Geographic%20Code%20(PSGC).%20The%20households%2C%20members%2C%20fishing%20units%2C%20boats%2C%20catch%2C%20weights%20and%20interviewers%20are%20**invented**%2C%20and%20so%20are%20the%20code%20lists%20other%20than%20geography%2C%20the%20sampling%20design%20and%20the%20survey's%20rules.%20The%20survey%20also%20carries%20defects%20planted%20for%20the%20lessons%20to%20find%3A%20keys%20that%20don't%20match%2C%20invalid%20codes%2C%20duplicates%20and%20a%20cluster%20of%20made-up%20interviews.%0A%0A%20%20%20%20**How%20the%20notebooks%20work.**%20As%20in%20Arrays%20%26%20DataFrames%3A%20work%20from%20top%20to%20bottom.%20Each%20exercise%20is%20a%20function%20you%20finish%2C%20ending%20in%20a%20%60raise%20NotImplementedError(...)%60%20line%20that%20you%20delete.%20Under%20it%2C%20a%20check%20runs%20your%20function%20on%20several%20inputs%20and%20turns%20**green**%20when%20every%20result%20is%20right%2C%20**red**%20with%20the%20first%20input%20that%20is%20not%2C%20or%20stays%20**grey**%20while%20the%20function%20is%20not%20written.%20A%20**Try%20it**%20cell%20calls%20your%20function%20on%20the%20survey%2C%20and%20a%20folded%20table%20lists%20the%20common%20causes%20of%20a%20red%20check.%20The%20companion%20on%20the%20Learn%20Python%20site%20has%20each%20exercise's%20solution%20and%20why%20it%20works.%20When%20something%20fails%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%0A%0A%20%20%20%20**Coming%20from%20R%3F**%20Many%20survey%20statisticians%20process%20their%20surveys%20in%20R.%20Where%20an%20R%20habit%20helps%2C%20the%20notebooks%20show%20it%20beside%20the%20Python%2C%20and%20section%203%20of%20this%20notebook%20is%20a%20translation%20table%20from%20the%20tidyverse%20to%20pandas%20and%20polars.%20Primer%20P1%2C%20%5Bfrom%20R%20to%20Python%5D(%2F%3Ffile%3D95_primer_r_to_python.py)%2C%20maps%20the%20rest%20of%20an%20R%20workflow.%20Primer%20P2%2C%20%5Bsurvey%20sampling%20in%20one%20page%5D(%2F%3Ffile%3D96_primer_survey_sampling.py)%2C%20recalls%20the%20design%20terms%20module%206%20uses.%0A%0A%20%20%20%20This%20module%20has%20three%20sections%3A%20get%20your%20laptop%20and%20the%20course%20folder%20ready%3B%20meet%20the%20survey%20through%20its%20tables%2C%20keys%20and%20codebook%3B%20and%20translate%20three%20steps%20of%20an%20R%20script%20to%20pandas%20and%20polars.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%221.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Your%20laptop%20and%20the%20course%20folder%20ready%0A%0A%20%20%20%20Nothing%20in%20the%20course%20works%20until%20three%20things%20are%20true%3A%20the%20Python%20that%20uv%20installed%20for%20the%20course%20has%20every%20package%20the%20notebooks%20import%2C%20the%20course%20folder%20is%20complete%20and%20somewhere%20you%20can%20save%20files%2C%20and%20the%20survey's%20data%20opens.%20The%20check%20below%20tests%20each%2C%20one%20line%20apiece.%20If%20a%20line%20says%20**no**%2C%20its%20last%20column%20says%20what%20to%20do%3A%20do%20it%2C%20then%20press%20**Check%20again**.%20If%20a%20line%20still%20says%20no%2C%20raise%20your%20hand%3B%20on%20your%20own%2C%20go%20through%20%60SETUP.md%60%20in%20the%20course%20folder%20again.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20recheck%20%3D%20mo.ui.run_button(label%3D%22Check%20again%22)%0A%20%20%20%20recheck%0A%20%20%20%20return%20(recheck%2C)%0A%0A%0A%40app.cell%0Adef%20_(recheck)%3A%0A%20%20%20%20%23%20The%20readiness%20check.%20Pressing%20%22Check%20again%22%20runs%20this%20cell%20again%2C%20because%20it%20uses%20%60recheck%60.%0A%20%20%20%20recheck%0A%20%20%20%20course.readiness()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20What%20each%20line%20checks%3A%0A%0A%20%20%20%20%7C%20line%20%7C%20what%20it%20checks%20%7C%20why%20the%20course%20needs%20it%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Python%203.12%20or%20newer%20%7C%20the%20Python%20that%20runs%20the%20notebooks.%20uv%20installs%20it%20for%20the%20course%20(3.14)%2C%20apart%20from%20any%20Python%20already%20on%20your%20laptop%20%7C%20the%20course's%20packages%20need%20a%20recent%20Python%20%7C%0A%20%20%20%20%7C%20the%20marimo%20package%20%7C%20marimo%20itself%20%7C%20the%20notebooks%20%7C%0A%20%20%20%20%7C%20numpy%2C%20pandas%2C%20polars%2C%20pyarrow%20%7C%20arrays%2C%20tables%2C%20and%20Apache%20Arrow%2C%20the%20column%20store%20pandas%20and%20polars%20share%20%7C%20every%20module%20%7C%0A%20%20%20%20%7C%20duckdb%20%7C%20DuckDB%3A%20a%20database%20kept%20in%20one%20file%20and%20read%20with%20SQL%2C%20with%20no%20server%20to%20install%20%7C%20the%20survey's%20regional%20databases%20%7C%0A%20%20%20%20%7C%20svy%20%7C%20survey%20estimation%3A%20totals%2C%20means%20and%20proportions%20with%20their%20standard%20errors%20under%20the%20design%20%7C%20module%206%20%7C%0A%20%20%20%20%7C%20sklearn%2C%20pyod%2C%20shap%20%7C%20machine%20learning%3A%20scikit-learn's%20detectors%20and%20classifiers%2C%20PyOD's%20ECOD%20detector%2C%20shap's%20explanations%20of%20a%20model's%20scores%20%7C%20module%209%20%7C%0A%20%20%20%20%7C%20openpyxl%2C%20xlsxwriter%20%7C%20reading%20and%20writing%20Excel%20workbooks%20%7C%20the%20codebook's%20workbook%2C%20and%20module%207's%20tables%20%7C%0A%20%20%20%20%7C%20matplotlib%2C%20seaborn%2C%20altair%20%7C%20the%20three%20chart%20libraries%20of%20Arrays%20%26%20DataFrames%20%7C%20charts%20throughout%20%7C%0A%20%20%20%20%7C%20typer%20%7C%20command-line%20programs%20in%20Python%20%7C%20module%207%20runs%20the%20tabulation%20as%20one%20script%20%7C%0A%20%20%20%20%7C%20the%20course%20folder%20%7C%20that%20%60modules.toml%60%2C%20%60lesson.py%60%20and%20%60checks.json%60%20are%20there%20%7C%20the%20agenda%20reads%20the%20first%3B%20the%20exercise%20checks%20read%20the%20other%20two%20%7C%0A%20%20%20%20%7C%20the%20survey's%20data%20opens%20%7C%20opens%20%60data%2F%7BPATHS.region(REGIONS%5B0%5D).name%7D%60%20read-only%20and%20counts%20its%20households%20%7C%20every%20notebook%20reads%20the%20survey%20%7C%0A%20%20%20%20%7C%20the%20course%20can%20save%20files%20here%20%7C%20writes%20a%20small%20file%20into%20the%20course%20folder%20and%20deletes%20it%20%7C%20modules%202%20and%207%20write%20Parquet%20files%20and%20workbooks%20%7C%0A%0A%20%20%20%20%23%23%23%20What%20the%20course%20folder%20holds%0A%0A%20%20%20%20%60course.PATHS%60%20knows%20where%20the%20course's%20files%20are%20on%20**your**%20laptop%2C%20wherever%20you%20put%20the%20folder%3A%20%60PATHS.data%60%20is%20the%20data%20folder%2C%20%60PATHS.region(%2201%22)%60%20region%2001's%20database%2C%20%60PATHS.codebook%60%20the%20codebook%20and%20%60PATHS.codes%60%20the%20folder%20of%20code%20lists.%20Every%20notebook%20finds%20its%20files%20through%20it.%20The%20data%20folder%20holds%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20folder_table()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20files%20in%20the%20course's%20data%20folder%2C%20as%20a%20Markdown%20table%3A%20one%20row%20per%20file%2C%20and%20one%20per%20folder%20of%20files.%22%22%22%0A%20%20%20%20about%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22sfh2025_r01.duckdb%22%3A%20%22region%2001's%20database%3A%20the%20survey's%20ten%20tables%20for%20Region%20I%20(section%202)%22%2C%0A%20%20%20%20%20%20%20%20%22sfh2025_r04.duckdb%22%3A%20%22region%2004's%20database%2C%20the%20same%20ten%20tables%20for%20Region%20IV-A%22%2C%0A%20%20%20%20%20%20%20%20%22sfh2025_r07.duckdb%22%3A%20%22region%2007's%20database%2C%20the%20same%20ten%20tables%20for%20Region%20VII%22%2C%0A%20%20%20%20%20%20%20%20%22export%22%3A%20%22the%20same%20tables%20as%20the%20database%20export%20writes%20them%3A%20Windows-1252%20text%2C%20%60%5C%5CN%60%20for%20a%20missing%20value%20(module%202)%22%2C%0A%20%20%20%20%20%20%20%20%22codes%22%3A%20%22the%20code%20lists%3A%20geography%20(real%20PSGC)%2C%20species%2C%20gears%2C%20education%2C%20relationship%20(section%202)%22%2C%0A%20%20%20%20%20%20%20%20%22codebook.csv%22%3A%20%22every%20table's%20variables%3A%20type%2C%20label%2C%20codes%20and%20source%20(section%202)%22%2C%0A%20%20%20%20%20%20%20%20%22codebook.xlsx%22%3A%20%22the%20same%20codebook%20as%20an%20Excel%20workbook%22%2C%0A%20%20%20%20%20%20%20%20%22codebook_rules.csv%22%3A%20%22the%20survey's%20validation%20rules%2C%20in%20words%20(module%204)%22%2C%0A%20%20%20%20%20%20%20%20%22stratum_counts.csv%22%3A%20%22each%20province's%20strata%20and%20their%20numbers%20of%20enumeration%20areas%2C%20in%20the%20frame%20and%20in%20the%20sample%2C%20for%20the%20finite%20population%20correction%20(module%206)%22%2C%0A%20%20%20%20%20%20%20%20%22planted_defects.csv%22%3A%20%22every%20defect%20the%20course%20planted%2C%20to%20judge%20a%20check%20or%20a%20detector%20by%3B%20a%20real%20survey%20has%20no%20such%20list%20(modules%208%20and%209)%22%2C%0A%20%20%20%20%20%20%20%20%22units_2025_all_regions.parquet%22%3A%20%22fishing%20units%20of%20every%20region%2C%20one%20row%20each%2C%20too%20large%20to%20read%20carelessly%20(module%209)%22%2C%0A%20%20%20%20%20%20%20%20%22units_2025_all_regions_defects.csv%22%3A%20%22the%20defects%20planted%20in%20that%20table%22%2C%0A%20%20%20%20%20%20%20%20%22units_2021_edit_decisions.parquet%22%3A%20%22the%20survey's%202021%20round%2C%20with%20what%20its%20editors%20decided%3A%20labels%20for%20module%209's%20classifier%22%2C%0A%20%20%20%20%20%20%20%20%22batch2%22%3A%20%22a%20second%20batch%20of%20the%20survey%2C%20from%20two%20other%20regions%2C%20for%20the%20capstone%22%2C%0A%20%20%20%20%20%20%20%20%22README.md%22%3A%20%22where%20the%20data%20comes%20from%2C%20and%20which%20of%20it%20is%20real%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20path%20in%20sorted(PATHS.data.iterdir())%3A%0A%20%20%20%20%20%20%20%20if%20path.name.startswith(%22.%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20if%20path.is_dir()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20files%20%3D%20%5Bp%20for%20p%20in%20path.rglob(%22*%22)%20if%20p.is_file()%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20size%20%3D%20sum(p.stat().st_size%20for%20p%20in%20files)%20%2F%201024%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(f%22%7C%20%60data%2F%7Bpath.name%7D%2F%60%20%7C%20%7Bsize%3A%2C.0f%7D%20KB%20in%20%7Blen(files)%7D%20files%20%7C%20%7Babout.get(path.name%2C%20'')%7D%20%7C%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(f%22%7C%20%60data%2F%7Bpath.name%7D%60%20%7C%20%7Bpath.stat().st_size%20%2F%201024%3A%2C.0f%7D%20KB%20%7C%20%7Babout.get(path.name%2C%20'')%7D%20%7C%22)%0A%20%20%20%20return%20%22%7C%20file%20%7C%20size%20%7C%20what%20it%20holds%20%7C%5Cn%7C%3A--%7C--%3A%7C%3A--%7C%5Cn%22%20%2B%20%22%5Cn%22.join(rows)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_notebooks%20%3D%20sorted(p.name%20for%20p%20in%20PATHS.root.glob(%22%5B0-9%5D*.py%22))%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20folder_table()%0A%20%20%20%20%20%20%20%20%2B%20%22%5Cn%5CnBeside%20the%20data%20folder%20are%20the%20notebooks%2C%20numbered%20in%20the%20order%20you%20use%20them%20(%22%0A%20%20%20%20%20%20%20%20%2B%20%22%2C%20%22.join(f%22%60%7Bn%7D%60%22%20for%20n%20in%20_notebooks)%0A%20%20%20%20%20%20%20%20%2B%20%22)%3B%20%60checks.json%60%2C%20what%20the%20exercise%20checks%20compare%20your%20results%20with%2C%20stored%20so%20that%20the%20answers%20cannot%20be%20read%20from%20it%3B%20and%20%60course.py%60%20and%20%60lesson.py%60%2C%20the%20helpers%20the%20notebooks%20share%2C%20which%20you%20do%20not%20need%20to%20open.%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20PATHS.region(%2201%22)%20%20%23%20region%2001's%20database%2C%20on%20this%20laptop%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20The%20survey%3A%20its%20tables%2C%20keys%20and%20codebook%0A%0A%20%20%20%20**The%20problem.**%20A%20survey%20is%20not%20one%20table.%20Its%20answers%20sit%20in%20ten%20tables%2C%20linked%20by%20keys%20of%20several%20parts%2C%20with%20codes%20in%20place%20of%20words%20and%20removed%20records%20still%20in%20the%20files.%20Join%20two%20tables%20on%20the%20wrong%20key%20and%20rows%20multiply.%20Count%20without%20the%20removal%20marks%20and%20removed%20households%20are%20counted.%20Read%20a%20code%20as%20an%20amount%20and%20a%20mean%20comes%20out%20wrong.%20No%20error%20message%20says%20so%20in%20any%20of%20these%20cases.%20This%20section%20answers%20five%20questions%20before%20any%20computing%3A%20where%20the%20tables%20are%2C%20what%20one%20row%20of%20each%20is%2C%20which%20columns%20are%20its%20key%2C%20what%20its%20codes%20mean%2C%20and%20which%20rows%20are%20removed.%0A%0A%20%20%20%20%23%23%23%20Three%20regions%2C%20three%20databases%0A%0A%20%20%20%20The%20survey%20is%20kept%20the%20way%20many%20surveys%20are%3A%20one%20database%20per%20region%2C%20each%20holding%20the%20same%20ten%20tables.%20Each%20database%20is%20a%20**DuckDB**%20file%3A%20a%20whole%20database%20in%20one%20file%2C%20read%20with%20SQL.%20Module%202%20reads%20them%20properly%3B%20here%2C%20the%20course%20opens%20each%20one%20read-only%20and%20counts%20what%20is%20in%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20%23%20Each%20region's%20database%2C%20opened%20read-only%3A%20its%20tables%2C%20its%20households%2C%20and%20how%20it%20stores%20the%20area%20codes%0A%20%20%20%20_geography%20%3D%20course.codes(%22geography%22)%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_reg%20in%20REGIONS%3A%0A%20%20%20%20%20%20%20%20with%20duckdb.connect(str(PATHS.region(_reg))%2C%20read_only%3DTrue)%20as%20_con%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_tables%20%3D%20_con.execute(%22SELECT%20count(*)%20FROM%20duckdb_tables()%22).fetchone()%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_households%20%3D%20_con.execute(%22SELECT%20count(*)%20FROM%20household%22).fetchone()%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_stored%20%3D%20_con.execute(%22SELECT%20data_type%20FROM%20duckdb_columns()%20WHERE%20table_name%20%3D%20'household'%20AND%20column_name%20%3D%20'region_code'%22).fetchone()%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_first%20%3D%20_con.execute(%22SELECT%20region_code%2C%20province_code%20FROM%20household%20ORDER%20BY%20row_id%20LIMIT%201%22).fetchone()%0A%20%20%20%20%20%20%20%20_here%20%3D%20_geography%5B_geography%5B%22region_code%22%5D%20%3D%3D%20_reg%5D%0A%20%20%20%20%20%20%20%20_rows.append(%7B%22region_code%22%3A%20_reg%2C%20%22region%22%3A%20_here%5B%22region%22%5D.iloc%5B0%5D%2C%20%22provinces%22%3A%20%22%2C%20%22.join(_here%5B%22province%22%5D.unique())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22file%22%3A%20PATHS.region(_reg).name%2C%20%22tables%22%3A%20_tables%2C%20%22households%22%3A%20_households%2C%20%22area%20codes%20stored%20as%22%3A%20_stored%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22first%20household's%20region%20and%20province%22%3A%20f%22%7B_first%5B0%5D%7D%2C%20%7B_first%5B1%5D%7D%22%7D)%0A%20%20%20%20regions%20%3D%20pd.DataFrame(_rows)%0A%20%20%20%20regions%0A%20%20%20%20return%20(regions%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Open%20one%20region's%20database%20read-only%20and%20list%20its%20tables.%20SHOW%20TABLES%20is%20SQL%3B%20DuckDB%20runs%20it%2C%20and%20.df()%20returns%20a%20pandas%20table%0A%20%20%20%20with%20duckdb.connect(str(PATHS.region(%2201%22))%2C%20read_only%3DTrue)%20as%20_con%3A%0A%20%20%20%20%20%20%20%20region01_tables%20%3D%20_con.execute(%22SHOW%20TABLES%22).df()%0A%20%20%20%20region01_tables%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(regions)%3A%0A%20%20%20%20_text%20%3D%20regions%5Bregions%5B%22area%20codes%20stored%20as%22%5D%20%3D%3D%20%22VARCHAR%22%5D%0A%20%20%20%20_numbers%20%3D%20regions%5Bregions%5B%22area%20codes%20stored%20as%22%5D%20!%3D%20%22VARCHAR%22%5D%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20Two%20facts%20in%20that%20table%20carry%20through%20the%20course.%20First%2C%20each%20database%20numbers%20its%20households%20on%20its%20own%2C%20so%20the%20same%20household%20id%20can%20be%20a%20different%20household%20in%20another%20region%3A%20the%20keys%20below%20deal%20with%20it.%20Second%2C%20the%20databases%20do%20not%20agree%20on%20types.%20Region%20%7B%22%2C%20%22.join(_text%5B%22region_code%22%5D)%7D%20stores%20its%20area%20codes%20as%20zero-padded%20**text**%20(its%20first%20household%3A%20%7B_text%5B%22first%20household's%20region%20and%20province%22%5D.iloc%5B0%5D%7D)%2C%20and%20regions%20%7B%22%20and%20%22.join(_numbers%5B%22region_code%22%5D)%7D%20store%20them%20as%20**numbers**%20(%7B_numbers%5B%22first%20household's%20region%20and%20province%22%5D.iloc%5B0%5D%7D).%20Stacked%20as%20they%20come%2C%20one%20region's%20%6001%60%20and%20another's%20%601%60%20are%20two%20different%20values.%20%60course.table()%60%20gives%20every%20region's%20rows%20in%20one%20pandas%20table%2C%20with%20the%20codes%20as%20zero-padded%20text%3B%20module%202%20shows%20how%20to%20do%20that%20yourself.%0A%0A%20%20%20%20%23%23%23%20Ten%20tables%2C%20nested%0A%0A%20%20%20%20Every%20table%20hangs%20from%20%60household%60%3A%20one%20row%20per%20household%20interviewed.%20Two%20tables%20hang%20from%20the%20household%20with%20it%3A%20%60selection%60%2C%20its%20row%20in%20the%20sample%20(its%20stratum%2C%20its%20area%20and%20its%20weights)%2C%20and%20%60member%60%2C%20its%20roster.%20So%20do%20its%20fishing%20units.%20Each%20unit%20has%20six%20child%20tables%20of%20its%20own%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.mermaid(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20flowchart%20LR%0A%20%20%20%20%20%20%20%20%20%20%20%20hh%5B%22household%3Cbr%2F%3Eone%20row%20per%20household%20interviewed%22%5D%20--%3E%20sel%5B%22selection%3Cbr%2F%3Eits%20stratum%2C%20area%20and%20weights%3A%20one%20row%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20hh%20--%3E%20mem%5B%22member%3Cbr%2F%3Eone%20row%20per%20member%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20hh%20--%3E%20unit%5B%22fishing_unit%3Cbr%2F%3Eone%20row%20per%20fishing%20unit%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20unit%20--%3E%20crew%5B%22crew%3Cbr%2F%3Eone%20row%20per%20person%20who%20worked%20in%20it%20besides%20its%20operator%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20unit%20--%3E%20boat%5B%22boat%3Cbr%2F%3Eone%20row%20per%20boat%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20unit%20--%3E%20gear%5B%22gear%3Cbr%2F%3Eone%20row%20per%20gear%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20unit%20--%3E%20fish%5B%22catch%3Cbr%2F%3Eone%20row%20per%20species%20caught%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20unit%20--%3E%20land%5B%22landing%3Cbr%2F%3Eone%20row%20per%20landing%20site%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20unit%20--%3E%20aid%5B%22assistance%3Cbr%2F%3Eone%20row%20per%20item%20of%20help%20received%22%5D%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20ten%20tables%2C%20every%20region's%20rows%20in%20one%20pandas%20table%20each%2C%20as%20the%20databases%20hold%20them%3A%20clean%3DFalse%20keeps%20the%0A%20%20%20%20%23%20removed%20households%20and%20voided%20rows.%20course.table()%20adds%20region_code%20to%20the%20tables%20that%20lack%20it%2C%20and%20makes%20the%20area%0A%20%20%20%20%23%20codes%20zero-padded%20text.%0A%20%20%20%20survey%20%3D%20%7Bname%3A%20course.table(name%2C%20clean%3DFalse)%20for%20name%20in%20TABLES%7D%0A%20%20%20%20return%20(survey%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey)%3A%0A%20%20%20%20_parent%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22household%22%3A%20(%22a%20household%20interviewed%22%2C%20%22(the%20top%20of%20the%20tree)%22)%2C%0A%20%20%20%20%20%20%20%20%22selection%22%3A%20(%22the%20household's%20row%20in%20the%20sample%3A%20its%20stratum%2C%20area%20and%20weights%22%2C%20%22%60hh_id%60%22)%2C%0A%20%20%20%20%20%20%20%20%22member%22%3A%20(%22a%20member%20of%20the%20household%22%2C%20%22%60hh_id%60%22)%2C%0A%20%20%20%20%20%20%20%20%22fishing_unit%22%3A%20(%22a%20fishing%20unit%20the%20household%20runs%22%2C%20%22%60hh_id%60%22)%2C%0A%20%20%20%20%20%20%20%20%22crew%22%3A%20(%22a%20person%20who%20worked%20in%20the%20unit%20besides%20its%20operator%22%2C%20%22%60hh_id%60%20and%20the%20unit's%20key%20parts%2C%20%60crew_%60%22)%2C%0A%20%20%20%20%20%20%20%20%22boat%22%3A%20(%22a%20boat%20the%20unit%20used%22%2C%20%22%60hh_id%60%20and%20the%20unit's%20key%20parts%2C%20%60boat_%60%22)%2C%0A%20%20%20%20%20%20%20%20%22gear%22%3A%20(%22a%20fishing%20gear%20the%20unit%20used%22%2C%20%22%60hh_id%60%20and%20the%20unit's%20key%20parts%2C%20%60gear_%60%22)%2C%0A%20%20%20%20%20%20%20%20%22catch%22%3A%20(%22a%20species%20the%20unit%20caught%22%2C%20%22%60hh_id%60%20and%20the%20unit's%20key%20parts%2C%20%60catch_%60%22)%2C%0A%20%20%20%20%20%20%20%20%22landing%22%3A%20(%22a%20landing%20site%20the%20unit%20used%22%2C%20%22%60hh_id%60%20and%20the%20unit's%20key%20parts%2C%20%60land_%60%22)%2C%0A%20%20%20%20%20%20%20%20%22assistance%22%3A%20(%22an%20item%20of%20help%20the%20unit%20received%22%2C%20%22%60hh_id%60%20and%20the%20unit's%20key%20parts%2C%20%60aid_%60%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20_counts%20%3D%20%7Bname%3A%20survey%5Bname%5D%5B%22region_code%22%5D.value_counts()%20for%20name%20in%20TABLES%7D%0A%20%20%20%20mo.md(%22%7C%20table%20%7C%20one%20row%20is%20%7C%20linked%20to%20its%20parent%20by%20%7C%20%22%20%2B%20%22%20%7C%20%22.join(f%22rows%20in%20%7Br%7D%22%20for%20r%20in%20REGIONS)%20%2B%20%22%20%7C%5Cn%7C%3A--%7C%3A--%7C%3A--%7C%22%20%2B%20%22--%3A%7C%22%20*%20len(REGIONS)%20%2B%20%22%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%2B%20%22%5Cn%22.join(f%22%7C%20%60%7Bname%7D%60%20%7C%20%7B_parent%5Bname%5D%5B0%5D%7D%20%7C%20%7B_parent%5Bname%5D%5B1%5D%7D%20%7C%20%22%20%2B%20%22%20%7C%20%22.join(f%22%7B_counts%5Bname%5D.get(r%2C%200)%3A%2C%7D%22%20for%20r%20in%20REGIONS)%20%2B%20%22%20%7C%22%20for%20name%20in%20TABLES))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Pick%20a%20household%20to%20see%20its%20rows%20in%20every%20table.%20The%20household%20shown%20first%20is%20one%20with%20more%20than%20one%20fishing%20unit.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20household_region%20%3D%20mo.ui.dropdown(options%3DREGIONS%2C%20value%3D%2201%22%2C%20label%3D%22Region%20(region_code)%3A%22)%0A%20%20%20%20household_region%0A%20%20%20%20return%20(household_region%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(household_region%2C%20survey)%3A%0A%20%20%20%20_units%20%3D%20survey%5B%22fishing_unit%22%5D%0A%20%20%20%20_here%20%3D%20_units%5B_units%5B%22region_code%22%5D%20%3D%3D%20household_region.value%5D%0A%20%20%20%20_busy%20%3D%20_here.groupby(%22hh_id%22).size()%0A%20%20%20%20_ids%20%3D%20survey%5B%22household%22%5D.loc%5Bsurvey%5B%22household%22%5D%5B%22region_code%22%5D%20%3D%3D%20household_region.value%2C%20%22hh_id%22%5D.astype(str).tolist()%0A%20%20%20%20household_pick%20%3D%20mo.ui.dropdown(options%3D_ids%2C%20value%3Dstr(_busy%5B_busy%20%3E%3D%202%5D.index%5B0%5D)%2C%20searchable%3DTrue%2C%20label%3D%22Household%20(hh_id)%3A%22)%0A%20%20%20%20household_pick%0A%20%20%20%20return%20(household_pick%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(household_pick%2C%20household_region%2C%20survey)%3A%0A%20%20%20%20_tabs%20%3D%20%7B%7D%0A%20%20%20%20for%20_name%20in%20TABLES%3A%0A%20%20%20%20%20%20%20%20_table%20%3D%20survey%5B_name%5D%0A%20%20%20%20%20%20%20%20_rows%20%3D%20_table%5B(_table%5B%22region_code%22%5D%20%3D%3D%20household_region.value)%20%26%20(_table%5B%22hh_id%22%5D%20%3D%3D%20int(household_pick.value))%5D%0A%20%20%20%20%20%20%20%20_tabs%5Bf%22%7B_name%7D%20(%7Blen(_rows)%7D)%22%5D%20%3D%20_rows%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22Household%20**%7Bhousehold_pick.value%7D**%20of%20region%20**%7Bhousehold_region.value%7D**%2C%20table%20by%20table%20(the%20number%20of%20its%20rows%20in%20brackets)%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.ui.tabs(_tabs)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20One%20household%2C%20many%20rows%3A%20its%20household%20and%20selection%20rows%20once%20each%2C%20a%20row%20per%20member%2C%20a%20row%20per%20fishing%20unit%2C%20and%20under%20each%20unit%20its%20crew%2C%20boats%2C%20gears%2C%20species%2C%20landing%20sites%20and%20help%20received.%20In%20the%20databases%20only%20%60household%60%20holds%20the%20region%3B%20%60course.table()%60%20adds%20%60region_code%60%20to%20every%20other%20table's%20rows%2C%20because%20a%20household%20id%20is%20unique%20only%20within%20its%20region.%0A%0A%20%20%20%20%23%23%23%20The%20names%3A%20a%20prefix%20for%20the%20table%2C%20then%20what%20the%20column%20holds%0A%0A%20%20%20%20Every%20name%20in%20the%20survey%20is%20in%20lower%20case%2C%20with%20its%20words%20joined%20by%20%60_%60%2C%20and%20most%20start%20with%20a%20short%20prefix%20that%20says%20their%20table.%20The%20codebook%20lists%20every%20variable%20with%20its%20table%2C%20so%20the%20prefixes%20can%20be%20read%20from%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_codebook%20%3D%20pd.read_csv(PATHS.codebook)%0A%20%20%20%20_prefix%20%3D%20%7B%22household%22%3A%20%22hh%22%2C%20%22selection%22%3A%20%22sel%22%2C%20%22member%22%3A%20%22mem%22%2C%20%22fishing_unit%22%3A%20%22unit%22%2C%20**course.CHILD_PREFIX%7D%0A%20%20%20%20_tables_of%20%3D%20_codebook.groupby(%22variable%22)%5B%22table%22%5D.nunique()%0A%20%20%20%20_everywhere%20%3D%20_tables_of%5B_tables_of%20%3D%3D%20len(TABLES)%5D.index.tolist()%20%20%23%20the%20columns%20every%20table%20has%0A%20%20%20%20_rows%2C%20_others%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20for%20_name%20in%20TABLES%3A%0A%20%20%20%20%20%20%20%20_names%20%3D%20_codebook.loc%5B(_codebook%5B%22table%22%5D%20%3D%3D%20_name)%20%26%20~_codebook%5B%22variable%22%5D.isin(_everywhere)%2C%20%22variable%22%5D%0A%20%20%20%20%20%20%20%20_own%20%3D%20_names%5B_names.str.startswith(_prefix%5B_name%5D%20%2B%20%22_%22)%5D%0A%20%20%20%20%20%20%20%20_answers%20%3D%20_own%5B~_own.str.endswith((%22operator_line%22%2C%20%22unit_no%22))%5D%20%20%23%20examples%20beyond%20the%20key%20parts%0A%20%20%20%20%20%20%20%20_rows.append(f%22%7C%20%60%7B_prefix%5B_name%5D%7D_%60%20%7C%20%60%7B_name%7D%60%20%7C%20%7Blen(_own)%7D%20of%20%7Blen(_names)%7D%20%7C%20%7B'%2C%20'.join(f'%60%7Bv%7D%60'%20for%20v%20in%20_answers.head(2))%7D%20%7C%22)%0A%20%20%20%20%20%20%20%20_others%20%2B%3D%20%5Bf%22%60%7Bv%7D%60%20in%20%60%7B_name%7D%60%22%20for%20v%20in%20_names%5B~_names.str.startswith(_prefix%5B_name%5D%20%2B%20%22_%22)%5D%5D%0A%20%20%20%20mo.md(%22%7C%20prefix%20%7C%20table%20%7C%20its%20variables%20with%20the%20prefix%20%7C%20for%20example%20%7C%5Cn%7C%3A--%7C%3A--%7C--%3A%7C%3A--%7C%5Cn%22%20%2B%20%22%5Cn%22.join(_rows)%0A%20%20%20%20%20%20%20%20%20%20%2B%20f%22%5Cn%5CnThe%20counts%20leave%20out%20the%20%7Blen(_everywhere)%7D%20columns%20every%20table%20has%2C%20which%20carry%20no%20table's%20prefix%3A%20%7B'%2C%20'.join(f'%60%7Bv%7D%60'%20for%20v%20in%20_everywhere)%7D.%20%22%0A%20%20%20%20%20%20%20%20%20%20%2B%20f%22%60hh_id%60%20is%20the%20household's%20id%2C%20the%20link%20from%20every%20table%20to%20its%20household%3B%20%60row_id%60%20is%20the%20row's%20own%20id%2C%20and%20%60row_void%60%20its%20void%20mark.%20The%20other%20names%20without%20their%20table's%20prefix%3A%20%7B'%2C%20'.join(_others)%7D.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20prefix%20keeps%20its%20meaning%20after%20a%20join%3A%20%60boat_length%60%20and%20%60unit_boats%60%20cannot%20be%20mistaken%20for%20each%20other%2C%20nor%20for%20a%20column%20of%20another%20table.%20The%20columns%20every%20table%20has%20are%20the%20exception%3A%20a%20join%20that%20keeps%20both%20tables'%20columns%20gets%20two%20%60row_id%60%20columns%2C%20which%20pandas%20names%20%60row_id_x%60%20and%20%60row_id_y%60%2C%20and%20two%20of%20%60row_void%60.%20Bring%20only%20the%20columns%20you%20need%20from%20the%20second%20table%2C%20as%20Exercise%201.4%20does.%20The%20stretch%20S1%2C%20%5Bprocessing%5D(%2F%3Ffile%3D36_stretch_processing.py)%2C%20has%20an%20exercise%20that%20reads%20any%20variable's%20table%20from%20its%20prefix.%0A%0A%20%20%20%20The%20codebook's%20%60label%60%20says%20what%20each%20variable%20holds%2C%20and%20its%20%60source%60%20says%20where%20the%20variable%20comes%20from%3A%20the%20**questionnaire**%20(the%20household's%20answers)%2C%20the%20**sample%20file**%20(the%20sampling%20side's%20columns%20of%20%60selection%60%2C%20and%20the%20members%20counted%20when%20the%20area%20was%20listed)%2C%20or%20the%20**capture%20system**%20(ids%2C%20the%20interview's%20date%20and%20length%2C%20and%20the%20removal%20marks).%0A%0A%20%20%20%20**Brackets%20and%20quotes.**%20No%20name%20in%20this%20survey%20needs%20quoting.%20In%20pandas%2C%20write%20a%20column%20in%20brackets%20and%20quotes%20anyway%2C%20%60units%5B%22unit_scale%22%5D%60%2C%20even%20though%20%60units.unit_scale%60%20works%20here%3A%20the%20bracket%20form%20works%20for%20every%20name%2C%20and%20the%20dot%20form%20breaks%20on%20a%20name%20with%20a%20hyphen%20or%20a%20space%2C%20and%20on%20a%20name%20that%20is%20also%20a%20method's.%20Exercise%201.3%20adds%20a%20column%20named%20%60gt%60%2C%20for%20one%3A%20%60boats.gt%60%20is%20then%20pandas'%20method%20that%20compares%20the%20table%20with%20a%20value%2C%20not%20the%20column.%20SQL%20wants%20a%20name%20with%20a%20hyphen%20in%20double%20quotes%20(%60%22hh-id%22%60)%2C%20and%20R%20in%20backticks%3B%20module%202%20writes%20table%20names%20in%20double%20quotes%20for%20the%20same%20reason.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey)%3A%0A%20%20%20%20units_raw%20%3D%20survey%5B%22fishing_unit%22%5D%20%20%23%20every%20fishing%20unit%2C%20as%20the%20databases%20hold%20them%0A%20%20%20%20units_raw%5BUNIT_KEY%20%2B%20%5B%22unit_scale%22%2C%20%22unit_boats%22%5D%5D.head(3)%20%20%23%20in%20brackets%20and%20quotes%2C%20whatever%20the%20name%0A%20%20%20%20return%20(units_raw%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey%2C%20units_raw)%3A%0A%20%20%20%20_households%20%3D%20survey%5B%22household%22%5D%0A%20%20%20%20_ids%20%3D%20_households.groupby(%22hh_id%22)%5B%22region_code%22%5D.nunique()%0A%20%20%20%20_shared%20%3D%20int((_ids%20%3E%201).sum())%0A%20%20%20%20_per_household%20%3D%20units_raw.groupby(%5B%22region_code%22%2C%20%22hh_id%22%5D)%0A%20%20%20%20_several%20%3D%20int((_per_household.size()%20%3E%201).sum())%0A%20%20%20%20_two_runners%20%3D%20int((_per_household%5B%22operator_line%22%5D.nunique()%20%3E%201).sum())%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20Keys%3A%20a%20household%20is%20its%20region%20and%20its%20id%0A%0A%20%20%20%20A%20**key**%20is%20the%20set%20of%20columns%20that%20names%20one%20row.%20Each%20database%20numbers%20its%20households%20from%20its%20own%20start%2C%20so%20%60hh_id%60%20is%20unique%20only%20within%20its%20region%3A%20**%7B_shared%3A%2C%7D%20household%20ids**%20appear%20in%20more%20than%20one%20region's%20database.%20A%20household%20is%20%60region_code%60%20**and**%20%60hh_id%60.%0A%0A%20%20%20%20A%20fishing%20unit's%20key%20has%20four%20parts%3A%20the%20household%20(%60region_code%60%2C%20%60hh_id%60)%2C%20the%20roster%20line%20of%20the%20member%20who%20runs%20it%20(%60operator_line%60)%2C%20and%20the%20unit's%20number%20among%20that%20member's%20units%20(%60unit_no%60).%20Two%20units%20of%20one%20household%20differ%20in%20their%20line%20when%20two%20members%20each%20run%20one%2C%20and%20in%20their%20number%20when%20one%20member%20runs%20two%3A%20%60unit_no%60%201%20and%202%20on%20the%20same%20line.%20%7B_several%3A%2C%7D%20households%20have%20more%20than%20one%20unit%2C%20%7B_two_runners%3A%2C%7D%20of%20them%20with%20units%20run%20by%20different%20members.%20%60course.UNIT_KEY%60%20holds%20the%20four%20names%3A%20%60%7BUNIT_KEY%7D%60.%0A%0A%20%20%20%20The%20child%20tables%20carry%20the%20unit's%20two%20key%20parts%20with%20their%20own%20short%20name%20in%20front%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(%22%7C%20child%20table%20%7C%20its%20key%20parts%20%7C%5Cn%7C%3A--%7C%3A--%7C%5Cn%22%20%2B%20%22%5Cn%22.join(%0A%20%20%20%20%20%20%20%20f%22%7C%20%60%7Bname%7D%60%20%7C%20%60hh_id%60%2C%20%60%7Bp%7D_operator_line%60%2C%20%60%7Bp%7D_unit_no%60%20%7C%22%20for%20name%2C%20p%20in%20course.CHILD_PREFIX.items()))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20join%20of%20the%20catch%20to%20its%20units%20must%20pair%20%60catch_operator_line%60%20with%20%60operator_line%60%20and%20%60catch_unit_no%60%20with%20%60unit_no%60%2C%20by%20name.%20%60course.child(name)%60%20renames%20them%20to%20the%20unit's%20names%2C%20so%20that%20a%20child%20table%20joins%20on%20%60UNIT_KEY%60%3B%20section%203%20does%20the%20renaming%20itself.%0A%0A%20%20%20%20**The%20R%20habit%20of%20pasting%20keys.**%20An%20R%20script%20often%20pastes%20the%20parts%20into%20one%20text%20key%20and%20joins%20on%20that.%20In%20pandas%20and%20polars%20you%20join%20on%20the%20list%20of%20columns%2C%20with%20nothing%20to%20paste.%20Pasting%20without%20a%20separator%20can%20also%20make%20two%20different%20keys%20one%20text%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Region%20%221%22%20with%20household%20%2223%22%2C%20and%20region%20%2212%22%20with%20household%20%223%22%3A%20two%20households%2C%20one%20pasted%20key%0A%20%20%20%20str(1)%20%2B%20str(23)%20%3D%3D%20str(12)%20%2B%20str(3)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**A%20key%20names%20one%20row%2C%20or%20it%20is%20not%20a%20key.**%20Before%20you%20join%20on%20one%2C%20count%20the%20rows%20that%20share%20it%3A%20%60table.duplicated(subset%3Dkey%2C%20keep%3DFalse).sum()%60%20is%200%20for%20a%20key%20that%20names%20one%20row.%20Module%204%20finds%20the%20survey's%20repeated%20keys%20and%20decides%20what%20to%20do%20with%20them%3B%20the%20stretch%20S1%2C%20%5Bprocessing%5D(%2F%3Ffile%3D36_stretch_processing.py)%2C%20builds%20this%20count%20as%20an%20exercise%20and%20runs%20it%20on%20every%20table.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20codebook%20and%20the%20code%20lists%0A%0A%20%20%20%20The%20codebook%2C%20%60data%2Fcodebook.csv%60%2C%20has%20one%20row%20per%20variable%20of%20every%20table%3A%0A%0A%20%20%20%20%7C%20codebook%20column%20%7C%20what%20it%20says%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60table%60%20%7C%20which%20table%20the%20variable%20is%20in%20%7C%0A%20%20%20%20%7C%20%60variable%60%20%7C%20the%20column's%20name%20in%20that%20table%2C%20exactly%20as%20you%20type%20it%20in%20code%20%7C%0A%20%20%20%20%7C%20%60type%60%20%7C%20how%20the%20database%20stores%20it%3A%20%60INTEGER%60%2C%20%60DOUBLE%60%20(a%20decimal)%2C%20%60VARCHAR%60%20(text)%20or%20%60DATE%60%20%7C%0A%20%20%20%20%7C%20%60label%60%20%7C%20what%20the%20variable%20holds%2C%20in%20words%20%7C%0A%20%20%20%20%7C%20%60values%60%20%7C%20the%20codes%20and%20what%20each%20means%20(%601%20%3D%20Male%3B%202%20%3D%20Female%60)%2C%20a%20unit%2C%20or%20the%20code%20list%20to%20look%20in%20%7C%0A%20%20%20%20%7C%20%60source%60%20%7C%20where%20the%20variable%20comes%20from%3A%20the%20questionnaire%2C%20the%20sample%20file%20or%20the%20capture%20system%20%7C%0A%0A%20%20%20%20Pick%20a%20table%20to%20see%20its%20variables%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20codebook_table%20%3D%20mo.ui.dropdown(options%3DTABLES%2C%20value%3D%22catch%22%2C%20label%3D%22Table%3A%22)%0A%20%20%20%20codebook_table%0A%20%20%20%20return%20(codebook_table%2C)%0A%0A%0A%40app.cell%0Adef%20_(codebook_table)%3A%0A%20%20%20%20codebook%20%3D%20pd.read_csv(PATHS.codebook)%20%20%23%20every%20table's%20variables%2C%20one%20row%20each%0A%20%20%20%20codebook%5Bcodebook%5B%22table%22%5D%20%3D%3D%20codebook_table.value%5D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_species%2C%20_education%2C%20_geography%20%3D%20course.codes(%22species%22)%2C%20course.codes(%22education%22)%2C%20course.codes(%22geography%22)%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20Where%20a%20variable%20has%20more%20codes%20than%20fit%20in%20a%20cell%2C%20its%20%60values%60%20says%20which%20file%20in%20%60data%2Fcodes%2F%60%20lists%20them.%20The%20species%20list%20is%20the%20course's%20own%20(%7Blen(_species)%7D%20codes%2C%20from%20%60%7B_species%5B%22code%22%5D.iloc%5B0%5D%7D%60%20%7B_species%5B%22name%22%5D.iloc%5B0%5D%7D%20on%2C%20in%20%7B_species%5B%22group%22%5D.nunique()%7D%20groups)%3B%20so%20are%20the%20gears%2C%20the%20education%20levels%20(%7Blen(_education)%7D%20codes%20in%20%7B_education%5B%22group%22%5D.nunique()%7D%20groups)%20and%20the%20relationship%20to%20the%20head.%20The%20geography%20list%20holds%20the%20real%20PSGC%20codes%20and%20names%20of%20the%20cities%20and%20municipalities%20of%20%7B_geography%5B%22province%22%5D.nunique()%7D%20provinces%3A%20the%20survey's%20and%20the%20second%20batch's.%20%60course.codes(name)%60%20reads%20one%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20five%20code%20lists%2C%20one%20tab%20each%0A%20%20%20%20mo.ui.tabs(%7Bname%3A%20course.codes(name)%20for%20name%20in%20%5B%22species%22%2C%20%22gears%22%2C%20%22education%22%2C%20%22relationship%22%2C%20%22geography%22%5D%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20codebook%20has%20a%20companion%2C%20%60data%2Fcodebook_rules.csv%60%3A%20the%20survey's%20validation%20rules%2C%20in%20words%2C%20each%20named%20by%20its%20table's%20letter%20and%20a%20number%20(%60M1%60%20is%20the%20first%20rule%20on%20members).%20Module%204%20writes%20them%20as%20code.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20pd.read_csv(PATHS.rules)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20code%20is%20valid%20only%20if%20it%20is%20on%20its%20list%2C%20and%20a%20survey's%20tables%20hold%20codes%20that%20are%20not%3A%20a%20sex%20coded%208%2C%20a%20species%20code%20no%20list%20has.%20Finding%20them%20is%20the%20first%20edit%20check%2C%20which%20module%204%20builds%3B%20the%20next%20exercise%20is%20its%20core.%0A%0A%20%20%20%20%23%23%23%20Exercise%201.1.%20Codes%20that%20are%20not%20on%20the%20list%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20code%20outside%20its%20list%20is%20a%20keying%20slip%20or%20a%20code%20the%20list%20forgot.%20Either%20way%20an%20editor%20must%20see%20it%20before%20tabulating%2C%20or%20a%20table%20gets%20a%20row%20nobody%20can%20label.%20A%20blank%20is%20a%20different%20thing%3A%20the%20education%20level%20is%20blank%20for%20members%20under%20six%2C%20because%20the%20question%20is%20not%20asked%20of%20them.%20Reported%20as%20an%20invalid%20code%2C%20every%20young%20child%20would%20become%20an%20edit%20to%20make.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20column%20of%20codes%20and%20the%20list%20of%20valid%20codes%20%E2%86%92%20the%20codes%20in%20the%20column%20that%20are%20not%20on%20the%20list%2C%20each%20once%2C%20smallest%20first%2C%20with%20blanks%20(missing%20values)%20left%20out.%20%60%5B%5D%60%20when%20every%20code%20is%20valid.%0A%0A%20%20%20%20%7C%20%60column%60%20%7C%20%60valid%60%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%5B1%2C%202%2C%209%2C%20%3CNA%3E%2C%203%2C%209%5D%60%20%7C%20%60%5B1%2C%202%5D%60%20%7C%20%60%5B3%2C%209%5D%60%20%7C%0A%20%20%20%20%7C%20%60%5B1%2C%202%2C%202%2C%201%5D%60%20%7C%20%60%5B1%2C%202%5D%60%20%7C%20%60%5B%5D%60%20%7C%0A%0A%20%20%20%20**Tools**%20(Arrays%20%26%20DataFrames%2C%20modules%203%20and%205)%3A%20%60column.dropna()%60%20leaves%20out%20the%20blanks%3B%20%60column.isin(valid)%60%20is%20%60True%60%20where%20a%20value%20is%20on%20the%20list%2C%20and%20%60~%60%20turns%20each%20%60True%60%20to%20%60False%60%20and%20back%3B%20%60.unique()%60%3B%20%60.tolist()%60%3B%20%60sorted(...)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20codes_outside(column%3A%20pd.Series%2C%20valid%3A%20list)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22The%20codes%20in%20a%20column%20that%20are%20not%20in%20the%20list%20of%20valid%20codes%3A%20each%20once%2C%20smallest%20first%2C%20blanks%20left%20out.%22%22%22%0A%20%20%20%20%23%20Exercise%201.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20leave%20out%20the%20blanks%0A%20%20%20%20%23%20Step%202%3A%20keep%20the%20values%20that%20are%20not%20in%20%60valid%60%0A%20%20%20%20%23%20Step%203%3A%20return%20each%20of%20them%20once%2C%20as%20a%20sorted%20list%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.1%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey)%3A%0A%20%20%20%20_members%2C%20_catch%2C%20_boats%20%3D%20survey%5B%22member%22%5D%2C%20survey%5B%22catch%22%5D%2C%20survey%5B%22boat%22%5D%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%201.1%22%2C%20codes_outside%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22%5B1%2C%202%2C%209%2C%20%3CNA%3E%2C%203%2C%209%5D%20against%20%5B1%2C%202%5D%22%2C%20(pd.Series(%5B1%2C%202%2C%209%2C%20None%2C%203%2C%209%5D%2C%20dtype%3D%22Int32%22)%2C%20%5B1%2C%202%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22%5B1%2C%202%2C%202%2C%201%5D%20against%20%5B1%2C%202%5D%22%2C%20(pd.Series(%5B1%2C%202%2C%202%2C%201%5D%2C%20dtype%3D%22Int32%22)%2C%20%5B1%2C%202%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20members'%20sex%2C%20mem_sex%2C%20against%201%20and%202%22%2C%20(_members%5B%22mem_sex%22%5D%2C%20%5B1%2C%202%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20members'%20education%2C%20mem_education%20(blank%20under%206)%2C%20against%20codes%2Feducation.csv%22%2C%20(_members%5B%22mem_education%22%5D%2C%20course.codes(%22education%22)%5B%22code%22%5D.tolist()))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20catch's%20species%2C%20catch_species%2C%20against%20codes%2Fspecies.csv%22%2C%20(_catch%5B%22catch_species%22%5D%2C%20course.codes(%22species%22)%5B%22code%22%5D.tolist()))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20boats'%20engine%2C%20boat_engine%2C%20against%200%2C%201%20and%202%22%2C%20(_boats%5B%22boat_engine%22%5D%2C%20%5B0%2C%201%2C%202%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22columns%22%2C%20hint%3D%22Drop%20the%20blanks%20first%3B%20keep%20the%20values%20not%20in%20valid%20(~%20and%20isin)%3B%20each%20once%2C%20sorted.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20members'%20education%20against%20its%20code%20list.%20Every%20member%20under%206%20has%20it%20blank.%0A%20%20%20%20try_it(codes_outside%2C%20survey%5B%22member%22%5D%5B%22mem_education%22%5D%2C%20course.codes(%22education%22)%5B%22code%22%5D.tolist())%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%201.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*TypeError%3A%20boolean%20value%20of%20NA%20is%20ambiguous*%20%7C%20A%20blank%20reached%20%60sorted%60%2C%20which%20cannot%20compare%20it%20with%20a%20number.%20%7C%20%60column.dropna()%60%20before%20anything%20else%20%7C%0A%20%20%20%20%7C%20%60%5B3%2C%209%2C%20None%5D%60%20or%20a%20blank%20in%20the%20list%20%7C%20The%20blanks%20were%20kept%3A%20%60isin%60%20is%20%60False%60%20for%20a%20blank%2C%20so%20%60~%60%20keeps%20it.%20%7C%20%60column.dropna()%60%20%7C%0A%20%20%20%20%7C%20%60%5B9%2C%203%5D%60%20%7C%20Each%20code%20once%2C%20but%20in%20the%20order%20they%20first%20appear.%20%7C%20%60sorted(...)%60%20%7C%0A%20%20%20%20%7C%20%60%5B9%2C%203%2C%209%5D%60%20%7C%20One%20entry%20per%20row%2C%20not%20per%20code.%20%7C%20%60.unique()%60%20%7C%0A%20%20%20%20%7C%20%60%5B1%2C%202%5D%60%3A%20the%20valid%20codes%20%7C%20The%20%60~%60%20is%20missing%2C%20so%20the%20values%20on%20the%20list%20are%20kept.%20%7C%20%60~present.isin(valid)%60%20%7C%0A%20%20%20%20%7C%20A%20column%20of%20values%20shown%2C%20not%20a%20list%20%7C%20The%20filtered%20column%20was%20returned%20as%20it%20is.%20%7C%20%60.unique().tolist()%60%2C%20then%20%60sorted%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey)%3A%0A%20%20%20%20_members%2C%20_catch%2C%20_boats%2C%20_units%20%3D%20survey%5B%22member%22%5D%2C%20survey%5B%22catch%22%5D%2C%20survey%5B%22boat%22%5D%2C%20survey%5B%22fishing_unit%22%5D%0A%20%20%20%20_tests%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22%60mem_relation%60%2C%20relationship%20to%20the%20head%22%2C%20_members%5B%22mem_relation%22%5D%2C%20course.codes(%22relationship%22)%5B%22code%22%5D.tolist()%2C%20%22%60codes%2Frelationship.csv%60%22)%2C%0A%20%20%20%20%20%20%20%20(%22%60mem_sex%60%2C%20sex%22%2C%20_members%5B%22mem_sex%22%5D%2C%20%5B1%2C%202%5D%2C%20%221%2C%202%22)%2C%0A%20%20%20%20%20%20%20%20(%22%60mem_education%60%2C%20education%22%2C%20_members%5B%22mem_education%22%5D%2C%20course.codes(%22education%22)%5B%22code%22%5D.tolist()%2C%20%22%60codes%2Feducation.csv%60%22)%2C%0A%20%20%20%20%20%20%20%20(%22%60unit_ownership%60%2C%20who%20owns%20the%20unit%22%2C%20_units%5B%22unit_ownership%22%5D%2C%20%5B10%2C%2020%2C%2030%2C%2040%2C%2090%5D%2C%20%2210%2C%2020%2C%2030%2C%2040%2C%2090%22)%2C%0A%20%20%20%20%20%20%20%20(%22%60boat_hull%60%2C%20kind%20of%20hull%22%2C%20_boats%5B%22boat_hull%22%5D%2C%20%5B1%2C%202%2C%203%5D%2C%20%221%2C%202%2C%203%22)%2C%0A%20%20%20%20%20%20%20%20(%22%60boat_engine%60%2C%20kind%20of%20engine%22%2C%20_boats%5B%22boat_engine%22%5D%2C%20%5B0%2C%201%2C%202%5D%2C%20%220%2C%201%2C%202%22)%2C%0A%20%20%20%20%20%20%20%20(%22%60catch_species%60%2C%20species%20caught%22%2C%20_catch%5B%22catch_species%22%5D%2C%20course.codes(%22species%22)%5B%22code%22%5D.tolist()%2C%20%22%60codes%2Fspecies.csv%60%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20_found%20%3D%20%5Battempt(codes_outside%2C%20column%2C%20valid)%20for%20_%2C%20column%2C%20valid%2C%20_%20in%20_tests%5D%0A%20%20%20%20mo.stop(any(f%20is%20NOT_YET%20for%20f%20in%20_found)%2C%20waiting_for(%22Exercise%201.1%22))%0A%20%20%20%20mo.md(%22**Your%20function%20on%20the%20survey's%20coded%20variables%3A**%5Cn%5Cn%7C%20variable%20%7C%20valid%20codes%20%7C%20blanks%20%7C%20codes%20outside%20the%20list%20%7C%5Cn%7C%3A--%7C%3A--%7C--%3A%7C%3A--%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%2B%20%22%5Cn%22.join(f%22%7C%20%7Bshown%7D%20%7C%20%7Blisted%7D%20%7C%20%7Bint(column.isna().sum())%3A%2C%7D%20%7C%20%7B'%2C%20'.join(f'%60%7Bc%7D%60'%20for%20c%20in%20found)%20or%20'none'%7D%20%7C%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20(shown%2C%20column%2C%20_%2C%20listed)%2C%20found%20in%20zip(_tests%2C%20_found)))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Removed%20households%20and%20voided%20rows%0A%0A%20%20%20%20The%20databases%20keep%20what%20was%20removed.%20A%20mark%20says%20so%2C%20nothing%20is%20taken%20out%2C%20and%20every%20count%2C%20join%20and%20estimate%20must%20leave%20the%20marked%20records%20out%3A%0A%0A%20%20%20%20%7C%20mark%20%7C%20in%20%7C%20means%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60hh_deleted_on%60%20filled%20(a%20date)%20%7C%20%60household%60%20%7C%20the%20household%20was%20deleted%20in%20the%20capture%20system%2C%20on%20that%20day%20%7C%0A%20%20%20%20%7C%20%60row_void%20%3D%201%60%20%7C%20%60household%60%20%7C%20the%20household%20was%20voided%20in%20editing%20%7C%0A%20%20%20%20%7C%20%60sel_excluded%20%3D%201%60%20%7C%20%60selection%60%20%7C%20the%20sampling%20side%20left%20the%20household%20out%20of%20estimation%20%7C%0A%20%20%20%20%7C%20%60row_void%20%3D%201%60%20%7C%20every%20other%20table%20%7C%20that%20row%20was%20voided%20in%20editing%20%7C%0A%0A%20%20%20%20A%20removed%20household%20takes%20its%20rows%20in%20every%20table%20with%20it%3B%20a%20voided%20row%20takes%20only%20itself.%20%60row_void%60%20and%20%60sel_excluded%60%20are%20%601%60%20or%20blank%2C%20never%20%600%60%2C%20so%20a%20filter%20that%20keeps%20%60row_void%20%3D%3D%200%60%20keeps%20nothing%3B%20%60hh_deleted_on%60%20is%20a%20date%20or%20blank.%20Section%203%20shows%20what%20%60row_void%20!%3D%201%60%20does.%0A%0A%20%20%20%20%60course.table(name)%60%20leaves%20the%20removed%20households%20and%20the%20voided%20rows%20out%20(%60clean%3DTrue%60%2C%20the%20default).%20%60course.table(name%2C%20clean%3DFalse)%60%20keeps%20every%20row%2C%20as%20this%20section's%20%60survey%60%20does.%20Module%202%20writes%20that%20filter%20itself%2C%20in%20one%20query%20across%20the%20three%20databases.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey)%3A%0A%20%20%20%20_rows%2C%20_left_out%2C%20_own_mark%20%3D%20%5B%5D%2C%200%2C%200%0A%20%20%20%20for%20_name%20in%20TABLES%3A%0A%20%20%20%20%20%20%20%20_kept%20%3D%20len(course.table(_name))%0A%20%20%20%20%20%20%20%20_voided%20%3D%20int(survey%5B_name%5D%5B%22row_void%22%5D.eq(1).sum())%0A%20%20%20%20%20%20%20%20_rows.append(f%22%7C%20%60%7B_name%7D%60%20%7C%20%7Blen(survey%5B_name%5D)%3A%2C%7D%20%7C%20%7B_voided%3A%2C%7D%20%7C%20%7B_kept%3A%2C%7D%20%7C%20%7Blen(survey%5B_name%5D)%20-%20_kept%3A%2C%7D%20%7C%22)%0A%20%20%20%20%20%20%20%20if%20_name%20not%20in%20(%22household%22%2C%20%22selection%22)%3A%20%20%23%20in%20these%20two%2C%20the%20marks%20are%20the%20household's%20own%0A%20%20%20%20%20%20%20%20%20%20%20%20_left_out%2C%20_own_mark%20%3D%20_left_out%20%2B%20len(survey%5B_name%5D)%20-%20_kept%2C%20_own_mark%20%2B%20_voided%0A%20%20%20%20mo.md(%22%7C%20table%20%7C%20rows%20stored%20%7C%20rows%20with%20%60row_void%20%3D%201%60%20%7C%20rows%20%60course.table()%60%20keeps%20%7C%20left%20out%20%7C%5Cn%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%5Cn%22%20%2B%20%22%5Cn%22.join(_rows)%0A%20%20%20%20%20%20%20%20%20%20%2B%20f%22%5Cn%5CnIn%20the%20%7Blen(TABLES)%20-%202%7D%20tables%20under%20the%20household%2C%20%7B_left_out%3A%2C%7D%20rows%20are%20left%20out%20and%20only%20%7B_own_mark%3A%2C%7D%20of%20them%20carry%20%60row_void%20%3D%201%60.%20The%20others%20belong%20to%20removed%20households%3A%20their%20own%20mark%20is%20blank%2C%20and%20only%20the%20household's%20marks%2C%20in%20%60household%60%20and%20%60selection%60%2C%20say%20they%20are%20gone.%20A%20filter%20on%20%60row_void%60%20alone%2C%20table%20by%20table%2C%20keeps%20them.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Before%20you%20analyze%3A%20the%20survey%20in%20one%20table%0A%0A%20%20%20%20%7C%20question%20%7C%20the%20survey's%20answer%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Where%20are%20the%20tables%3F%20%7C%20three%20DuckDB%20files%2C%20%60data%2Fsfh2025_r01.duckdb%60%2C%20%60sfh2025_r04.duckdb%60%20and%20%60sfh2025_r07.duckdb%60%2C%20each%20with%20the%20same%20ten%20tables%20%7C%0A%20%20%20%20%7C%20What%20is%20one%20row%3F%20%7C%20in%20%60household%60%2C%20a%20household%3B%20in%20%60selection%60%2C%20its%20row%20in%20the%20sample%3B%20in%20%60member%60%2C%20a%20member%3B%20in%20%60fishing_unit%60%2C%20a%20fishing%20unit%3B%20in%20each%20child%20table%2C%20one%20crew%20member%2C%20boat%2C%20gear%2C%20species%2C%20landing%20site%20or%20item%20of%20help%20of%20a%20unit%20%7C%0A%20%20%20%20%7C%20Which%20columns%20are%20the%20key%3F%20%7C%20a%20household%3A%20%60region_code%60%20and%20%60hh_id%60%3B%20a%20fishing%20unit%3A%20the%20four%20parts%20of%20%60UNIT_KEY%60%3B%20a%20child%20row%3A%20the%20unit's%20key%20parts%2C%20with%20the%20table's%20short%20name%20in%20front%20%7C%0A%20%20%20%20%7C%20What%20do%20the%20names%20mean%3F%20%7C%20a%20prefix%20for%20the%20table%2C%20then%20what%20the%20column%20holds%3B%20the%20codebook's%20%60label%60%20says%20it%20in%20words%2C%20and%20its%20%60source%60%20where%20the%20variable%20comes%20from%20%7C%0A%20%20%20%20%7C%20What%20do%20the%20codes%20mean%3F%20%7C%20the%20codebook's%20%60values%60%2C%20and%20the%20code%20lists%20in%20%60data%2Fcodes%2F%60%3B%20a%20code%20outside%20its%20list%20is%20an%20error%20to%20edit%2C%20a%20blank%20is%20not%20%7C%0A%20%20%20%20%7C%20Which%20rows%20count%3F%20%7C%20not%20the%20removed%20or%20voided%20ones%3A%20%60hh_deleted_on%60%2C%20%60row_void%60%20and%20%60sel_excluded%60%20%7C%0A%20%20%20%20%7C%20Does%20every%20household%20count%20the%20same%3F%20%7C%20no%3A%20%60sel_final_weight%60%2C%20in%20%60selection%60%2C%20is%20the%20final%20weight%2C%20how%20many%20households%20one%20interview%20stands%20for%20(module%206)%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20From%20the%20tidyverse%20to%20pandas%20and%20polars%3A%20filter%2C%20mutate%2C%20inner_join%0A%0A%20%20%20%20**The%20problem.**%20An%20R%20script%20for%20a%20survey%20like%20this%20one%20reads%20each%20region's%20tables%20with%20DBI%2C%20then%20works%20with%20dplyr%3A%20%60filter%60%20the%20rows%2C%20%60mutate%60%20new%20columns%2C%20%60inner_join%60%20child%20tables%20to%20their%20parents.%20Most%20of%20that%20has%20a%20direct%20match%20in%20pandas%20and%20in%20polars%2C%20and%20the%20code%20reads%20almost%20line%20for%20line.%20A%20few%20habits%20break%2C%20and%20they%20break%20without%20an%20error%3A%20a%20comparison%20with%20a%20blank%2C%20a%20condition%20without%20brackets%2C%20a%20join%20on%20columns%20with%20different%20names.%20This%20section%20translates%20the%20verbs%2C%20then%20takes%20three%20steps%20of%20one%20question%20in%20all%20three.%0A%0A%20%20%20%20**The%20question%3A**%20which%20species%20do%20the%20large-scale%20fishing%20units%20catch%3F%20A%20unit%20is%20**large-scale**%20when%20one%20of%20its%20boats%20is%205.0%20gross%20tons%20or%20more%20(%60unit_scale%20%3D%202%60)%20and%20**small-scale**%20otherwise%20(%601%60).%0A%0A%20%20%20%201.%20**filter**%20the%20units%20to%20the%20large-scale%20ones%20that%20are%20not%20voided%3B%0A%20%20%20%202.%20**mutate**%3A%20compute%20each%20boat's%20gross%20tonnage%2C%20the%20measure%20that%20makes%20a%20unit%20large-scale%3B%0A%20%20%20%203.%20**inner_join**%20the%20catch%20to%20the%20large-scale%20units.%0A%0A%20%20%20%20%23%23%23%20The%20translation%20table%0A%0A%20%20%20%20%7C%20to%20%7C%20tidyverse%20(dplyr)%20%7C%20pandas%20%7C%20polars%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20read%20a%20table%20from%20a%20database%20%7C%20%60collect(tbl(con%2C%20%22household%22))%60%20%7C%20%60con.execute(%22SELECT%20*%20FROM%20household%22).df()%60%20%7C%20%60con.execute(...).pl()%60%20%7C%0A%20%20%20%20%7C%20keep%20columns%20%7C%20%60select(region_code%2C%20unit_scale)%60%20%7C%20%60x%5B%5B%22region_code%22%2C%20%22unit_scale%22%5D%5D%60%20%7C%20%60x.select(%22region_code%22%2C%20%22unit_scale%22)%60%20%7C%0A%20%20%20%20%7C%20rename%20%7C%20%60rename(operator_line%20%3D%20catch_operator_line)%60%20%7C%20%60x.rename(columns%3D%7B%22catch_operator_line%22%3A%20%22operator_line%22%7D)%60%20%7C%20%60x.rename(%7B%22catch_operator_line%22%3A%20%22operator_line%22%7D)%60%20%7C%0A%20%20%20%20%7C%20keep%20rows%20%7C%20%60filter(unit_scale%20%3D%3D%202%2C%20unit_sold_share%20!%3D%200)%60%20%7C%20%60x%5B(x%5B%22unit_scale%22%5D%20%3D%3D%202)%20%26%20(x%5B%22unit_sold_share%22%5D%20!%3D%200)%5D%60%20%7C%20%60x.filter(pl.col(%22unit_scale%22)%20%3D%3D%202%2C%20pl.col(%22unit_sold_share%22)%20!%3D%200)%60%20%7C%0A%20%20%20%20%7C%20is%20in%20a%20list%20%7C%20%60region_code%20%25in%25%20c(%2201%22%2C%20%2207%22)%60%20%7C%20%60x%5B%22region_code%22%5D.isin(%5B%2201%22%2C%20%2207%22%5D)%60%20%7C%20%60pl.col(%22region_code%22).is_in(%5B%2201%22%2C%20%2207%22%5D)%60%20%7C%0A%20%20%20%20%7C%20is%20blank%20%7C%20%60is.na(row_void)%60%20%7C%20%60x%5B%22row_void%22%5D.isna()%60%20%7C%20%60pl.col(%22row_void%22).is_null()%60%20%7C%0A%20%20%20%20%7C%20add%20a%20column%20%7C%20%60mutate(age5%20%3D%20mem_age%20%25%2F%25%205%20*%205)%60%20%7C%20%60x.assign(age5%3Dx%5B%22mem_age%22%5D%20%2F%2F%205%20*%205)%60%20%7C%20%60x.with_columns(age5%3Dpl.col(%22mem_age%22)%20%2F%2F%205%20*%205)%60%20%7C%0A%20%20%20%20%7C%20a%20column%20by%20cases%20%7C%20%60case_when(c1%20~%20v1%2C%20c2%20~%20v2)%60%20%7C%20%60np.select(%5Bc1%2C%20c2%5D%2C%20%5Bv1%2C%20v2%5D%2C%20default%3Dnp.nan)%60%20%7C%20%60pl.when(c1).then(v1).when(c2).then(v2)%60%20%7C%0A%20%20%20%20%7C%20a%20column%20from%20two%20cases%20%7C%20%60if_else(c%2C%20a%2C%20b)%60%20%7C%20%60np.where(c%2C%20a%2C%20b)%60%20%7C%20%60pl.when(c).then(a).otherwise(b)%60%20%7C%0A%20%20%20%20%7C%20join%2C%20matching%20rows%20only%20%7C%20%60inner_join(y%2C%20by%20%3D%20join_by(a%20%3D%3D%20b))%60%20%7C%20%60x.merge(y%2C%20left_on%3D%22a%22%2C%20right_on%3D%22b%22%2C%20how%3D%22inner%22)%60%20%7C%20%60x.join(y%2C%20left_on%3D%22a%22%2C%20right_on%3D%22b%22%2C%20how%3D%22inner%22)%60%20%7C%0A%20%20%20%20%7C%20join%2C%20every%20row%20of%20x%20%7C%20%60left_join(y%2C%20by%20%3D%20...)%60%20%7C%20%60how%3D%22left%22%60%20%7C%20%60how%3D%22left%22%60%20%7C%0A%20%20%20%20%7C%20rows%20of%20x%20with%20no%20match%20in%20y%20%7C%20%60anti_join(y%2C%20by%20%3D%20...)%60%20%7C%20%60how%3D%22left_anti%22%60%20(pandas%203)%20%7C%20%60how%3D%22anti%22%60%20%7C%0A%20%20%20%20%7C%20summarize%20by%20group%20%7C%20%60group_by(region_code)%60%2C%20then%20%60summarise(n%20%3D%20n()%2C%20species%20%3D%20n_distinct(catch_species))%60%20%7C%20%60x.groupby(%22region_code%22).agg(n%3D(%22catch_species%22%2C%20%22size%22)%2C%20species%3D(%22catch_species%22%2C%20%22nunique%22))%60%20%7C%20%60x.group_by(%22region_code%22).agg(n%3Dpl.len()%2C%20species%3Dpl.col(%22catch_species%22).n_unique())%60%20%7C%0A%20%20%20%20%7C%20count%20rows%20by%20group%20%7C%20%60count(region_code%2C%20unit_scale)%60%20%7C%20%60x.groupby(%5B%22region_code%22%2C%20%22unit_scale%22%5D).size()%60%20%7C%20%60x.group_by(%22region_code%22%2C%20%22unit_scale%22).len()%60%20%7C%0A%20%20%20%20%7C%20sort%2C%20largest%20first%20%7C%20%60arrange(desc(unit_boats))%60%20%7C%20%60x.sort_values(%22unit_boats%22%2C%20ascending%3DFalse)%60%20%7C%20%60x.sort(%22unit_boats%22%2C%20descending%3DTrue%2C%20nulls_last%3DTrue)%60%20%7C%0A%20%20%20%20%7C%20distinct%20rows%20%7C%20%60distinct(region_code%2C%20hh_id)%60%20%7C%20%60x.drop_duplicates(%5B%22region_code%22%2C%20%22hh_id%22%5D)%60%20%7C%20%60x.unique(%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20maintain_order%3DTrue)%60%20%7C%0A%20%20%20%20%7C%20number%20of%20rows%20%7C%20%60nrow(x)%60%20%7C%20%60len(x)%60%20%7C%20%60x.height%60%20%7C%0A%20%20%20%20%7C%20one%20column%20%7C%20%60pull(mem_age)%60%20%7C%20%60x%5B%22mem_age%22%5D%60%20%7C%20%60x%5B%22mem_age%22%5D%60%20%7C%0A%0A%20%20%20%20R's%20pipe%2C%20%60x%20%7C%3E%20f()%20%7C%3E%20g()%60%2C%20is%20a%20**method%20chain**%20in%20Python%3A%20%60x.f().g()%60.%20A%20chain%20that%20runs%20over%20several%20lines%20goes%20inside%20round%20brackets%2C%20so%20Python%20reads%20it%20as%20one%20expression.%0A%0A%20%20%20%20%23%23%23%20The%20same%20task%2C%20in%20all%20three%0A%0A%20%20%20%20Pick%20a%20task.%20The%20R%20code%20is%20shown%20as%20an%20R%20script%20would%20write%20it%20after%20%60collect()%60%3B%20the%20pandas%20and%20the%20polars%20code%20run%20here%2C%20on%20the%20survey%20(%60course.table()%60's%20clean%20tables%2C%20turned%20into%20polars%20with%20%60pl.from_pandas%60)%2C%20and%20the%20code%20shown%20is%20the%20code%20that%20runs.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20tables%20the%20side-by-side%20tasks%20run%20on%2C%20in%20both%20libraries%3A%20as%20exported%20(the%20catch%20keeps%20its%20catch_%20key%20parts)%2C%0A%20%20%20%20%23%20removed%20households%20and%20voided%20rows%20out%2C%20and%20the%20units%20without%20the%20copy%20of%20a%20unit%20entered%20twice%20(Exercise%201.4%20shows%20why)%0A%20%20%20%20side_pd%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22units%22%3A%20course.units().drop_duplicates(subset%3DUNIT_KEY)%2C%0A%20%20%20%20%20%20%20%20%22boats%22%3A%20course.table(%22boat%22)%2C%0A%20%20%20%20%20%20%20%20%22catch%22%3A%20course.table(%22catch%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20side_pl%20%3D%20%7Bname%3A%20pl.from_pandas(table)%20for%20name%2C%20table%20in%20side_pd.items()%7D%0A%20%20%20%20return%20side_pd%2C%20side_pl%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20%23%20Each%20task%3A%20the%20R%20code%2C%20the%20pandas%20code%2C%20the%20polars%20code%2C%20and%20what%20to%20notice.%20The%20pandas%20and%20polars%20code%20is%20the%20code%20that%20runs.%0A%20%20%20%20SIDE_BY_SIDE%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22filter%3A%20large-scale%20units%2C%20not%20voided%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22units%20%7C%3E%5Cn%20%20filter(unit_scale%20%3D%3D%202%2C%20is.na(row_void)%20%7C%20row_void%20!%3D%201)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'units%5B(units%5B%22unit_scale%22%5D%20%3D%3D%202)%20%26%20(units%5B%22row_void%22%5D.isna()%20%7C%20(units%5B%22row_void%22%5D%20!%3D%201))%5D'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'units.filter(pl.col(%22unit_scale%22)%20%3D%3D%202%2C%20pl.col(%22row_void%22).is_null()%20%7C%20(pl.col(%22row_void%22)%20!%3D%201))'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22In%20dplyr's%20and%20polars'%20%60filter%60%2C%20a%20comma%20means%20*and*.%20pandas%20needs%20%60%26%60%2C%20with%20brackets%20around%20each%20comparison.%20All%20three%20drop%20a%20row%20whose%20condition%20comes%20out%20blank%2C%20which%20is%20why%20a%20blank%20%60row_void%60%20needs%20its%20own%20test%3A%20%60is.na(row_void)%20%7C%60%2C%20%60.isna()%20%7C%60%2C%20%60.is_null()%20%7C%60.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22mutate%3A%20each%20boat's%20gross%20tonnage%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22boats%20%7C%3E%5Cn%20%20mutate(gt%20%3D%20case_when(%5Cn%20%20%20%20boat_measured_in%20%3D%3D%201%20~%20boat_tonnage_reported%2C%5Cn%20%20%20%20boat_measured_in%20%3D%3D%202%20~%20boat_length%20*%20boat_beam%20*%20boat_depth%20*%200.212%2C%5Cn%20%20%20%20boat_measured_in%20%3D%3D%203%20~%20boat_length%20*%20boat_beam%20*%20boat_depth%20*%200.006%5Cn%20%20))%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'boats.assign(gt%3Dnp.select(%5Cn%20%20%20%20%5Bboats%5B%22boat_measured_in%22%5D%20%3D%3D%201%2C%20boats%5B%22boat_measured_in%22%5D%20%3D%3D%202%2C%20boats%5B%22boat_measured_in%22%5D%20%3D%3D%203%5D%2C%5Cn%20%20%20%20%5Bboats%5B%22boat_tonnage_reported%22%5D%2C%5Cn%20%20%20%20%20boats%5B%22boat_length%22%5D%20*%20boats%5B%22boat_beam%22%5D%20*%20boats%5B%22boat_depth%22%5D%20*%200.212%2C%5Cn%20%20%20%20%20boats%5B%22boat_length%22%5D%20*%20boats%5B%22boat_beam%22%5D%20*%20boats%5B%22boat_depth%22%5D%20*%200.006%5D%2C%5Cn%20%20%20%20default%3Dnp.nan%2C%5Cn))'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'boats.with_columns(gt%3Dpl.when(pl.col(%22boat_measured_in%22)%20%3D%3D%201).then(pl.col(%22boat_tonnage_reported%22))%5Cn%20%20%20%20.when(pl.col(%22boat_measured_in%22)%20%3D%3D%202).then(pl.col(%22boat_length%22)%20*%20pl.col(%22boat_beam%22)%20*%20pl.col(%22boat_depth%22)%20*%200.212)%5Cn%20%20%20%20.when(pl.col(%22boat_measured_in%22)%20%3D%3D%203).then(pl.col(%22boat_length%22)%20*%20pl.col(%22boat_beam%22)%20*%20pl.col(%22boat_depth%22)%20*%200.006))'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%60case_when%60%20has%20two%20Python%20forms%3A%20%60np.select%60%20takes%20the%20conditions%20and%20the%20values%20as%20two%20lists%2C%20in%20the%20same%20order%3B%20%60pl.when%60%20chains%20them.%20A%20row%20no%20condition%20matches%20gets%20%60NA%60%20in%20R%2C%20the%20%60default%60%20from%20%60np.select%60%20(here%20%60NaN%60%2C%20missing)%2C%20and%20%60null%60%20in%20polars.%20pandas%20also%20has%20%60Series.case_when%60%2C%20which%20keeps%20the%20calling%20column's%20value%20where%20no%20condition%20matches.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22inner_join%3A%20the%20catch%20of%20large-scale%20units%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22large%20%3C-%20units%20%7C%3E%20filter(unit_scale%20%3D%3D%202)%5Cncatch%20%7C%3E%5Cn%20%20inner_join(%5Cn%20%20%20%20large%20%7C%3E%20select(region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no)%2C%5Cn%20%20%20%20by%20%3D%20join_by(region_code%2C%20hh_id%2C%20catch_operator_line%20%3D%3D%20operator_line%2C%5Cn%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20catch_unit_no%20%3D%3D%20unit_no)%2C%5Cn%20%20%20%20relationship%20%3D%20%5C%22many-to-one%5C%22%5Cn%20%20)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'(catch.rename(columns%3D%7B%22catch_operator_line%22%3A%20%22operator_line%22%2C%20%22catch_unit_no%22%3A%20%22unit_no%22%7D)%5Cn%20%20%20%20.merge(units.loc%5Bunits%5B%22unit_scale%22%5D%20%3D%3D%202%2C%20UNIT_KEY%5D%2C%20on%3DUNIT_KEY%2C%20how%3D%22inner%22%2C%20validate%3D%22many_to_one%22))'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'catch.join(%5Cn%20%20%20%20units.filter(pl.col(%22unit_scale%22)%20%3D%3D%202).select(UNIT_KEY)%2C%5Cn%20%20%20%20left_on%3D%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22catch_operator_line%22%2C%20%22catch_unit_no%22%5D%2C%20right_on%3DUNIT_KEY%2C%5Cn%20%20%20%20how%3D%22inner%22%2C%20validate%3D%22m%3A1%22%2C%5Cn)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22When%20the%20key's%20names%20differ%2C%20pandas%20keeps%20**both**%20sets%20of%20key%20columns%20(%60catch_operator_line%60%20and%20%60operator_line%60)%3B%20dplyr%20and%20polars%20keep%20the%20left%20table's%20only.%20Renaming%20first%2C%20as%20the%20pandas%20code%20does%2C%20avoids%20the%20doubled%20columns.%20%60validate%3D%5C%22many_to_one%5C%22%60%20(pandas)%2C%20%60validate%3D%5C%22m%3A1%5C%22%60%20(polars)%20and%20%60relationship%20%3D%20%5C%22many-to-one%5C%22%60%20(dplyr)%20each%20stop%20the%20join%20if%20a%20unit%20is%20listed%20twice.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22summarise%3A%20catch%20rows%20and%20species%20by%20region%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22catch%20%7C%3E%5Cn%20%20group_by(region_code)%20%7C%3E%5Cn%20%20summarise(rows%20%3D%20n()%2C%20species%20%3D%20n_distinct(catch_species))%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'catch.groupby(%22region_code%22).agg(rows%3D(%22catch_species%22%2C%20%22size%22)%2C%20species%3D(%22catch_species%22%2C%20%22nunique%22)).reset_index()'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'catch.group_by(%22region_code%22).agg(rows%3Dpl.len()%2C%20species%3Dpl.col(%22catch_species%22).n_unique()).sort(%22region_code%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%60n()%60%20is%20%60%5C%22size%5C%22%60%20in%20pandas'%20%60agg%60%20and%20%60pl.len()%60%20in%20polars%3B%20%60n_distinct%60%20is%20%60%5C%22nunique%5C%22%60%20and%20%60.n_unique()%60.%20pandas%20sorts%20the%20groups%3B%20polars%20returns%20them%20in%20no%20fixed%20order%2C%20so%20the%20polars%20code%20sorts.%20A%20blank%20species%20would%20be%20a%20value%20of%20its%20own%20for%20%60n_unique%60%20and%20%60n_distinct%60%2C%20and%20left%20out%20by%20%60nunique%60.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22count%3A%20units%20by%20region%20and%20scale%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22units%20%7C%3E%20count(region_code%2C%20unit_scale)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'units.groupby(%5B%22region_code%22%2C%20%22unit_scale%22%5D).size().reset_index(name%3D%22n%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'units.group_by(%22region_code%22%2C%20%22unit_scale%22).len(name%3D%22n%22).sort(%22region_code%22%2C%20%22unit_scale%22)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22dplyr's%20%60count%60%20names%20its%20column%20%60n%60.%20pandas'%20%60size()%60%20gives%20a%20column%20with%20the%20groups%20as%20its%20index%3B%20%60reset_index(name%3D%5C%22n%5C%22)%60%20makes%20it%20a%20table.%20polars'%20%60.len()%60%20names%20its%20column%20%60len%60%20unless%20given%20%60name%3D%60.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22arrange%3A%20units%20with%20the%20most%20boats%20first%22%3A%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22units%20%7C%3E%20arrange(desc(unit_boats))%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'units.sort_values(%22unit_boats%22%2C%20ascending%3DFalse)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20'units.sort(%22unit_boats%22%2C%20descending%3DTrue%2C%20nulls_last%3DTrue)'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Blanks%3A%20dplyr%20and%20pandas%20put%20them%20last.%20polars%20puts%20them%20**first**%20in%20a%20descending%20sort%20unless%20told%20%60nulls_last%3DTrue%60%2C%20and%20a%20unit%20that%20used%20no%20boat%20has%20a%20blank%20%60unit_boats%60%20(number%20of%20boats).%20Remove%20%60nulls_last%3DTrue%60%20and%20the%20polars%20result%20starts%20with%20them.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%7D%0A%20%20%20%20side_task%20%3D%20mo.ui.dropdown(options%3Dlist(SIDE_BY_SIDE)%2C%20value%3D%22filter%3A%20large-scale%20units%2C%20not%20voided%22%2C%20label%3D%22Task%22)%0A%20%20%20%20side_task%0A%20%20%20%20return%20SIDE_BY_SIDE%2C%20side_task%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(SIDE_BY_SIDE%2C%20side_pd%2C%20side_pl%2C%20side_task)%3A%0A%20%20%20%20_r_code%2C%20_pandas_code%2C%20_polars_code%2C%20_notice%20%3D%20SIDE_BY_SIDE%5Bside_task.value%5D%0A%20%20%20%20_pandas%20%3D%20eval(_pandas_code%2C%20%7B%22np%22%3A%20np%2C%20%22pd%22%3A%20pd%2C%20%22UNIT_KEY%22%3A%20UNIT_KEY%2C%20**side_pd%7D)%0A%20%20%20%20_polars%20%3D%20eval(_polars_code%2C%20%7B%22pl%22%3A%20pl%2C%20%22UNIT_KEY%22%3A%20UNIT_KEY%2C%20**side_pl%7D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**R%20(dplyr)**%5Cn%5Cn%60%60%60r%5Cn%7B_r_code%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**pandas**%5Cn%5Cn%60%60%60python%5Cn%7B_pandas_code%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**polars**%5Cn%5Cn%60%60%60python%5Cn%7B_polars_code%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%20widths%3D%22equal%22%2C%20gap%3D1)%2C%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(f%22**What%20to%20notice.**%20%7B_notice%7D%22)%2C%20kind%3D%22neutral%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**pandas**%20gives%20%7Blen(_pandas)%3A%2C%7D%20rows%3B%20the%20first%20ones%3A%22)%2C%0A%20%20%20%20%20%20%20%20_pandas.head(5)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**polars**%20gives%20%7B_polars.height%3A%2C%7D%20rows%3B%20the%20first%20ones%3A%22)%2C%0A%20%20%20%20%20%20%20%20_polars.head(5)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey)%3A%0A%20%20%20%20_households%20%3D%20survey%5B%22household%22%5D%0A%20%20%20%20_households_pl%20%3D%20pl.from_pandas(_households)%0A%20%20%20%20_voided%20%3D%20int(_households%5B%22row_void%22%5D.eq(1).sum())%0A%20%20%20%20_ways%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22%60row_void%20!%3D%201%60%22%2C%20len(_households%5B_households%5B%22row_void%22%5D%20!%3D%201%5D)%2C%20_households_pl.filter(pl.col(%22row_void%22)%20!%3D%201).height)%2C%0A%20%20%20%20%20%20%20%20(%22%60row_void%20%3D%3D%200%60%22%2C%20len(_households%5B_households%5B%22row_void%22%5D%20%3D%3D%200%5D)%2C%20_households_pl.filter(pl.col(%22row_void%22)%20%3D%3D%200).height)%2C%0A%20%20%20%20%20%20%20%20(%22blank%2C%20or%20not%201%22%2C%20len(_households%5B_households%5B%22row_void%22%5D.isna()%20%7C%20(_households%5B%22row_void%22%5D%20!%3D%201)%5D)%2C%20_households_pl.filter(pl.col(%22row_void%22).is_null()%20%7C%20(pl.col(%22row_void%22)%20!%3D%201)).height)%2C%0A%20%20%20%20%5D%0A%20%20%20%20_as_float%20%3D%20_households%5B%22row_void%22%5D.astype(%22float64%22)%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20Where%20an%20R%20habit%20breaks%3A%20a%20blank%20in%20a%20condition%0A%0A%20%20%20%20In%20R%2C%20%60NA%20!%3D%201%60%20is%20%60NA%60%2C%20and%20dplyr's%20%60filter%60%20drops%20every%20row%20whose%20condition%20is%20%60NA%60.%20So%20%60filter(row_void%20!%3D%201)%60%20keeps%20no%20row%20whose%20%60row_void%60%20is%20blank%3A%20on%20%60household%60%2C%20which%20has%20%7Blen(_households)%3A%2C%7D%20households%2C%20%7B_voided%7D%20of%20them%20voided%2C%20it%20keeps%20none%20at%20all.%20pandas%20(with%20a%20nullable%20integer%20column%2C%20as%20%60course.table()%60%20gives)%20and%20polars%20do%20the%20same%3A%0A%0A%20%20%20%20%7C%20condition%20on%20%60household%60's%20%60row_void%60%20%7C%20pandas%20keeps%20%7C%20polars%20keeps%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7B(chr(10)%20%2B%20%22%20%20%20%20%22).join(f%22%7C%20%7Bway%7D%20%7C%20%7Bpandas%3A%2C%7D%20%7C%20%7Bpolars%3A%2C%7D%20%7C%22%20for%20way%2C%20pandas%2C%20polars%20in%20_ways)%7D%0A%0A%20%20%20%20Only%20the%20last%20condition%20keeps%20the%20%7Blen(_households)%20-%20_voided%3A%2C%7D%20households%20that%20are%20not%20voided.%20Watch%20one%20exception%20in%20pandas%3A%20a%20column%20of%20decimals%20(%60float64%60)%20holds%20a%20blank%20as%20%60NaN%60%2C%20and%20%60NaN%20!%3D%201%60%20is%20%60True%60.%20On%20such%20a%20column%20%60row_void%20!%3D%201%60%20keeps%20%7Bint((_as_float%20!%3D%201).sum())%3A%2C%7D%20rows%3A%20every%20blank.%20The%20same%20comparison%20gives%20different%20rows%20by%20the%20column's%20type%2C%20so%20write%20the%20blank's%20test%20out%2C%20whatever%20the%20type%3A%20%60x.isna()%20%7C%20(x%20!%3D%201)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(units_raw)%3A%0A%20%20%20%20_right%20%3D%20int(((units_raw%5B%22unit_scale%22%5D%20%3D%3D%202)%20%26%20units_raw%5B%22region_code%22%5D.isin(%5B%2201%22%5D)).sum())%0A%20%20%20%20_without%20%3D%20int((units_raw%5B%22unit_scale%22%5D%20%3D%3D%202%20%26%20units_raw%5B%22region_code%22%5D.isin(%5B%2201%22%5D)).sum())%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20units_raw%5Bunits_raw%5B%22region_code%22%5D%20%3D%3D%20%2201%22%20%26%20units_raw%5B%22unit_scale%22%5D%20%3D%3D%202%5D%0A%20%20%20%20%20%20%20%20_error%20%3D%20%22no%20error%22%0A%20%20%20%20except%20Exception%20as%20_err%3A%20%20%23%20the%20error%20is%20the%20lesson%3A%20show%20its%20kind%20and%20message%0A%20%20%20%20%20%20%20%20_error%20%3D%20f%22%7Btype(_err).__name__%7D%3A%20%7B_err%7D%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20units_raw%5B(units_raw%5B%22region_code%22%5D%20%3D%3D%20%2201%22)%20and%20(units_raw%5B%22unit_scale%22%5D%20%3D%3D%202)%5D%0A%20%20%20%20%20%20%20%20_and_error%20%3D%20%22no%20error%22%0A%20%20%20%20except%20Exception%20as%20_err%3A%0A%20%20%20%20%20%20%20%20_and_error%20%3D%20f%22%7Btype(_err).__name__%7D%3A%20%7B_err%7D%22%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20Where%20an%20R%20habit%20breaks%3A%20two%20conditions%0A%0A%20%20%20%20dplyr%20writes%20two%20conditions%20with%20a%20comma%2C%20or%20with%20%60%26%60%20and%20no%20brackets%3A%20%60filter(region_code%20%3D%3D%20%2201%22%20%26%20unit_scale%20%3D%3D%202)%60.%20In%20R%2C%20%60%3D%3D%60%20is%20worked%20out%20before%20%60%26%60.%20In%20Python%2C%20%60%26%60%20is%20worked%20out%20**first**%2C%20so%20the%20same%20line%20without%20brackets%20compares%20a%20column%20with%20the%20result%20of%20%60%2201%22%20%26%20...%60%3A%0A%0A%20%20%20%20%7C%20written%20in%20pandas%20%7C%20what%20happens%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60units%5Bunits%5B%22region_code%22%5D%20%3D%3D%20%2201%22%20%26%20units%5B%22unit_scale%22%5D%20%3D%3D%202%5D%60%20%7C%20*%7B_error%7D*%20%7C%0A%20%20%20%20%7C%20%60units%5B(units%5B%22region_code%22%5D%20%3D%3D%20%2201%22)%20and%20(units%5B%22unit_scale%22%5D%20%3D%3D%202)%5D%60%20%7C%20*%7B_and_error%7D*%3A%20%60and%60%20wants%20one%20%60True%60%20or%20%60False%60%2C%20and%20a%20column%20holds%20many%20%7C%0A%20%20%20%20%7C%20%60units%5B%22unit_scale%22%5D%20%3D%3D%202%20%26%20units%5B%22region_code%22%5D.isin(%5B%2201%22%5D)%60%20%7C%20no%20error%2C%20and%20%7B_without%7D%20rows%20where%20%7B_right%7D%20are%20meant%3A%20%602%20%26%20True%60%20is%20%600%60%2C%20so%20it%20tests%20%60unit_scale%20%3D%3D%200%60%20%7C%0A%20%20%20%20%7C%20%60(units%5B%22unit_scale%22%5D%20%3D%3D%202)%20%26%20units%5B%22region_code%22%5D.isin(%5B%2201%22%5D)%60%20%7C%20%7B_right%7D%20rows%3A%20right%20%7C%0A%0A%20%20%20%20The%20third%20line%20is%20the%20dangerous%20one.%20Read%20the%20first%20two%20errors%20from%20their%20last%20line%3A%20both%20say%20the%20condition%20was%20not%20what%20you%20meant.%20The%20cure%20is%20the%20same%20for%20all%20three%3A%20a%20pair%20of%20brackets%20around%20each%20comparison%2C%20joined%20with%20%60%26%60%20(and)%2C%20%60%7C%60%20(or)%20or%20%60~%60%20(not).%20polars'%20%60filter%60%20takes%20the%20conditions%20separated%20by%20commas%2C%20as%20dplyr%20does.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%201.2.%20Step%201%2C%20filter%3A%20the%20large-scale%20units%20that%20are%20not%20voided%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20count%20of%20large-scale%20units%20starts%20from%20this%20filter.%20Written%20the%20dplyr%20way%20without%20the%20blank%20in%20mind%2C%20it%20keeps%20no%20row%20at%20all%2C%20and%20every%20table%20after%20it%20comes%20out%20empty%20with%20no%20error.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20fishing%20units%20table%20as%20the%20database%20holds%20it%20%E2%86%92%20its%20rows%20whose%20%60unit_scale%60%20is%202%20and%20whose%20%60row_void%60%20is%20blank%20or%20not%201%2C%20every%20column%20kept.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20unit_scale%20%7C%20row_void%20%7C%20kept%3F%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2001%20%7C%205012%20%7C%202%20%7C%20*blank*%20%7C%20yes%20%7C%0A%20%20%20%20%7C%2001%20%7C%205013%20%7C%202%20%7C%201%20%7C%20no%3A%20voided%20in%20editing%20%7C%0A%20%20%20%20%7C%2004%20%7C%205204%20%7C%201%20%7C%20*blank*%20%7C%20no%3A%20small-scale%20%7C%0A%20%20%20%20%7C%2007%20%7C%205088%20%7C%202%20%7C%20*blank*%20%7C%20yes%20%7C%0A%0A%20%20%20%20In%20R%3A%20%60units%20%7C%3E%20filter(unit_scale%20%3D%3D%202%2C%20is.na(row_void)%20%7C%20row_void%20!%3D%201)%60.%0A%0A%20%20%20%20**Tools%3A**%20the%20*keep%20rows*%20and%20*is%20blank*%20rows%20of%20the%20translation%20table%3A%20%60table%5B(condition)%20%26%20(condition)%5D%60%2C%20%60.isna()%60%2C%20and%20%60%7C%60%20for%20*or*%2C%20with%20brackets%20around%20each%20comparison.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20large_scale_units(units%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20fishing%20units%20whose%20scale%20is%202%20(a%20boat%20of%205.0%20gross%20tons%20or%20more)%20and%20that%20are%20not%20voided.%22%22%22%0A%20%20%20%20%23%20Exercise%201.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20condition%20for%20large-scale%3A%20unit_scale%20equal%20to%202%0A%20%20%20%20%23%20Step%202%3A%20a%20condition%20for%20not%20voided%3A%20row_void%20blank%2C%20or%20row_void%20not%20equal%20to%201%0A%20%20%20%20%23%20Step%203%3A%20keep%20the%20rows%20where%20both%20hold%20(%26%2C%20a%20pair%20of%20brackets%20around%20each%20part)%20and%20return%20them%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.2%22)%0A%0A%0A%40app.function%0Adef%20small_units()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Five%20fishing%20units%20for%20Exercise%201.2's%20check%3A%20one%20large-scale%20and%20voided%20in%20editing%2C%20one%20small-scale%20and%20voided.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2204%22%2C%20%2207%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B5012%2C%205013%2C%205204%2C%205205%2C%205088%5D%2C%0A%20%20%20%20%20%20%20%20%22unit_scale%22%3A%20%5B2%2C%202%2C%201%2C%201%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22row_void%22%3A%20pd.array(%5BNone%2C%201%2C%20None%2C%201%2C%20None%5D%2C%20dtype%3D%22Int32%22)%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20as_rows(result)%20-%3E%20pd.DataFrame%20%7C%20str%3A%0A%20%20%20%20%22%22%22A%20table%20as%20the%20checks%20compare%20it%3A%20its%20rows%20sorted%20by%20every%20column%20and%20numbered%20afresh%2C%20so%20a%20right%20answer%20passes%0A%20%20%20%20whatever%20order%20its%20rows%20come%20in%20and%20whatever%20their%20index%20labels%20are.%22%22%22%0A%20%20%20%20if%20not%20isinstance(result%2C%20pd.DataFrame)%3A%0A%20%20%20%20%20%20%20%20return%20f%22not%20a%20pandas%20DataFrame%20but%20a%20%7Btype(result).__name__%7D%22%0A%20%20%20%20return%20result.sort_values(list(result.columns)%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(units_raw)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%201.2%22%2C%20large_scale_units%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20five%20units%20above%22%2C%20(small_units()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20unit%2C%20as%20the%20databases%20hold%20them%22%2C%20(units_raw%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20unit%2C%20removed%20households%20left%20out%20(course.units())%22%2C%20(course.units()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20units%22%2C%20(units_raw%5Bunits_raw%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20fishing%20units%22%2C%20summarize%3Das_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Two%20conditions%20joined%20with%20%26%2C%20brackets%20around%20each%3A%20unit_scale%20%3D%3D%202%2C%20and%20(row_void%20blank%20%7C%20row_void%20!%3D%201).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(units_raw)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20large-scale%20units%20of%20the%20survey%0A%20%20%20%20try_it(large_scale_units%2C%20units_raw)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%201.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%200%20rows%20for%20the%20survey%20%7C%20%60row_void%20!%3D%201%60%20alone%3A%20it%20is%20blank%20for%20a%20blank%20mark%2C%20and%20a%20row%20with%20a%20blank%20condition%20is%20dropped.%20Or%20%60row_void%20%3D%3D%200%60%3A%20the%20mark%20is%20never%200.%20Or%20%60units%5B%22row_void%22%5D.isna()%20%5C%7C%20units%5B%22row_void%22%5D%20!%3D%201%60%20with%20no%20brackets%20around%20the%20comparison%3A%20%60%5C%7C%60%20is%20worked%20out%20before%20%60!%3D%60%2C%20with%20no%20error.%20%7C%20%60units%5B%22row_void%22%5D.isna()%20%5C%7C%20(units%5B%22row_void%22%5D%20!%3D%201)%60%20%7C%0A%20%20%20%20%7C%20The%20second%20of%20the%20five%20units%20is%20kept%20%7C%20The%20void%20mark%20is%20not%20tested.%20%7C%20the%20second%20condition%20%7C%0A%20%20%20%20%7C%20Small-scale%20units%20(%60unit_scale%60%201)%20are%20kept%20%7C%20The%20scale%20is%20not%20tested%2C%20or%20tested%20with%20%60!%3D%201%60%20on%20a%20column%20that%20could%20hold%20other%20values.%20%7C%20%60units%5B%22unit_scale%22%5D%20%3D%3D%202%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20Cannot%20perform%20'rand_'%20...*%20or%20*TypeError%3A%20'other'%20should%20be%20pandas.NA%20or%20a%20bool*%20%7C%20A%20comparison%20without%20its%20own%20brackets%3A%20%60%26%60%20and%20%60%5C%7C%60%20are%20worked%20out%20before%20%60%3D%3D%60%20and%20%60!%3D%60.%20%7C%20a%20pair%20of%20brackets%20around%20each%20comparison%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20The%20truth%20value%20of%20a%20Series%20is%20ambiguous*%20%7C%20%60and%60%20or%20%60or%60%20between%20two%20columns%20of%20conditions%2C%20or%20two%20comparisons%20chained%20without%20brackets.%20%7C%20%60%26%60%20and%20%60%5C%7C%60%2C%20and%20brackets%20%7C%0A%20%20%20%20%7C%20Fewer%20columns%20than%20the%20input%20%7C%20Columns%20were%20selected%20after%20the%20filter.%20%7C%20return%20%60units%5B...%5D%60%20with%20every%20column%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20beside_polars(code%3A%20str%2C%20yours%2C%20**tables)%20-%3E%20mo.Html%3A%0A%20%20%20%20%22%22%22The%20same%20step%20in%20polars%3A%20its%20code%2C%20run%20on%20the%20same%20tables%20made%20polars%2C%20and%20its%20row%20count%20beside%20your%20pandas%0A%20%20%20%20function's%20result%20(%60yours%60%3A%20a%20table%2C%20NOT_YET%2C%20or%20the%20error%20your%20function%20raised).%22%22%22%0A%20%20%20%20result%20%3D%20eval(code%2C%20%7B%22pl%22%3A%20pl%2C%20%22UNIT_KEY%22%3A%20UNIT_KEY%2C%20**%7Bname%3A%20pl.from_pandas(t)%20for%20name%2C%20t%20in%20tables.items()%7D%7D)%0A%20%20%20%20if%20yours%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20line%20%3D%20%22Your%20pandas%20function%20is%20not%20written%20yet%3A%20finish%20it%20above%20to%20compare.%22%0A%20%20%20%20elif%20isinstance(yours%2C%20Exception)%3A%0A%20%20%20%20%20%20%20%20line%20%3D%20f%22Your%20pandas%20function%20raised%20*%7Btype(yours).__name__%7D*%3B%20the%20check%20above%20says%20on%20which%20input.%22%0A%20%20%20%20elif%20isinstance(yours%2C%20pd.DataFrame)%3A%0A%20%20%20%20%20%20%20%20same%20%3D%20%22the%20same%20number%22%20if%20len(yours)%20%3D%3D%20result.height%20else%20%22**not**%20the%20same%20number%3A%20the%20check%20above%20says%20which%20input%20differs%22%0A%20%20%20%20%20%20%20%20line%20%3D%20f%22Your%20pandas%20function%20gives%20**%7Blen(yours)%3A%2C%7D%20rows**%2C%20%7Bsame%7D.%22%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20line%20%3D%20f%22Your%20pandas%20function%20gives%20a%20%7Btype(yours).__name__%7D%2C%20not%20a%20table.%22%0A%20%20%20%20return%20mo.vstack(%5Bmo.md(f%22**The%20same%20step%20in%20polars**%5Cn%5Cn%60%60%60python%5Cn%7Bcode%7D%5Cn%60%60%60%5Cn%5Cnpolars%20gives%20**%7Bresult.height%3A%2C%7D%20rows**.%20%7Bline%7D%22)%2C%20result.head(5)%5D)%0A%0A%0A%40app.function%0Adef%20yours(function%2C%20*args)%3A%0A%20%20%20%20%22%22%22Your%20exercise%20function's%20result%20on%20these%20inputs%3A%20NOT_YET%20while%20it%20is%20not%20written%2C%20the%20error%20if%20it%20raised%20one.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20return%20function(*args)%0A%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20return%20NOT_YET%0A%20%20%20%20except%20Exception%20as%20error%3A%20%20%23%20a%20wrong%20answer%20that%20raises%20is%20still%20shown%2C%20as%20the%20error%20it%20raised%0A%20%20%20%20%20%20%20%20return%20error%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(units_raw)%3A%0A%20%20%20%20beside_polars(%0A%20%20%20%20%20%20%20%20'units.filter(pl.col(%22unit_scale%22)%20%3D%3D%202%2C%20pl.col(%22row_void%22).is_null()%20%7C%20(pl.col(%22row_void%22)%20!%3D%201))'%2C%0A%20%20%20%20%20%20%20%20yours(large_scale_units%2C%20units_raw)%2C%20units%3Dunits_raw%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%201.3.%20Step%202%2C%20mutate%3A%20each%20boat's%20gross%20tonnage%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20unit%20is%20large-scale%20when%20one%20of%20its%20boats%20is%205.0%20gross%20tons%20or%20more%2C%20so%20the%20tonnage%20decides%20which%20table%20every%20unit%20is%20counted%20in.%20The%20questionnaire%20takes%20a%20boat's%20size%20from%20its%20registration%20papers%2C%20or%20measures%20it%20in%20meters%20or%20in%20feet%20(%60boat_measured_in%60)%2C%20and%20the%20formula%20depends%20on%20which.%20The%20capture%20program%20stores%20a%20tonnage%20too%20(%60boat_tonnage%60)%2C%20but%20stored%20figures%20can%20be%20wrong%3B%20computing%20it%20again%20is%20how%20module%204%20checks%20them.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20boats%20table%20%E2%86%92%20the%20same%20table%20with%20one%20more%20column%20at%20the%20end%2C%20%60gt%60%2C%20the%20boat's%20gross%20tonnage%2C%20by%20rule%20B3%20of%20%60codebook_rules.csv%60%3A%0A%0A%20%20%20%20%7C%20%60boat_measured_in%60%2C%20where%20the%20size%20comes%20from%20%7C%20%60gt%60%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%2C%20the%20registration%20papers%20%7C%20%60boat_tonnage_reported%60%2C%20the%20tonnage%20on%20the%20papers%20%7C%0A%20%20%20%20%7C%202%2C%20measured%20in%20meters%20%7C%20%60boat_length%20%C3%97%20boat_beam%20%C3%97%20boat_depth%20%C3%97%200.212%60%20%7C%0A%20%20%20%20%7C%203%2C%20measured%20in%20feet%20%7C%20%60boat_length%20%C3%97%20boat_beam%20%C3%97%20boat_depth%20%C3%97%200.006%60%20%7C%0A%0A%20%20%20%20For%20example%2C%20a%20boat%20measured%20in%20meters%2C%2010%20long%2C%201%20wide%20and%200.5%20deep%2C%20is%2010%20%C3%97%201%20%C3%97%200.5%20%C3%97%200.212%20%3D%201.06%20gross%20tons.%20Mind%20the%20order%20of%20the%20codes%3A%201%20is%20the%20papers%2C%20not%20meters.%0A%0A%20%20%20%20In%20R%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20boats%20%7C%3E%20mutate(gt%20%3D%20case_when(%0A%20%20%20%20%20%20boat_measured_in%20%3D%3D%201%20~%20boat_tonnage_reported%2C%0A%20%20%20%20%20%20boat_measured_in%20%3D%3D%202%20~%20boat_length%20*%20boat_beam%20*%20boat_depth%20*%200.212%2C%0A%20%20%20%20%20%20boat_measured_in%20%3D%3D%203%20~%20boat_length%20*%20boat_beam%20*%20boat_depth%20*%200.006%0A%20%20%20%20))%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20**Tools%3A**%20the%20*a%20column%20by%20cases*%20row%20of%20the%20translation%20table%3A%20%60np.select(%5Bconditions%5D%2C%20%5Bvalues%5D%2C%20default%3Dnp.nan)%60%2C%20the%20conditions%20and%20the%20values%20in%20the%20same%20order%3B%20%60table.assign(gt%3D...)%60%20returns%20the%20table%20with%20the%20new%20column.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20with_gross_tonnage(boats%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20boats%20table%20with%20one%20more%20column%2C%20gt%3A%20the%20gross%20tonnage%20from%20the%20papers%2C%20from%20meters%20or%20from%20feet%2C%20by%20boat_measured_in.%22%22%22%0A%20%20%20%20%23%20Exercise%201.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20product%20of%20the%20dimensions%3A%20boat_length%20*%20boat_beam%20*%20boat_depth%0A%20%20%20%20%23%20Step%202%3A%20np.select%3A%20the%20three%20conditions%20on%20boat_measured_in%20(1%2C%202%2C%203)%2C%20and%20the%20three%20values%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20(boat_tonnage_reported%2C%20the%20product%20times%200.212%2C%20the%20product%20times%200.006)%2C%20default%20np.nan%0A%20%20%20%20%23%20Step%203%3A%20return%20boats.assign(gt%3D...)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.3%22)%0A%0A%0A%40app.function%0Adef%20small_boats()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Four%20boats%20for%20Exercise%201.3's%20check%3A%20measured%20in%20meters%2C%20in%20feet%2C%20from%20its%20papers%2C%20and%20one%20in%20meters%20whose%20stored%0A%20%20%20%20tonnage%20is%20wrong%20(the%20dimensions'%20product%20with%20the%20factor%20left%20out).%22%22%22%0A%20%20%20%20nan%20%3D%20np.nan%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22boat_measured_in%22%3A%20%5B2%2C%203%2C%201%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22boat_length%22%3A%20%5B10.0%2C%2030.0%2C%20nan%2C%208.0%5D%2C%20%22boat_beam%22%3A%20%5B1.0%2C%203.0%2C%20nan%2C%201.0%5D%2C%20%22boat_depth%22%3A%20%5B0.5%2C%202.0%2C%20nan%2C%200.6%5D%2C%0A%20%20%20%20%20%20%20%20%22boat_tonnage%22%3A%20%5B1.06%2C%201.08%2C%205.2%2C%204.8%5D%2C%0A%20%20%20%20%20%20%20%20%22boat_tonnage_reported%22%3A%20%5Bnan%2C%20nan%2C%205.2%2C%20nan%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey)%3A%0A%20%20%20%20_boats%20%3D%20survey%5B%22boat%22%5D%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%201.3%22%2C%20with_gross_tonnage%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20boats%3A%20meters%2C%20feet%2C%20from%20the%20papers%2C%20a%20wrong%20stored%20tonnage%22%2C%20(small_boats()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20boat%2C%20as%20the%20databases%20hold%20them%22%2C%20(_boats%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20boats%22%2C%20(_boats%5B_boats%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20boats%22%2C%20summarize%3Das_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Three%20cases%20on%20boat_measured_in%3A%201%20the%20papers'%20tonnage%2C%202%20meters%20%C3%97%200.212%2C%203%20feet%20%C3%97%200.006%3B%20np.select%2C%20then%20assign(gt%3D...).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey)%3A%0A%20%20%20%20%23%20Try%20it%3A%20every%20boat's%20gross%20tonnage.%20The%20new%20column%20is%20the%20last%20one.%0A%20%20%20%20try_it(with_gross_tonnage%2C%20survey%5B%22boat%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%201.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20About%2038%20for%20the%20second%20small%20boat%2C%20not%201.08%20%7C%20Feet%20multiplied%20by%200.212%2C%20the%20factor%20for%20meters.%20%7C%20%60*%200.006%60%20for%20a%20boat%20measured%20in%20feet%20%7C%0A%20%20%20%20%7C%20A%20blank%20%60gt%60%20for%20the%20third%20small%20boat%20%7C%20Only%20two%20cases%20(an%20%60np.where%60%20on%20meters%20or%20feet)%3A%20the%20papers'%20tonnage%20is%20never%20taken.%20%7C%20a%20condition%20%60boat_measured_in%20%3D%3D%201%60%2C%20with%20%60boat_tonnage_reported%60%20%7C%0A%20%20%20%20%7C%200.03%20for%20the%20first%20small%20boat%2C%20and%20blanks%20for%20the%20next%20two%20%7C%20The%20codes%20read%20in%20another%20order%20(1%20as%20meters%2C%202%20as%20feet%2C%203%20as%20the%20papers).%20%7C%20the%20codebook's%20%60values%60%20for%20%60boat_measured_in%60%3A%201%20is%20the%20papers%20%7C%0A%20%20%20%20%7C%204.8%20for%20the%20fourth%20small%20boat%20%7C%20The%20stored%20%60boat_tonnage%60%20was%20copied%3B%20it%20is%20wrong%20for%20that%20boat.%20%7C%20compute%20%60boat_length%20*%20boat_beam%20*%20boat_depth%20*%200.212%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20list%20of%20cases%20must%20be%20same%20length%20as%20list%20of%20conditions*%20%7C%20%60np.select%60%20was%20given%20three%20conditions%20and%20two%20values%2C%20or%20the%20other%20way%20round.%20%7C%20the%20two%20lists%2C%20in%20the%20same%20order%20%7C%0A%20%20%20%20%7C%20No%20%60gt%60%20column%2C%20or%20%60gt%60%20not%20last%20%7C%20The%20result%20was%20not%20added%20as%20a%20column%2C%20or%20the%20columns%20were%20reordered.%20%7C%20%60boats.assign(gt%3D...)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey)%3A%0A%20%20%20%20beside_polars(%0A%20%20%20%20%20%20%20%20'boats.with_columns(gt%3Dpl.when(pl.col(%22boat_measured_in%22)%20%3D%3D%201).then(pl.col(%22boat_tonnage_reported%22))%5Cn'%0A%20%20%20%20%20%20%20%20'%20%20%20%20.when(pl.col(%22boat_measured_in%22)%20%3D%3D%202).then(pl.col(%22boat_length%22)%20*%20pl.col(%22boat_beam%22)%20*%20pl.col(%22boat_depth%22)%20*%200.212)%5Cn'%0A%20%20%20%20%20%20%20%20'%20%20%20%20.when(pl.col(%22boat_measured_in%22)%20%3D%3D%203).then(pl.col(%22boat_length%22)%20*%20pl.col(%22boat_beam%22)%20*%20pl.col(%22boat_depth%22)%20*%200.006))'%2C%0A%20%20%20%20%20%20%20%20yours(with_gross_tonnage%2C%20survey%5B%22boat%22%5D)%2C%20boats%3Dsurvey%5B%22boat%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_boats%20%3D%20yours(with_gross_tonnage%2C%20course.child(%22boat%22))%0A%20%20%20%20mo.stop(_boats%20is%20NOT_YET%2C%20waiting_for(%22Exercise%201.3%22))%0A%20%20%20%20mo.stop(not%20isinstance(_boats%2C%20pd.DataFrame)%20or%20%22gt%22%20not%20in%20_boats.columns%2C%20course.look(%22No%20gt%20column%20yet%22%2C%20%22Exercise%201.3's%20check%20says%20what%20to%20fix.%22))%0A%20%20%20%20_largest%20%3D%20_boats.groupby(UNIT_KEY)%5B%22gt%22%5D.max().rename(%22largest_gt%22).reset_index()%0A%20%20%20%20_units%20%3D%20course.units().merge(_largest%2C%20on%3DUNIT_KEY%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20_from_boats%20%3D%20np.where(_units%5B%22largest_gt%22%5D%20%3E%3D%205%2C%202%2C%201)%0A%20%20%20%20_units%5B%22from%20your%20tonnages%22%5D%20%3D%20np.where(_from_boats%20%3D%3D%202%2C%20%222%3A%20a%20boat%20of%205%20GT%20or%20more%22%2C%20%221%3A%20no%20boat%20of%205%20GT%20or%20more%22)%0A%20%20%20%20_units%5B%22unit_scale%2C%20as%20reported%22%5D%20%3D%20_units%5B%22unit_scale%22%5D.map(%7B1%3A%20%221%3A%20small-scale%22%2C%202%3A%20%222%3A%20large-scale%22%7D)%0A%20%20%20%20_table%20%3D%20pd.crosstab(_units%5B%22unit_scale%2C%20as%20reported%22%5D%2C%20_units%5B%22from%20your%20tonnages%22%5D)%0A%20%20%20%20_agree%20%3D%20int((_units%5B%22unit_scale%22%5D%20%3D%3D%20_from_boats).sum())%0A%20%20%20%20_small_but_large%20%3D%20int(((_units%5B%22unit_scale%22%5D%20%3D%3D%201)%20%26%20(_from_boats%20%3D%3D%202)).sum())%0A%20%20%20%20_which%20%3D%20%22every%20one%20of%20them%20is%22%20if%20_small_but_large%20%3D%3D%20len(_units)%20-%20_agree%20else%20f%22%7B_small_but_large%7D%20of%20them%20are%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20**Your%20tonnages%20against%20the%20reported%20scale.**%20Rule%20U3%20says%20%60unit_scale%60%20is%202%20when%20any%20of%20the%20unit's%20boats%20is%205.0%20gross%20tons%20or%20more%2C%20and%201%20otherwise%3B%20a%20unit%20without%20a%20boat%20is%201.%20Here%20is%20every%20unit%20(removed%20households%20left%20out)%2C%20its%20reported%20scale%20against%20the%20scale%20your%20largest%20tonnage%20gives%3A%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20_table%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22They%20agree%20on%20**%7B_agree%3A%2C%7D**%20of%20%7Blen(_units)%3A%2C%7D%20units.%20The%20%7Blen(_units)%20-%20_agree%7D%20that%20disagree%20are%20edit%20failures%2C%20of%20the%20kind%20module%204%20writes%20rules%20for%3A%20%7B_which%7D%20reported%20small-scale%20while%20one%20of%20its%20boats%20is%205%20gross%20tons%20or%20more.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%201.4.%20Step%203%2C%20inner_join%3A%20the%20catch%20of%20the%20large-scale%20units%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20catch%20table%20does%20not%20say%20whether%20its%20unit%20is%20large-scale%3B%20only%20the%20units%20table%20does.%20An%20inner%20join%20keeps%20the%20catch%20rows%20whose%20unit%20is%20in%20the%20large-scale%20set%20and%20drops%20the%20rest.%20It%20needs%20the%20whole%20key%3A%20without%20%60region_code%60%20a%20household%20of%20region%2001%20matches%20the%20household%20with%20the%20same%20id%20in%20region%2007%2C%20and%20without%20%60unit_no%60%20a%20member's%20second%20unit%20matches%20their%20first.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_units%20%3D%20course.units()%0A%20%20%20%20_twice%20%3D%20_units%5B(_units%5B%22unit_scale%22%5D%20%3D%3D%202)%20%26%20_units.duplicated(subset%3DUNIT_KEY%2C%20keep%3DFalse)%5D%0A%20%20%20%20_copies%20%3D%20_twice.duplicated(subset%3DUNIT_KEY).sum()%0A%20%20%20%20_count%20%3D%20%22one%20unit%20is%22%20if%20_copies%20%3D%3D%201%20else%20f%22%7B_copies%7D%20units%20are%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**One%20unit%20twice.**%20A%20join%20that%20expects%20one%20unit%20per%20key%20should%20say%20so%2C%20and%20on%20this%20survey%20it%20has%20a%20reason%20to%3A%20among%20the%20large-scale%20units%2C%20%7B_count%7D%20in%20the%20table%20twice%3A%20the%20same%20key%2C%20and%20the%20same%20answers%2C%20on%20two%20rows.%20Module%204%20finds%20such%20copies%20and%20decides%20what%20to%20do%20with%20them.%20Here%2C%20the%20check%20and%20the%20Try%20it%20cell%20give%20your%20function%20the%20large-scale%20units%20with%20the%20copy%20dropped%20(%60drop_duplicates(subset%3DUNIT_KEY)%60)%3A%22)%2C%0A%20%20%20%20%20%20%20%20_twice%5BUNIT_KEY%20%2B%20%5B%22row_id%22%2C%20%22unit_scale%22%2C%20%22unit_boats%22%5D%5D%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20fishing%20units%20(the%20large-scale%20ones%2C%20from%20step%201)%20and%20the%20catch%20table%20as%20exported%2C%20with%20its%20key%20parts%20named%20%60catch_operator_line%60%20and%20%60catch_unit_no%60%20%E2%86%92%20one%20row%20per%20catch%20row%20whose%20unit%20is%20in%20the%20units%20table%2C%20with%20five%20columns%3A%20the%20four%20of%20%60UNIT_KEY%60%2C%20then%20%60catch_species%60%2C%20the%20species.%0A%0A%20%20%20%20%7C%20the%20catch%20row's%20key%20%7C%20its%20unit%20is%20in%20%60units%60%3F%20%7C%20in%20the%20result%3F%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2001%2C%205012%2C%20line%201%2C%20unit%201%20%7C%20yes%20%7C%20yes%20%7C%0A%20%20%20%20%7C%2001%2C%205012%2C%20line%201%2C%20unit%20**2**%20%7C%20no%3A%20the%20member's%20second%20unit%2C%20small-scale%20%7C%20no%20%7C%0A%20%20%20%20%7C%20**07**%2C%205012%2C%20line%201%2C%20unit%201%20%7C%20no%3A%20another%20region's%20household%20with%20the%20same%20id%20%7C%20no%20%7C%0A%0A%20%20%20%20In%20R%3A%20%60catch%20%7C%3E%20inner_join(units%2C%20by%20%3D%20join_by(region_code%2C%20hh_id%2C%20catch_operator_line%20%3D%3D%20operator_line%2C%20catch_unit_no%20%3D%3D%20unit_no))%60%2C%20then%20%60select%60%20the%20five%20columns.%0A%0A%20%20%20%20**Tools%3A**%20the%20*rename*%20and%20*join*%20rows%20of%20the%20translation%20table.%20%60CATCH_KEY%60%20(in%20the%20setup%20cell)%20maps%20the%20catch's%20two%20key%20names%20to%20the%20unit's%3A%20%60catch.rename(columns%3DCATCH_KEY)%60%3B%20then%20%60.merge(units%5BUNIT_KEY%5D%2C%20on%3DUNIT_KEY%2C%20how%3D%22inner%22%2C%20validate%3D%22many_to_one%22)%60%3B%20then%20the%20five%20columns%2C%20%60%5BUNIT_KEY%20%2B%20%5B%22catch_species%22%5D%5D%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20catch_of(units%3A%20pd.DataFrame%2C%20catch%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20catch%20rows%20of%20the%20given%20fishing%20units%2C%20joined%20on%20the%20whole%20unit%20key%3A%20UNIT_KEY's%20four%20columns%20and%20catch_species.%22%22%22%0A%20%20%20%20%23%20Exercise%201.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20rename%20the%20catch's%20key%20parts%20to%20the%20unit's%20names%20(CATCH_KEY)%0A%20%20%20%20%23%20Step%202%3A%20inner%20join%20to%20units%5BUNIT_KEY%5D%20on%20UNIT_KEY%2C%20with%20validate%3D%22many_to_one%22%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20five%20columns%3A%20UNIT_KEY%20%2B%20%5B%22catch_species%22%5D%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.4%22)%0A%0A%0A%40app.function%0Adef%20small_catch()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Three%20large-scale%20units%20and%20six%20catch%20rows%20for%20Exercise%201.4's%20check%3A%20a%20row%20of%20a%20member's%20second%20unit%2C%20a%20row%20of%0A%20%20%20%20another%20region's%20household%20with%20the%20same%20id%2C%20a%20row%20whose%20line%20matches%20no%20unit%2C%20and%20a%20unit%20with%20no%20catch.%22%22%22%0A%20%20%20%20units%20%3D%20pd.DataFrame(%5B(%2201%22%2C%205012%2C%201%2C%201)%2C%20(%2204%22%2C%205204%2C%202%2C%201)%2C%20(%2201%22%2C%205013%2C%201%2C%201)%5D%2C%20columns%3DUNIT_KEY)%0A%20%20%20%20catch%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B(%2201%22%2C%205012%2C%201%2C%201%2C%20201)%2C%20(%2201%22%2C%205012%2C%201%2C%201%2C%20301)%2C%20(%2201%22%2C%205012%2C%201%2C%202%2C%20203)%2C%0A%20%20%20%20%20%20%20%20%20(%2207%22%2C%205012%2C%201%2C%201%2C%20401)%2C%20(%2201%22%2C%205012%2C%203%2C%201%2C%20101)%2C%20(%2204%22%2C%205204%2C%202%2C%201%2C%20501)%5D%2C%0A%20%20%20%20%20%20%20%20columns%3D%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22catch_operator_line%22%2C%20%22catch_unit_no%22%2C%20%22catch_species%22%5D)%0A%20%20%20%20return%20units%2C%20catch%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20large-scale%20units%20without%20the%20copy%20of%20the%20one%20entered%20twice%2C%20and%20the%20catch%3A%20the%20inputs%20of%20Exercise%201.4's%20check%0A%20%20%20%20%23%20and%20Try%20it%2C%20and%20of%20the%20polars%20join%20beside%20it%0A%20%20%20%20large%20%3D%20course.units().drop_duplicates(subset%3DUNIT_KEY)%0A%20%20%20%20large%20%3D%20large%5Blarge%5B%22unit_scale%22%5D%20%3D%3D%202%5D%0A%20%20%20%20catch%20%3D%20course.table(%22catch%22)%0A%20%20%20%20return%20catch%2C%20large%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(catch%2C%20large)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%201.4%22%2C%20catch_of%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20units%20and%20catch%20rows%20above%22%2C%20small_catch())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20large-scale%20units%20and%20its%20catch%22%2C%20(large%2C%20catch))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20large-scale%20units%20and%20the%20whole%20catch%22%2C%20(large%5Blarge%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C%20catch))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20summarize%3Das_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Rename%20the%20catch's%20key%20parts%20(CATCH_KEY)%2C%20inner%20join%20on%20all%20four%20parts%20of%20UNIT_KEY%2C%20keep%20UNIT_KEY%20%2B%20%5B'catch_species'%5D.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(catch%2C%20large)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20catch%20of%20the%20survey's%20large-scale%20units%0A%20%20%20%20try_it(catch_of%2C%20large%2C%20catch)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%201.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'operator_line'*%20%7C%20The%20catch's%20key%20parts%20still%20have%20their%20%60catch_%60%20names.%20%7C%20%60catch.rename(columns%3DCATCH_KEY)%60%20first%20%7C%0A%20%20%20%20%7C%20A%20row%20of%20region%2007%20in%20the%20small%20table's%20result%20%7C%20%60region_code%60%20is%20not%20in%20the%20key%2C%20so%20household%205012%20of%20region%2007%20matched%20region%2001's.%20%7C%20all%20four%20parts%3A%20%60on%3DUNIT_KEY%60%20%7C%0A%20%20%20%20%7C%20Species%20%60203%60%20in%20the%20small%20table's%20result%20%7C%20%60unit_no%60%20is%20not%20in%20the%20key%2C%20so%20the%20member's%20second%20unit%20matched%20the%20first.%20%7C%20all%20four%20parts%20%7C%0A%20%20%20%20%7C%20Species%20%60101%60%20in%20the%20result%2C%20or%20a%20row%20with%20a%20blank%20species%20%7C%20A%20left%20or%20outer%20join%3A%20unmatched%20rows%20were%20kept.%20%7C%20%60how%3D%22inner%22%60%20%7C%0A%20%20%20%20%7C%20Columns%20such%20as%20%60row_id_x%60%20and%20%60row_id_y%60%2C%20or%20more%20than%20five%20columns%20%7C%20The%20whole%20units%20table%20was%20joined%2C%20or%20the%20five%20columns%20were%20not%20selected.%20%7C%20%60units%5BUNIT_KEY%5D%60%2C%20then%20%60%5BUNIT_KEY%20%2B%20%5B%22catch_species%22%5D%5D%60%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20Merge%20keys%20are%20not%20unique%20in%20right%20dataset*%20%7C%20A%20unit%20is%20in%20the%20units%20table%20twice.%20The%20check%20gives%20none%3B%20your%20own%20call%20may%20have%20used%20every%20unit%2C%20with%20the%20copy%20shown%20above.%20%7C%20the%20units%20with%20the%20copy%20dropped%2C%20as%20%60large%60%20is%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(catch%2C%20large)%3A%0A%20%20%20%20beside_polars(%0A%20%20%20%20%20%20%20%20'catch.join(%5Cn%20%20%20%20large.select(UNIT_KEY)%2C%5Cn%20%20%20%20left_on%3D%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22catch_operator_line%22%2C%20%22catch_unit_no%22%5D%2C%20right_on%3DUNIT_KEY%2C%5Cn%20%20%20%20how%3D%22inner%22%2C%20validate%3D%22m%3A1%22%2C%5Cn).select(%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22catch_operator_line%22%2C%20%22catch_unit_no%22%2C%20%22catch_species%22%5D)'%2C%0A%20%20%20%20%20%20%20%20yours(catch_of%2C%20large%2C%20catch)%2C%20large%3Dlarge%2C%20catch%3Dcatch%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20polars%20joins%20on%20the%20different%20names%20with%20%60left_on%3D%60%20and%20%60right_on%3D%60%20and%20keeps%20the%20left%20table's%20names%2C%20here%20the%20catch's%20%60catch_%60%20ones%3A%20no%20renaming%2C%20and%20no%20doubled%20key%20columns.%20Its%20%60validate%3D%22m%3A1%22%60%20is%20pandas'%20%60%22many_to_one%22%60%3B%20a%20repeated%20unit%20stops%20it%20with%20*ComputeError%3A%20join%20keys%20did%20not%20fulfill%20m%3A1%20validation*.%0A%0A%20%20%20%20%23%23%23%20The%20three%20steps%2C%20chained%0A%0A%20%20%20%20The%20question%20was%20which%20species%20the%20large-scale%20units%20catch.%20Here%20are%20the%20three%20steps%20as%20one%20chain%20in%20each%20language%2C%20then%20the%20answer%20from%20your%20own%20functions%2C%20with%20each%20species%20named%20from%20%60codes%2Fspecies.csv%60.%20Each%20chain%20drops%20the%20copy%20of%20the%20unit%20entered%20twice%20before%20the%20join%2C%20as%20the%20check%20did%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.ui.tabs(%7B%0A%20%20%20%20%20%20%20%20%22R%20(dplyr)%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%60%60%60r%0A%20%20%20%20large%20%3C-%20units%20%7C%3E%0A%20%20%20%20%20%20filter(unit_scale%20%3D%3D%202%2C%20is.na(row_void)%20%7C%20row_void%20!%3D%201)%20%7C%3E%0A%20%20%20%20%20%20distinct(region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no)%0A%20%20%20%20catch%20%7C%3E%0A%20%20%20%20%20%20inner_join(large%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20by%20%3D%20join_by(region_code%2C%20hh_id%2C%20catch_operator_line%20%3D%3D%20operator_line%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20catch_unit_no%20%3D%3D%20unit_no)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20relationship%20%3D%20%22many-to-one%22)%20%7C%3E%0A%20%20%20%20%20%20left_join(species%2C%20by%20%3D%20join_by(catch_species%20%3D%3D%20code))%20%7C%3E%0A%20%20%20%20%20%20count(name%2C%20sort%20%3D%20TRUE)%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20%22pandas%2C%20with%20your%20functions%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%60%60%60python%0A%20%20%20%20large%20%3D%20large_scale_units(units).drop_duplicates(subset%3DUNIT_KEY)%0A%20%20%20%20(catch_of(large%2C%20catch)%0A%20%20%20%20%20%20%20%20.merge(species%2C%20left_on%3D%22catch_species%22%2C%20right_on%3D%22code%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20%20%20%20%20.value_counts(%22name%22))%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20%22polars%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%60%60%60python%0A%20%20%20%20large%20%3D%20(units.filter(pl.col(%22unit_scale%22)%20%3D%3D%202%2C%20pl.col(%22row_void%22).is_null()%20%7C%20(pl.col(%22row_void%22)%20!%3D%201))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.unique(UNIT_KEY%2C%20keep%3D%22first%22%2C%20maintain_order%3DTrue))%0A%20%20%20%20(catch.join(large.select(UNIT_KEY)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20left_on%3D%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22catch_operator_line%22%2C%20%22catch_unit_no%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20right_on%3DUNIT_KEY%2C%20how%3D%22inner%22%2C%20validate%3D%22m%3A1%22)%0A%20%20%20%20%20%20%20%20.join(species%2C%20left_on%3D%22catch_species%22%2C%20right_on%3D%22code%22%2C%20how%3D%22left%22%2C%20validate%3D%22m%3A1%22)%0A%20%20%20%20%20%20%20%20.group_by(%22name%22).len().sort(%22len%22%2C%20descending%3DTrue))%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_units%2C%20_catch%2C%20_species%20%3D%20course.units()%2C%20course.table(%22catch%22)%2C%20course.codes(%22species%22)%0A%20%20%20%20_large%20%3D%20yours(large_scale_units%2C%20_units)%0A%20%20%20%20mo.stop(_large%20is%20NOT_YET%2C%20waiting_for(%22Exercise%201.2%22))%0A%20%20%20%20_large%20%3D%20_large.drop_duplicates(subset%3DUNIT_KEY)%20if%20isinstance(_large%2C%20pd.DataFrame)%20else%20_large%0A%20%20%20%20_rows%20%3D%20yours(catch_of%2C%20_large%2C%20_catch)%20if%20isinstance(_large%2C%20pd.DataFrame)%20else%20_large%0A%20%20%20%20mo.stop(_rows%20is%20NOT_YET%2C%20waiting_for(%22Exercise%201.4%22))%0A%20%20%20%20mo.stop(not%20isinstance(_rows%2C%20pd.DataFrame)%2C%20course.look(%22Not%20a%20table%20yet%22%2C%20%22Exercises%201.2%20and%201.4%20must%20each%20return%20a%20table%3B%20their%20checks%20say%20what%20to%20fix.%22))%0A%20%20%20%20_named%20%3D%20_rows.merge(_species%5B%5B%22code%22%2C%20%22name%22%2C%20%22group%22%5D%5D%2C%20left_on%3D%22catch_species%22%2C%20right_on%3D%22code%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20_named%5B%22name%22%5D%20%3D%20_named%5B%22name%22%5D.fillna(%22(a%20code%20not%20in%20the%20species%20list)%22)%0A%20%20%20%20_top%20%3D%20_named.value_counts(%5B%22name%22%2C%20%22group%22%5D%2C%20dropna%3DFalse).rename(%22catch%20rows%22).reset_index().head(10)%0A%20%20%20%20_groups%20%3D%20_top.groupby(%22group%22%2C%20dropna%3DFalse%2C%20sort%3DFalse)%5B%22catch%20rows%22%5D.sum().sort_values(ascending%3DFalse)%0A%20%20%20%20_polars%20%3D%20(%0A%20%20%20%20%20%20%20%20pl.from_pandas(_catch)%0A%20%20%20%20%20%20%20%20.join(pl.from_pandas(_units).filter(pl.col(%22unit_scale%22)%20%3D%3D%202%2C%20pl.col(%22row_void%22).is_null()%20%7C%20(pl.col(%22row_void%22)%20!%3D%201))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20.unique(UNIT_KEY%2C%20keep%3D%22first%22%2C%20maintain_order%3DTrue).select(UNIT_KEY)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20left_on%3D%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22catch_operator_line%22%2C%20%22catch_unit_no%22%5D%2C%20right_on%3DUNIT_KEY%2C%20how%3D%22inner%22%2C%20validate%3D%22m%3A1%22)%0A%20%20%20%20)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**Your%20chain%3A**%20%7Blen(_large)%3A%2C%7D%20large-scale%20units%2C%20**%7Blen(_rows)%3A%2C%7D%20catch%20rows**%20(polars'%20chain%3A%20%7B_polars.height%3A%2C%7D).%20The%20ten%20species%20reported%20most%20often%2C%20one%20catch%20row%20per%20species%20a%20unit%20reports%3A%22)%2C%0A%20%20%20%20%20%20%20%20_top%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22Among%20these%20ten%2C%20the%20groups%20with%20the%20most%20catch%20rows%20are%20%7B'%2C%20'.join(f'%7Bg.lower()%20if%20isinstance(g%2C%20str)%20else%20%22codes%20not%20in%20the%20list%22%7D%20(%7Bn%3A%2C%7D)'%20for%20g%2C%20n%20in%20_groups.head(3).items())%7D.%20A%20species%20can%20be%20counted%20twice%20for%20one%20unit%20when%20its%20catch%20row%20was%20entered%20twice%3B%20module%204%20finds%20those%20duplicates.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20%7C%20in%20pandas%20%7C%20in%20polars%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20open%20a%20region's%20database%20%7C%20%60duckdb.connect(path%2C%20read_only%3DTrue)%60%2C%20then%20%60con.execute(sql).df()%60%20%7C%20%60.pl()%60%20in%20place%20of%20%60.df()%60%20%7C%20read-only%2C%20so%20nothing%20you%20run%20changes%20the%20survey%20%7C%0A%20%20%20%20%7C%20read%20a%20variable's%20name%20%7C%20its%20prefix%20names%20its%20table%3A%20%60boat_length%60%20is%20in%20%60boat%60%20%7C%20%7C%20the%20codebook's%20%60label%60%20and%20%60source%60%20%7C%0A%20%20%20%20%7C%20name%20one%20row%20%7C%20a%20key%20of%20several%20columns%3A%20%60%5B%22region_code%22%2C%20%22hh_id%22%5D%60%2C%20%60UNIT_KEY%60%20%7C%20the%20same%20lists%20%7C%20%60duplicated(subset%3Dkey%2C%20keep%3DFalse)%60%20before%20you%20join%20%7C%0A%20%20%20%20%7C%20find%20codes%20off%20their%20list%20%7C%20%60~column.isin(valid)%60%20on%20%60column.dropna()%60%20%7C%20%60~pl.col(c).is_in(valid)%60%20%7C%20a%20blank%20is%20not%20an%20invalid%20code%20%7C%0A%20%20%20%20%7C%20keep%20rows%20%7C%20%60x%5B(c1)%20%26%20(c2)%5D%60%20%7C%20%60x.filter(c1%2C%20c2)%60%20%7C%20brackets%20around%20each%20comparison%3B%20a%20blank%20in%20a%20condition%20drops%20the%20row%20%7C%0A%20%20%20%20%7C%20add%20a%20column%20by%20cases%20%7C%20%60x.assign(gt%3Dnp.select(conditions%2C%20values%2C%20default%3Dnp.nan))%60%20%7C%20%60x.with_columns(gt%3Dpl.when(...).then(...))%60%20%7C%20the%20value%20for%20rows%20no%20case%20matches%20%7C%0A%20%20%20%20%7C%20join%20on%20differently%20named%20keys%20%7C%20%60rename%60%2C%20then%20%60merge(on%3D...%2C%20how%3D%22inner%22%2C%20validate%3D...)%60%20%7C%20%60join(left_on%3D...%2C%20right_on%3D...%2C%20validate%3D%22m%3A1%22)%60%20%7C%20the%20region%20in%20every%20household%20key%20%7C%0A%0A%20%20%20%20Next%20is%20module%202%2C%20**Get%20the%20tables%3A%20from%20regional%20databases%20to%20one%20flat%20table**%3A%20open%20%6010_flat_table.py%60%20from%20the%20file%20list%20(in%20marimo%2C%20the%20menu%20at%20the%20top%20left).%20It%20reads%20the%20three%20databases%20yourself%2C%20in%20one%20query%2C%20with%20the%20codes%20kept%20whole%20and%20the%20removed%20rows%20left%20out%2C%20and%20saves%20the%20result%20as%20one%20flat%20table.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%221.3%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
0f9e3ccdd786697241856905dc7e4521