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%22Stretch%3A%20processing%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20tables%20every%20other%20cell%20uses.%0A%20%20%20%20import%20duckdb%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%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey's%20tables%2C%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%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%20the%20two%20tables%20under%20it%2C%20the%20fishing%20unit%20and%20its%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%20short%20name%20each%20table's%20variables%20start%20with%3A%20mem_age%20is%20the%20member%20table's%2C%20land_name%20the%20landing%20table's%0A%20%20%20%20TABLE_OF_PREFIX%20%3D%20%7B%22hh%22%3A%20%22household%22%2C%20%22sel%22%3A%20%22selection%22%2C%20%22mem%22%3A%20%22member%22%2C%20%22unit%22%3A%20%22fishing_unit%22%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**%7Bprefix%3A%20table%20for%20table%2C%20prefix%20in%20course.CHILD_PREFIX.items()%7D%7D%0A%20%20%20%20HOUSEHOLD%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%20%20%23%20a%20household%3A%20each%20regional%20database%20numbers%20its%20own%2C%20so%20the%20region%20is%20part%20of%20the%20key%0A%20%20%20%20ROW_ID%20%3D%20%22row_id%22%20%20%23%20the%20capture%20system's%20row%20id%2C%20in%20every%20table%0A%0A%20%20%20%20%23%20The%20survey%20as%20modules%204%20and%205%20read%20it%3A%20every%20region%20in%20one%20table%2C%20removed%20households%20and%20voided%20rows%20left%20out%3B%20the%0A%20%20%20%20%23%20catch's%20key%20parts%20renamed%20to%20the%20unit's%0A%20%20%20%20members%20%3D%20course.members()%0A%20%20%20%20households%20%3D%20course.table(%22household%22)%0A%20%20%20%20units%20%3D%20course.units()%0A%20%20%20%20catch%20%3D%20course.child(%22catch%22)%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%20Stretch%3A%20processing%0A%0A%20%20%20%20For%20when%20you%20have%20finished%20module%204%20(%6030_edit_rules.py%60)%20and%20want%20more.%20Five%20exercises%20that%20sit%20beside%20modules%201%2C%202%20and%204.%20None%20is%20a%20step%20of%20processing%20a%20survey%20on%20its%20own%3B%20each%20is%20a%20check%20or%20a%20tool%20those%20steps%20lean%20on%2C%20and%20the%20modules%20use%20each%20one%20without%20asking%20you%20to%20write%20it%3A%0A%0A%20%20%20%20%7C%20exercise%20%7C%20beside%20%7C%20what%20you%20write%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20S1.1%20%7C%20module%201%2C%20the%20names%20%7C%20a%20variable's%20table%2C%20from%20its%20name%20%7C%0A%20%20%20%20%7C%20S1.2%20%7C%20module%201%2C%20the%20keys%20%7C%20how%20many%20rows%20share%20their%20key%3A%20the%20check%20before%20every%20join%20%7C%0A%20%20%20%20%7C%20S1.3%20%7C%20module%202%2C%20section%201%20%7C%20the%20households%20one%20region's%20household%20table%20still%20holds%2C%20as%20a%20polars%20table%20%7C%0A%20%20%20%20%7C%20S1.4%20%7C%20module%204%2C%20section%202%20%7C%20each%20household's%20member%20rows%20beside%20its%20count%20of%20members%20%7C%0A%20%20%20%20%7C%20S1.5%20%7C%20module%204%2C%20section%205%20%7C%20the%20one%20edit%20a%20program%20makes%20on%20duplicates%3A%20exact%20copies%20dropped%20%7C%0A%0A%20%20%20%20Each%20exercise%20has%20the%20text%2C%20the%20check%2C%20the%20**Try%20it**%20cell%20and%20the%20common%20causes%20it%20had%20in%20its%20module.%20The%20checks%20stand%20on%20their%20own%3A%20do%20them%20in%20any%20order.%0A%0A%20%20%20%20**The%20data.**%20The%20setup%20cell%20reads%20the%20survey%20as%20modules%204%20and%205%20do%3A%20%60members%60%2C%20%60households%60%2C%20%60units%60%20and%20%60catch%60%2C%20every%20region%20in%20one%20table%2C%20the%20removed%20households%20and%20voided%20rows%20left%20out.%20S1.2%20reads%20the%20ten%20tables%20as%20the%20databases%20hold%20them%2C%20removed%20and%20voided%20rows%20included%2C%20as%20module%201%20did%20(%60survey%60%2C%20below)%2C%20and%20S1.3%20opens%20region%2001's%20database%20itself.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20modules%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20If%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%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%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%20the%20voided%20rows.%20course.table()%20adds%20region_code%20to%20the%20tables%20that%20lack%20it%2C%20and%20makes%20the%0A%20%20%20%20%23%20area%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%20units_raw%20%3D%20survey%5B%22fishing_unit%22%5D%20%20%23%20every%20unit%2C%20as%20the%20databases%20hold%20them%0A%20%20%20%20return%20survey%2C%20units_raw%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%20Beside%20module%201%3A%20names%20and%20keys%0A%0A%20%20%20%20Module%201%20read%20a%20variable's%20name%20as%20the%20survey%20writes%20it%3A%20the%20part%20before%20the%20first%20%60_%60%20is%20the%20short%20name%20of%20the%20table%20it%20belongs%20to%2C%20so%20%60mem_age%60%20is%20the%20member%20table's%20and%20%60catch_species%60%20the%20catch%20table's.%20It%20also%20showed%20that%20a%20key%20is%20the%20set%20of%20columns%20that%20names%20one%20row%2C%20and%20that%20a%20household%20is%20its%20region%20**and**%20its%20%60hh_id%60%2C%20because%20each%20regional%20database%20numbers%20its%20own%20households.%20The%20two%20exercises%20below%20turn%20both%20into%20functions.%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%20S1.1%20%C2%B7%20A%20variable's%20table%2C%20from%20its%20name%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20joined%20table%20holds%20columns%20from%20several%20tables%2C%20and%20a%20codebook%20is%20read%20one%20table%20at%20a%20time.%20A%20function%20that%20turns%20a%20variable's%20name%20into%20its%20table%20lets%20you%20look%20any%20column%20up%2C%20and%20tells%20the%20survey's%20variables%20from%20the%20columns%20every%20table%20carries%2C%20such%20as%20the%20capture%20system's%20%60row_id%60.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20variable's%20name%20%E2%86%92%20the%20name%20of%20its%20table%2C%20or%20%60None%60%20when%20the%20name%20carries%20no%20table's%20short%20name.%20The%20setup%20cell%20holds%20the%20short%20names%20in%20%60TABLE_OF_PREFIX%60%2C%20a%20dictionary%20from%20short%20name%20to%20table%3A%20%60%22mem%22%60%20gives%20%60%22member%22%60%2C%20%60%22land%22%60%20gives%20%60%22landing%22%60.%0A%0A%20%20%20%20%7C%20%60name%60%20%7C%20returns%20%7C%20why%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%22mem_age%22%60%20%7C%20%60%22member%22%60%20%7C%20%60mem%60%20is%20the%20member%20table's%20short%20name%20%7C%0A%20%20%20%20%7C%20%60%22land_name%22%60%20%7C%20%60%22landing%22%60%20%7C%20the%20short%20name%20is%20not%20always%20the%20table's%20name%3A%20look%20it%20up%20%7C%0A%20%20%20%20%7C%20%60%22catch_operator_line%22%60%20%7C%20%60%22catch%22%60%20%7C%20a%20child%20table's%20copy%20of%20a%20key%20part%20carries%20the%20child's%20short%20name%20%7C%0A%20%20%20%20%7C%20%60%22hh_id%22%60%20%7C%20%60%22household%22%60%20%7C%20the%20household's%20id%2C%20which%20every%20table%20carries%2C%20is%20named%20for%20the%20household%20%7C%0A%20%20%20%20%7C%20%60%22row_id%22%60%20%7C%20%60None%60%20%7C%20a%20capture-system%20column%20of%20every%20table%3A%20%60row%60%20is%20no%20table's%20short%20name%20%7C%0A%20%20%20%20%7C%20%60%22catch%22%60%20%7C%20%60None%60%20%7C%20a%20table's%20name%20with%20no%20%60_%60%3A%20not%20a%20variable%20%7C%0A%0A%20%20%20%20The%20rule%3A%20cut%20the%20name%20at%20its%20first%20%60_%60.%20It%20names%20a%20table%20when%20there%20is%20a%20%60_%60%2C%20something%20follows%20it%2C%20and%20the%20part%20before%20it%20is%20a%20short%20name%20in%20%60TABLE_OF_PREFIX%60.%0A%0A%20%20%20%20**Tools**%20(Python%20Essentials%2C%20modules%202%20and%206)%3A%20%60name.partition(%22_%22)%60%20gives%20the%20part%20before%20the%20first%20%60_%60%2C%20the%20%60_%60%20itself%20(empty%20text%20when%20there%20is%20none)%20and%20the%20rest%3B%20%60dict.get(key)%60%2C%20which%20gives%20%60None%60%20for%20a%20key%20the%20dictionary%20does%20not%20have%3B%20%60and%60%2C%20which%20stops%20at%20the%20first%20test%20that%20is%20%60False%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20variable_table(name%3A%20str)%20-%3E%20str%20%7C%20None%3A%0A%20%20%20%20%22%22%22The%20table%20a%20variable's%20name%20says%20it%20belongs%20to%20(%22mem_age%22%20is%20%22member%22)%2C%20or%20None%20for%20a%20name%20that%20names%20no%20table.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20cut%20the%20name%20at%20its%20first%20%22_%22%20with%20name.partition(%22_%22)%3A%20the%20short%20name%2C%20the%20%22_%22%2C%20the%20rest%0A%20%20%20%20%23%20Step%202%3A%20if%20there%20was%20a%20%22_%22%20and%20something%20follows%20it%2C%20return%20the%20table%20TABLE_OF_PREFIX%20gives%20the%20short%20name%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20(.get%20gives%20None%20for%20a%20short%20name%20it%20does%20not%20have)%0A%20%20%20%20%23%20Step%203%3A%20otherwise%20return%20None%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.1%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S1.1%22%2C%20variable_table%2C%0A%20%20%20%20%20%20%20%20%5B(f%22variable_table(%7Bn!r%7D)%22%2C%20(n%2C))%20for%20n%20in%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mem_age%22%2C%20%22land_name%22%2C%20%22aid_kind%22%2C%20%22catch_operator_line%22%2C%20%22sel_final_weight%22%2C%20%22unit_scale%22%2C%20%22hh_id%22%2C%20%22boat_engine_hp%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22row_id%22%2C%20%22row_void%22%2C%20%22region_code%22%2C%20%22operator_line%22%2C%20%22catch%22%2C%20%22boat_%22%2C%20%22household%22%2C%0A%20%20%20%20%20%20%20%20%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22names%22%2C%20hint%3D'Cut%20at%20the%20first%20%22_%22%20with%20partition%3B%20if%20there%20is%20a%20%22_%22%20and%20something%20after%20it%2C%20look%20the%20part%20before%20it%20up%20in%20TABLE_OF_PREFIX.'%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20table%20of%20the%20crew's%20sex.%20Try%20others%3A%20%22boat_reg_no%22%2C%20%22gear_type%22%2C%20%22region_code%22.%0A%20%20%20%20try_it(variable_table%2C%20%22crew_sex%22)%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%20S1.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%60'catch'%60%20for%20%60'catch'%60%20%7C%20%60split(%22_%22)%5B0%5D%60%20gives%20the%20whole%20name%20when%20it%20has%20no%20%60_%60.%20%7C%20%60partition(%22_%22)%60%2C%20and%20test%20the%20%60_%60%20%7C%0A%20%20%20%20%7C%20%60'boat'%60%20for%20%60'boat_'%60%20%7C%20Only%20the%20%60_%60%20was%20tested%2C%20not%20what%20follows%20it.%20%7C%20test%20the%20rest%20too%3A%20%60underscore%20and%20rest%60%20%7C%0A%20%20%20%20%7C%20%60'land'%60%20for%20%60'land_name'%60%20%7C%20The%20short%20name%20was%20returned%20as%20the%20table's%20name.%20%7C%20look%20it%20up%3A%20%60TABLE_OF_PREFIX.get(prefix)%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'row'*%20%7C%20%60TABLE_OF_PREFIX%5Bprefix%5D%60%20raises%20for%20a%20short%20name%20the%20dictionary%20does%20not%20have.%20%7C%20%60.get(prefix)%60%2C%20which%20gives%20%60None%60%20%7C%0A%20%20%20%20%7C%20%60None%60%20for%20every%20name%20%7C%20The%20look-up%20used%20the%20whole%20name%2C%20or%20the%20rest%20after%20the%20%60_%60.%20%7C%20look%20up%20the%20part%20before%20the%20first%20%60_%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_()%3A%0A%20%20%20%20_codebook%20%3D%20pd.read_csv(PATHS.codebook)%0A%20%20%20%20_named%20%3D%20%5Battempt(variable_table%2C%20name)%20for%20name%20in%20_codebook%5B%22variable%22%5D%5D%0A%20%20%20%20mo.stop(any(found%20is%20NOT_YET%20for%20found%20in%20_named)%2C%20waiting_for(%22Exercise%20S1.1%22))%0A%20%20%20%20_codebook%20%3D%20_codebook.assign(named%3D_named)%0A%20%20%20%20_by_table%20%3D%20_codebook.groupby(%22table%22%2C%20sort%3DFalse).agg(%0A%20%20%20%20%20%20%20%20variables%3D(%22variable%22%2C%20%22size%22)%2C%0A%20%20%20%20%20%20%20%20named_for_this_table%3D(%22named%22%2C%20lambda%20found%3A%20int((found%20%3D%3D%20_codebook.loc%5Bfound.index%2C%20%22table%22%5D).sum()))%2C%0A%20%20%20%20%20%20%20%20named_for_another%3D(%22named%22%2C%20lambda%20found%3A%20int((found.notna()%20%26%20(found%20!%3D%20_codebook.loc%5Bfound.index%2C%20%22table%22%5D)).sum()))%2C%0A%20%20%20%20%20%20%20%20named_for_none%3D(%22named%22%2C%20lambda%20found%3A%20int(found.isna().sum()))%2C%0A%20%20%20%20)%0A%20%20%20%20_none%20%3D%20sorted(_codebook.loc%5B_codebook%5B%22named%22%5D.isna()%2C%20%22variable%22%5D.unique())%0A%20%20%20%20_other%20%3D%20sorted(_codebook.loc%5B_codebook%5B%22named%22%5D.notna()%20%26%20(_codebook%5B%22named%22%5D%20!%3D%20_codebook%5B%22table%22%5D)%2C%20%22variable%22%5D.unique())%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Your%20function%20on%20every%20variable%20of%20the%20codebook.**%20How%20many%20of%20each%20table's%20variables%20carry%20its%20own%20short%20name%3A%22)%2C%0A%20%20%20%20%20%20%20%20_by_table%2C%0A%20%20%20%20%20%20%20%20mo.md(%22Named%20for%20another%20table%3A%20%22%20%2B%20%22%2C%20%22.join(f%22%60%7Bn%7D%60%22%20for%20n%20in%20_other)%20%2B%20%22%2C%20the%20household's%20id%2C%20which%20every%20table%20carries%20to%20join%20on.%20Named%20for%20no%20table%3A%20%22%0A%20%20%20%20%20%20%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_none)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22.%20They%20are%20columns%20several%20tables%20carry%20under%20one%20name%3A%20the%20capture%20system's%20row%20id%20and%20void%20flag%2C%20the%20area%20codes%2C%20and%20the%20unit's%20key%20part%20%60operator_line%60.%20The%20unit's%20other%20key%20part%2C%20%60unit_no%60%2C%20reads%20as%20the%20unit%20table's%20own%2C%20which%20it%20is%3B%20in%20a%20child%20table%20it%20carries%20the%20child's%20short%20name%2C%20as%20%60catch_unit_no%60%20does.%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%20S1.2%20%C2%B7%20Rows%20that%20share%20a%20key%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20before%20you%20join%20on%20a%20key%2C%20check%20that%20it%20names%20one%20row.%20A%20key%20that%20repeats%20multiplies%20the%20rows%20of%20every%20join%20that%20uses%20it.%20The%20survey's%20rule%20U1%20says%20the%20unit%20key%20is%20unique%2C%20and%20this%20function%20tests%20it.%20It%20counts%20every%20row%20of%20a%20repeated%20key%2C%20the%20first%20one%20included%2C%20because%20an%20editor%20needs%20all%20of%20them%20side%20by%20side%20to%20decide%20which%20to%20keep.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20and%20a%20list%20of%20its%20column%20names%20(the%20key)%20%E2%86%92%20how%20many%20rows%20share%20their%20key%20with%20at%20least%20one%20other%20row.%20%600%60%20when%20the%20key%20is%20unique.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%205101%20%7C%0A%20%20%20%20%7C%2007%20%7C%205101%20%7C%0A%20%20%20%20%7C%2007%20%7C%205103%20%7C%0A%20%20%20%20%7C%2007%20%7C%205103%20%7C%0A%20%20%20%20%7C%2007%20%7C%205103%20%7C%0A%0A%20%20%20%20%60rows_sharing_key(table%2C%20%5B%22hh_id%22%5D)%60%20gives%20%605%60%3A%205101%20twice%20and%205103%20three%20times.%20%60rows_sharing_key(table%2C%20%5B%22region_code%22%2C%20%22hh_id%22%5D)%60%20gives%20%603%60%3A%20the%20three%20rows%20of%20region%2007's%20household%205103.%0A%0A%20%20%20%20**Tools**%20(Arrays%20%26%20DataFrames%2C%20module%205)%3A%20%60table.duplicated(subset%3Dkey%2C%20keep%3DFalse)%60%20is%20%60True%60%20on%20every%20row%20whose%20key%20appears%20more%20than%20once%3B%20without%20%60keep%3DFalse%60%20it%20marks%20only%20the%20repeats%20after%20the%20first.%20%60.sum()%60%20counts%20the%20%60True%60%20values%2C%20and%20%60int(...)%60%20makes%20the%20count%20a%20plain%20number.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20rows_sharing_key(table%3A%20pd.DataFrame%2C%20key%3A%20list%5Bstr%5D)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22How%20many%20rows%20of%20the%20table%20share%20their%20key%20(a%20list%20of%20column%20names)%20with%20at%20least%20one%20other%20row.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20mark%20every%20row%20whose%20key%20appears%20more%20than%20once%2C%20the%20first%20one%20included%0A%20%20%20%20%23%20Step%202%3A%20count%20the%20marked%20rows%20and%20return%20the%20count%20as%20an%20int%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.2%22)%0A%0A%0A%40app.function%0Adef%20small_households()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Five%20households%20of%20two%20regions%2C%20the%20table%20of%20Exercise%20S1.2's%20example%3A%20an%20id%20in%20both%20regions%2C%20and%20one%20household%20three%20times.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2207%22%2C%20%2207%22%2C%20%2207%22%2C%20%2207%22%5D%2C%20%22hh_id%22%3A%20%5B5101%2C%205101%2C%205103%2C%205103%2C%205103%5D%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(survey%2C%20units_raw)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S1.2%22%2C%20rows_sharing_key%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%20households%20above%2C%20key%20%5B'hh_id'%5D%22%2C%20(small_households()%2C%20%5B%22hh_id%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20five%20households%20above%2C%20key%20%5B'region_code'%2C%20'hh_id'%5D%22%2C%20(small_households()%2C%20%5B%22region_code%22%2C%20%22hh_id%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%2C%20key%20%5B'hh_id'%5D%22%2C%20(survey%5B%22household%22%5D%2C%20%5B%22hh_id%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%2C%20key%20%5B'region_code'%2C%20'hh_id'%5D%22%2C%20(survey%5B%22household%22%5D%2C%20%5B%22region_code%22%2C%20%22hh_id%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20units%2C%20key%20%5B'region_code'%2C%20'hh_id'%5D%22%2C%20(units_raw%2C%20%5B%22region_code%22%2C%20%22hh_id%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20units%2C%20key%20UNIT_KEY%22%2C%20(units_raw%2C%20UNIT_KEY))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20and%20keys%22%2C%20hint%3D%22Mark%20every%20row%20of%20a%20repeated%20key%20with%20duplicated(subset%3Dkey%2C%20keep%3DFalse)%2C%20then%20count%20the%20marks.%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%20rows%20of%20the%20unit%20table%20that%20share%20their%20four-part%20key.%20Try%20a%20shorter%20key%3A%20%5B%22region_code%22%2C%20%22hh_id%22%5D.%0A%20%20%20%20try_it(rows_sharing_key%2C%20units_raw%2C%20UNIT_KEY)%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%20S1.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%20%603%60%20for%20the%20five%20households%20by%20%60%5B'hh_id'%5D%60%2C%20%601%60%20by%20the%20two-part%20key%20%7C%20%60keep%3DFalse%60%20is%20missing%2C%20so%20only%20the%20repeats%20after%20the%20first%20are%20counted.%20%7C%20%60duplicated(subset%3Dkey%2C%20keep%3DFalse)%60%20%7C%0A%20%20%20%20%7C%20%602%60%20for%20the%20five%20households%20by%20%60%5B'hh_id'%5D%60%20%7C%20The%20distinct%20keys%20that%20repeat%20were%20counted%20(%60nunique%60%2C%20%60value_counts%60)%2C%20not%20the%20rows.%20%7C%20count%20rows%3A%20%60.sum()%60%20of%20the%20marks%20%7C%0A%20%20%20%20%7C%20%600%60%20everywhere%20%7C%20%60subset%3D%60%20was%20left%20out%2C%20so%20whole%20rows%20were%20compared%2C%20and%20no%20two%20rows%20are%20equal%20in%20every%20column.%20%7C%20%60subset%3Dkey%60%20%7C%0A%20%20%20%20%7C%20*KeyError*%20naming%20a%20column%20%7C%20The%20key's%20names%20are%20written%20differently%20from%20the%20table's%2C%20or%20%60%22key%22%60%20in%20quotes.%20%7C%20%60subset%3Dkey%60%2C%20the%20input%2C%20without%20quotes%20%7C%0A%20%20%20%20%7C%20A%20table%20instead%20of%20a%20number%20%7C%20%60table%5Btable.duplicated(...)%5D%60%20returns%20the%20rows%20themselves.%20%7C%20%60.sum()%60%20on%20the%20marks%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%2C%20units_raw)%3A%0A%20%20%20%20_tests%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22household%22%2C%20%22%60hh_id%60%22%2C%20survey%5B%22household%22%5D%2C%20%5B%22hh_id%22%5D)%2C%0A%20%20%20%20%20%20%20%20(%22household%22%2C%20%22%60region_code%60%2C%20%60hh_id%60%22%2C%20survey%5B%22household%22%5D%2C%20HOUSEHOLD)%2C%0A%20%20%20%20%20%20%20%20(%22selection%22%2C%20%22%60region_code%60%2C%20%60hh_id%60%22%2C%20survey%5B%22selection%22%5D%2C%20HOUSEHOLD)%2C%0A%20%20%20%20%20%20%20%20(%22member%22%2C%20%22%60region_code%60%2C%20%60hh_id%60%2C%20%60mem_line%60%22%2C%20survey%5B%22member%22%5D%2C%20%5B*HOUSEHOLD%2C%20%22mem_line%22%5D)%2C%0A%20%20%20%20%20%20%20%20(%22fishing_unit%22%2C%20%22%60region_code%60%2C%20%60hh_id%60%22%2C%20units_raw%2C%20HOUSEHOLD)%2C%0A%20%20%20%20%20%20%20%20(%22fishing_unit%22%2C%20%22%60UNIT_KEY%60%22%2C%20units_raw%2C%20UNIT_KEY)%2C%0A%20%20%20%20%5D%0A%20%20%20%20_found%20%3D%20%5Battempt(rows_sharing_key%2C%20table%2C%20key)%20for%20_%2C%20_%2C%20table%2C%20key%20in%20_tests%5D%0A%20%20%20%20mo.stop(any(f%20is%20NOT_YET%20for%20f%20in%20_found)%2C%20waiting_for(%22Exercise%20S1.2%22))%0A%20%20%20%20_repeated%20%3D%20int(units_raw.duplicated(subset%3DUNIT_KEY%2C%20keep%3DFalse).sum())%0A%20%20%20%20mo.md(%22**Your%20function%20on%20the%20survey's%20keys%3A**%5Cn%5Cn%7C%20table%20%7C%20key%20%7C%20rows%20%7C%20rows%20that%20share%20their%20key%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%60%7Bname%7D%60%20%7C%20%7Bshown%7D%20%7C%20%7Blen(table)%3A%2C%7D%20%7C%20%7Bfound%3A%2C%7D%20%7C%22%20for%20(name%2C%20shown%2C%20table%2C%20_)%2C%20found%20in%20zip(_tests%2C%20_found))%0A%20%20%20%20%20%20%20%20%20%20%2B%20f%22%5Cn%5CnThe%20four-part%20unit%20key%20repeats%20on%20**%7B_repeated%7D%20rows**%2C%20though%20rule%20U1%20says%20it%20is%20unique.%20Module%204%20found%20those%20duplicates%2C%20and%20S1.5%20below%20drops%20the%20exact%20copies%20among%20them%3B%20until%20they%20are%20dropped%2C%20every%20join%20on%20the%20unit%20key%20repeats%20those%20units'%20rows.%20A%20household%20runs%20one%20unit%20or%20several%2C%20so%20%60region_code%60%20and%20%60hh_id%60%20alone%20repeat%20in%20the%20unit%20table%20by%20design%3A%20that%20pair%20is%20the%20household's%20key%2C%20not%20the%20unit's.%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%20Beside%20module%202%3A%20the%20households%20a%20region's%20table%20still%20holds%0A%0A%20%20%20%20Module%202%2C%20section%201%2C%20showed%20why%20the%20obvious%20filter%20keeps%20no%20household%3A%20%60row_void%60%20is%20blank%20(%60NULL%60)%20for%20a%20household%20not%20voided%20in%20editing%2C%20and%20%60row_void%20%3C%3E%201%60%20is%20%60NULL%60%2C%20not%20true%2C%20for%20a%20blank%20flag%2C%20so%20%60WHERE%60%20drops%20the%20row.%20%60row_void%20IS%20DISTINCT%20FROM%201%60%20says%20what%20a%20blank%20means%3A%20true%20for%20blank%20and%20for%200%2C%20false%20only%20for%201.%20The%20exercise%20below%20writes%20that%20filter%2C%20with%20the%20other%20mark%20the%20household%20table%20holds%2C%20for%20one%20region%2C%20and%20returns%20the%20households%20as%20a%20polars%20table.%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%20One%20region's%20database%2C%20opened%20read-only%0A%20%20%20%20region01%20%3D%20duckdb.connect(str(PATHS.region(%2201%22))%2C%20read_only%3DTrue)%0A%20%20%20%20return%20(region01%2C)%0A%0A%0A%40app.function%0Adef%20small_region()%20-%3E%20duckdb.DuckDBPyConnection%3A%0A%20%20%20%20%22%22%22A%20regional%20database%20of%20five%20households%2C%20in%20memory%2C%20used%20by%20the%20check%20of%20Exercise%20S1.3%3A%20one%20deleted%20in%20the%20capture%0A%20%20%20%20system%2C%20one%20voided%20in%20editing%20(row_void%20%3D%201)%2C%20one%20with%20row_void%20%3D%200%2C%20households%20not%20in%20id%20order%2C%20and%20a%20catch%20table%0A%20%20%20%20with%20no%20rows.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20con.execute(%22%22%22CREATE%20TABLE%20household%20(row_id%20INTEGER%2C%20hh_id%20INTEGER%2C%20hh_code%20VARCHAR%2C%20region_code%20INTEGER%2C%20province_code%20INTEGER%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20citymun_code%20INTEGER%2C%20barangay_code%20INTEGER%2C%20hh_interviewer%20VARCHAR%2C%20hh_deleted_on%20DATE%2C%20row_void%20INTEGER)%22%22%22)%0A%20%20%20%20con.execute(%22%22%22INSERT%20INTO%20household%20VALUES%0A%20%20%20%20%20%20%20%20(1%2C%205201%2C%20'0701210010001'%2C%207%2C%2012%2C%2010%2C%2010%2C%20'INT017'%2C%20NULL%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(2%2C%205202%2C%20'0701210010002'%2C%207%2C%2012%2C%2010%2C%2010%2C%20'INT017'%2C%20NULL%2C%201)%2C%0A%20%20%20%20%20%20%20%20(3%2C%205204%2C%20'0701210020004'%2C%207%2C%2012%2C%2010%2C%2020%2C%20'INT018'%2C%20DATE%20'2026-05-30'%2C%20NULL)%2C%0A%20%20%20%20%20%20%20%20(4%2C%205205%2C%20'0701220030005'%2C%207%2C%2012%2C%2020%2C%2030%2C%20'INT018'%2C%20NULL%2C%200)%2C%0A%20%20%20%20%20%20%20%20(5%2C%205203%2C%20'0701220030003'%2C%207%2C%2012%2C%2020%2C%2030%2C%20'INT017'%2C%20NULL%2C%20NULL)%22%22%22)%0A%20%20%20%20con.execute(%22CREATE%20TABLE%20catch%20(row_id%20INTEGER%2C%20hh_id%20INTEGER%2C%20catch_operator_line%20INTEGER%2C%20catch_unit_no%20INTEGER%2C%20catch_species%20INTEGER)%22)%0A%20%20%20%20return%20con%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%20S1.3%20%C2%B7%20The%20households%20a%20region's%20table%20still%20holds%2C%20as%20a%20polars%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20filter%20is%20the%20first%20line%20of%20every%20query%20on%20the%20survey.%20Written%20with%20%60%3C%3E%60%2C%20it%20keeps%20no%20household%20at%20all%3B%20written%20right%2C%20it%20leaves%20out%20the%20removed%20households%20and%20nothing%20else.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20connection%20to%20one%20regional%20database%20%E2%86%92%20a%20**polars**%20DataFrame%20of%20the%20rows%20of%20%60household%60%20still%20in%20the%20survey%20by%20the%20household%20table's%20own%20two%20marks%2C%20every%20column%2C%20ordered%20by%20%60hh_id%60.%20A%20household%20is%20kept%20when%20%60hh_deleted_on%60%20is%20blank%20(it%20was%20never%20deleted%20in%20the%20capture%20system)%20and%20%60row_void%60%20is%20not%201%20(blank%20or%20any%20other%20value)%3A%0A%0A%20%20%20%20%7C%20hh_id%20%7C%20%E2%80%A6%20%7C%20hh_deleted_on%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%205201%20%7C%20%E2%80%A6%20%7C%20*(blank)*%20%7C%20*(blank)*%20%7C%20yes%20%7C%0A%20%20%20%20%7C%205202%20%7C%20%E2%80%A6%20%7C%20*(blank)*%20%7C%201%20%7C%20no%3A%20voided%20in%20editing%20%7C%0A%20%20%20%20%7C%205204%20%7C%20%E2%80%A6%20%7C%202026-05-30%20%7C%20*(blank)*%20%7C%20no%3A%20deleted%20in%20the%20capture%20system%20%7C%0A%20%20%20%20%7C%205205%20%7C%20%E2%80%A6%20%7C%20*(blank)*%20%7C%200%20%7C%20yes%20%7C%0A%0A%20%20%20%20The%20third%20mark%2C%20%60sel_excluded%60%2C%20is%20in%20the%20selection%20table%3B%20module%202's%20Exercise%202.3%20leaves%20those%20households%20out%20with%20a%20subquery.%0A%0A%20%20%20%20**Tools%3A**%20a%20%60SELECT%20...%20WHERE%20...%20ORDER%20BY%60%20query%2C%20%60IS%20NULL%60%2C%20%60IS%20DISTINCT%20FROM%60%2C%20and%20%60con.sql(query).pl()%60%2C%20all%20from%20module%202%2C%20section%201.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20live_households(con%3A%20duckdb.DuckDBPyConnection)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22The%20household%20rows%20not%20deleted%20(hh_deleted_on%20blank)%20and%20not%20voided%20(row_void%20not%201)%2C%20every%20column%2C%20ordered%20by%0A%20%20%20%20hh_id%2C%20in%20polars.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20query%3A%20every%20column%20of%20household%20where%20hh_deleted_on%20is%20null%20and%20row_void%20is%20distinct%20from%201%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20ordered%20by%20hh_id%0A%20%20%20%20%23%20Step%202%3A%20run%20it%20with%20con.sql(...)%20and%20return%20the%20result%20as%20a%20polars%20table%20(.pl())%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S1.3%22%2C%20live_households%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20database%3A%20households%20deleted%20and%20voided%2C%20one%20with%20row_void%20%3D%200%2C%20not%20in%20id%20order%22%2C%20(small_region()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2001's%20database%22%2C%20(duckdb.connect(str(PATHS.region(%2201%22))%2C%20read_only%3DTrue)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20database%22%2C%20(duckdb.connect(str(PATHS.region(%2204%22))%2C%20read_only%3DTrue)%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22databases%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20hh_deleted_on%20IS%20NULL%20and%20row_void%20IS%20DISTINCT%20FROM%201%2C%20order%20by%20hh_id%2C%20and%20return%20.pl().%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(region01)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20households%20region%2001's%20household%20table%20still%20holds%0A%20%20%20%20try_it(live_households%2C%20region01)%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%20S1.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%20Only%20household%205205%2C%20the%20one%20with%20%60row_void%60%200%20%7C%20%60row_void%20%3C%3E%201%60%3A%20a%20blank%20flag%20makes%20the%20condition%20%60NULL%60%2C%20and%20the%20row%20is%20dropped.%20%7C%20%60row_void%20IS%20DISTINCT%20FROM%201%60%20%7C%0A%20%20%20%20%7C%20A%20table%20with%20no%20rows%20%7C%20%60hh_deleted_on%20%3D%20NULL%60%20is%20never%20true.%20%7C%20%60hh_deleted_on%20IS%20NULL%60%20%7C%0A%20%20%20%20%7C%20Household%205202%20or%205204%20is%20still%20there%20%7C%20One%20of%20the%20two%20marks%20is%20not%20filtered.%20%7C%20both%20conditions%2C%20joined%20with%20%60AND%60%20%7C%0A%20%20%20%20%7C%20Household%205205%20is%20missing%20%7C%20The%20filter%20keeps%20only%20blank%20flags%20(%60row_void%20IS%20NULL%60).%20%7C%20a%20flag%20of%200%20is%20not%20a%20void%20%7C%0A%20%20%20%20%7C%20The%20right%20rows%20in%20another%20order%20%7C%20No%20%60ORDER%20BY%60.%20%7C%20%60ORDER%20BY%20hh_id%60%20%7C%0A%20%20%20%20%7C%20%60gave%20%3Ca%20table%20of%20...%20rows%3E%60%20from%20Try%20it%2C%20but%20the%20check%20is%20red%20%7C%20A%20pandas%20table%3A%20%60.df()%60%20instead%20of%20%60.pl()%60.%20%7C%20%60.pl()%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_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Beside%20module%204%3A%20a%20count%20from%20another%20table%2C%20and%20exact%20copies%0A%0A%20%20%20%20Module%204's%20rules%20read%20one%20table%20each.%20A%20rule%20that%20compares%20two%20tables%2C%20such%20as%20H1%20(a%20household's%20count%20of%20members%20against%20its%20rows%20in%20the%20member%20table)%2C%20needs%20the%20other%20table's%20fact%20as%20a%20column%20first%3B%20S1.4%20computes%20that%20column.%20Module%204%20then%20edits%20by%20rule%20only%20where%20the%20right%20value%20follows%20from%20the%20data%20with%20no%20judgment%3A%20an%20exact%20copy%20of%20a%20row%20is%20the%20same%20answer%20stored%20twice%2C%20and%20dropping%20it%20loses%20nothing.%20S1.5%20is%20that%20edit.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20rows_in_order(table%3A%20pd.DataFrame%2C%20columns%3A%20list%20%7C%20None%20%3D%20None)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20table%20in%20one%20shape%20for%20the%20checks%3A%20these%20columns%20(all%20if%20None)%2C%20rows%20sorted%20by%20every%20column%2C%20numbered%20from%200.%0A%20%20%20%20So%20a%20right%20answer%20passes%20whatever%20order%20its%20rows%20came%20in.%22%22%22%0A%20%20%20%20table%20%3D%20table%20if%20columns%20is%20None%20else%20table%5Bcolumns%5D%0A%20%20%20%20return%20table.sort_values(list(table.columns)%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%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%20S1.4%20%C2%B7%20Member%20rows%20beside%20the%20household's%20count%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20count%20of%20members%20on%20the%20questionnaire's%20cover%20and%20the%20member%20roster%20are%20written%20in%20two%20places.%20When%20they%20disagree%2C%20every%20estimate%20per%20member%20and%20per%20household%20disagrees%20with%20the%20other.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20household%20table%20and%20the%20member%20table%20%E2%86%92%20the%20household%20table%2C%20its%20rows%20unchanged%2C%20with%20a%20new%20column%20**%60member_rows%60**%3A%20the%20number%20of%20the%20household's%20rows%20in%20the%20member%20table%2C%20**0**%20for%20a%20household%20with%20none.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20hh_roster_members%20%7C%20member_rows%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%205300%20%7C%203%20%7C%203%20%7C%0A%20%20%20%20%7C%2004%20%7C%205300%20%7C%202%20%7C%202%20%7C%0A%20%20%20%20%7C%2001%20%7C%205301%20%7C%201%20%7C%200%20%7C%0A%0A%20%20%20%20Two%20households%20share%20the%20%60hh_id%60%205300%2C%20one%20in%20each%20region%3B%20each%20has%20its%20own%20rows.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(HOUSEHOLD).size()%60%2C%20%60merge(...%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%60%2C%20%60fillna(0)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20member_rows(households%3A%20pd.DataFrame%2C%20members%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20household%20table%20with%20member_rows%3A%20the%20household's%20rows%20in%20the%20member%20table%20(0%20if%20none).%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20count%20the%20member%20rows%20of%20each%20household%3A%20group%20members%20by%20HOUSEHOLD%20(region%20and%20hh_id)%2C%20.size()%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20name%20the%20count%20%22member_rows%22%20and%20reset%20the%20index%0A%20%20%20%20%23%20Step%202%3A%20left-join%20the%20counts%20onto%20the%20households%20on%20HOUSEHOLD%2C%20with%20validate%3D%22one_to_one%22%0A%20%20%20%20%23%20Step%203%3A%20fill%20the%20missing%20counts%20with%200%2C%20make%20them%20integers%2C%20and%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.4%22)%0A%0A%0A%40app.function%0Adef%20two_regions_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Three%20households%2C%20two%20of%20them%20with%20the%20hh_id%205300%20in%20different%20regions%20and%20one%20with%20no%20member%20rows%2C%20and%20their%0A%20%20%20%20members.%20Used%20by%20the%20check%20of%20Exercise%20S1.4.%22%22%22%0A%20%20%20%20homes%20%3D%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2204%22%2C%20%2201%22%5D%2C%20%22hh_id%22%3A%20%5B5300%2C%205300%2C%205301%5D%2C%20%22hh_roster_members%22%3A%20%5B3%2C%202%2C%201%5D%7D)%0A%20%20%20%20people%20%3D%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2204%22%5D%2C%20%22hh_id%22%3A%20%5B5300%5D%20*%205%2C%20%22mem_line%22%3A%20%5B1%2C%202%2C%203%2C%201%2C%202%5D%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%22mem_age%22%3A%20%5B40%2C%2038%2C%209%2C%2051%2C%2049%5D%7D)%0A%20%20%20%20return%20homes%2C%20people%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S1.4%22%2C%20member_rows%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20households%3A%205300%20in%20two%20regions%2C%20one%20with%20no%20member%20rows%22%2C%20two_regions_fixture())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20household%20and%20member%20of%20the%20survey%22%2C%20(households%2C%20members))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20households%20of%20region%2004%20and%20every%20member%22%2C%20(households%5Bhouseholds%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D%2C%20members))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20rows_in_order(table%2C%20%5B*HOUSEHOLD%2C%20%22member_rows%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20members%20by%20region%20and%20hh_id%2C%20left-join%20the%20counts%20onto%20the%20households%2C%20and%20fill%20the%20missing%20ones%20with%200.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20households%20whose%20count%20and%20member%20rows%20disagree%0A%20%20%20%20def%20_try_rows()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20counted%20%3D%20member_rows(households%2C%20members)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20apart%20%3D%20counted%5Bcounted%5B%22hh_roster_members%22%5D%20!%3D%20counted%5B%22member_rows%22%5D%5D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bmo.md(f%22**%7Blen(apart)%7D%20households**%20fail%20H1%3A%22)%2C%20apart%5B%5B*HOUSEHOLD%2C%20%22hh_listed_members%22%2C%20%22hh_roster_members%22%2C%20%22member_rows%22%5D%5D%5D)%0A%0A%20%20%20%20_try_rows()%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%20S1.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%205%20for%20both%20households%205300%20%7C%20Grouped%20by%20%60hh_id%60%20alone%3A%20the%20two%20regions'%20households%20added%20together.%20%7C%20%60groupby(HOUSEHOLD)%60%20%7C%0A%20%20%20%20%7C%20Household%205301%20is%20missing%20%7C%20An%20inner%20join%20dropped%20the%20household%20with%20no%20member%20rows.%20%7C%20%60how%3D%22left%22%60%20%7C%0A%20%20%20%20%7C%20%60None%60%20for%20household%205301%20%7C%20The%20missing%20count%20was%20not%20filled.%20%7C%20%60fillna(0)%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'member_rows'*%20%7C%20The%20count%20is%20still%20named%20%600%60%20or%20%60size%60.%20%7C%20%60.rename(%22member_rows%22)%60%20before%20%60.reset_index()%60%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20Merge%20keys%20are%20not%20unique*%20%7C%20The%20members%20themselves%20were%20joined%2C%20not%20their%20counts.%20%7C%20count%20first%2C%20then%20join%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_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S1.5%20%C2%B7%20Drop%20exact%20copies%2C%20keep%20everything%20else%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20one%20edit%20a%20program%20makes%20on%20duplicates.%20It%20must%20drop%20the%20second%20of%20two%20rows%20that%20are%20the%20same%20answer%2C%20and%20nothing%20else%3A%20not%20a%20row%20that%20shares%20a%20key%20but%20differs%2C%20not%20the%20same%20row%20in%20another%20region.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20and%20the%20name%20of%20its%20row%20id%20column%20%E2%86%92%20the%20table%20without%20the%20rows%20that%20are%20equal%20to%20an%20earlier%20row%20**in%20every%20column%20but%20the%20row%20id**%3B%20the%20first%20copy%20is%20kept%2C%20in%20the%20table's%20order%3A%0A%0A%20%20%20%20%7C%20row_id%20%7C%20region_code%20%7C%20hh_id%20%7C%20catch_line%20%7C%20catch_species%20%7C%20%7C%0A%20%20%20%20%7C--%3A%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%201%20%7C%2001%20%7C%205300%20%7C%201%20%7C%20103%20%7C%20kept%20%7C%0A%20%20%20%20%7C%202%20%7C%2001%20%7C%205300%20%7C%201%20%7C%20103%20%7C%20dropped%3A%20row%201%20again%20%7C%0A%20%20%20%20%7C%203%20%7C%2004%20%7C%205300%20%7C%201%20%7C%20103%20%7C%20kept%3A%20another%20region%20%7C%0A%20%20%20%20%7C%204%20%7C%2001%20%7C%205301%20%7C%201%20%7C%20304%20%7C%20kept%20%7C%0A%20%20%20%20%7C%205%20%7C%2001%20%7C%205301%20%7C%202%20%7C%20305%20%7C%20kept%3A%20same%20unit%2C%20another%20species%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20list%20of%20the%20columns%20but%20one%20(%60%5Bc%20for%20c%20in%20table.columns%20if%20c%20!%3D%20id_column%5D%60)%2C%20%60drop_duplicates(subset%3D...%2C%20keep%3D%22first%22)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20drop_exact_copies(table%3A%20pd.DataFrame%2C%20id_column%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20table%20without%20rows%20equal%20to%20an%20earlier%20row%20in%20every%20column%20but%20the%20row%20id%3B%20the%20first%20copy%20is%20kept.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20columns%20to%20compare%3A%20every%20column%20of%20the%20table%20except%20id_column%0A%20%20%20%20%23%20Step%202%3A%20drop%20the%20rows%20equal%20to%20an%20earlier%20row%20on%20those%20columns%20(drop_duplicates%2C%20keep%3D%22first%22)%3B%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.5%22)%0A%0A%0A%40app.function%0Adef%20copies_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Five%20catch%20rows%3A%20an%20exact%20copy%2C%20the%20same%20row%20in%20another%20region%2C%20and%20two%20rows%20of%20one%20unit%20with%20different%0A%20%20%20%20species.%20The%20rows%20of%20Exercise%20S1.5's%20example%20table%2C%20used%20by%20its%20check.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20ROW_ID%3A%20%5B1%2C%202%2C%203%2C%204%2C%205%5D%2C%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2201%22%2C%20%2201%22%5D%2C%20%22hh_id%22%3A%20%5B5300%2C%205300%2C%205300%2C%205301%2C%205301%5D%2C%0A%20%20%20%20%20%20%20%20%22operator_line%22%3A%20%5B1%2C%201%2C%201%2C%201%2C%201%5D%2C%20%22unit_no%22%3A%20%5B1%2C%201%2C%201%2C%201%2C%201%5D%2C%20%22catch_line%22%3A%20%5B1%2C%201%2C%201%2C%201%2C%202%5D%2C%20%22catch_species%22%3A%20%5B103%2C%20103%2C%20103%2C%20304%2C%20305%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S1.5%22%2C%20drop_exact_copies%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22five%20catch%20rows%3A%20an%20exact%20copy%2C%20another%20region%2C%20one%20unit's%20two%20species%22%2C%20(copies_fixture()%2C%20ROW_ID))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20units%22%2C%20(units%2C%20ROW_ID))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20catch%22%2C%20(catch%2C%20ROW_ID))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Drows_in_order%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Compare%20every%20column%20except%20the%20row%20id%2C%20and%20keep%20the%20first%20of%20rows%20equal%20on%20all%20of%20them.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20catch%20without%20its%20exact%20copies%0A%20%20%20%20try_it(drop_exact_copies%2C%20catch%2C%20ROW_ID)%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%20S1.5%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%20No%20row%20dropped%20%7C%20%60drop_duplicates()%60%20compared%20the%20row%20id%20too%2C%20and%20every%20id%20differs.%20%7C%20compare%20every%20column%20but%20%60id_column%60%20%7C%0A%20%20%20%20%7C%20Row%205%20dropped%20%7C%20Compared%20on%20a%20key%20(the%20unit%20and%20%60catch_line%60%20or%20the%20household)%2C%20so%20a%20row%20that%20differs%20in%20its%20species%20went%20too.%20%7C%20every%20column%20but%20the%20id%2C%20not%20a%20key%20%7C%0A%20%20%20%20%7C%20Row%203%20dropped%20%7C%20The%20region%20was%20left%20out%20of%20the%20comparison.%20%7C%20keep%20%60region_code%60%20among%20the%20columns%20compared%20%7C%0A%20%20%20%20%7C%20Row%201%20dropped%2C%20row%202%20kept%20%7C%20%60keep%3D%22last%22%60.%20%7C%20%60keep%3D%22first%22%60%20%7C%0A%20%20%20%20%7C%20The%20id%20column%20is%20gone%20%7C%20It%20was%20dropped%20from%20the%20table%2C%20not%20left%20out%20of%20the%20comparison.%20%7C%20%60subset%3D%60%20names%20the%20columns%3B%20the%20table%20keeps%20them%20all%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_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20these%20five%20add%20up%20to%0A%0A%20%20%20%20%7C%20exercise%20%7C%20what%20it%20checks%20or%20does%20%7C%20the%20habit%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20S1.1%20%7C%20a%20variable's%20table%20%7C%20read%20a%20name%20as%20the%20survey%20writes%20it%3B%20a%20column%20every%20table%20carries%20names%20no%20table%20%7C%0A%20%20%20%20%7C%20S1.2%20%7C%20the%20rows%20that%20share%20a%20key%20%7C%20count%20them%20before%20you%20join%3A%20a%20repeated%20key%20multiplies%20every%20join%20%7C%0A%20%20%20%20%7C%20S1.3%20%7C%20the%20households%20a%20region's%20table%20still%20holds%20%7C%20say%20what%20a%20blank%20flag%20means%3A%20%60IS%20DISTINCT%20FROM%201%60%2C%20%60IS%20NULL%60%20%7C%0A%20%20%20%20%7C%20S1.4%20%7C%20another%20table's%20count%20as%20a%20column%20%7C%20count%20first%2C%20then%20a%20left%20join%20on%20the%20whole%20key%2C%20region%20included%20%7C%0A%20%20%20%20%7C%20S1.5%20%7C%20exact%20copies%20dropped%20%7C%20compare%20every%20column%20but%20the%20row%20id%3B%20keep%20the%20first%20%7C%0A%0A%20%20%20%20Next%3A%20module%205%20(%5B%6040_recode_and_tabulate.py%60%5D(%2F%3Ffile%3D40_recode_and_tabulate.py))%20turns%20the%20edited%20codes%20into%20the%20categories%20a%20table%20is%20made%20of.%0A%20%20%20%20%22%22%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
971d5ed333b996820c4d1d860df2084f