import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Debugging%20drill%3A%20a%20flat%20table%20that%20lies%22%2C%0A)%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%20atexit%0A%20%20%20%20import%20io%0A%20%20%20%20import%20shutil%0A%20%20%20%20import%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%2C%20the%20survey's%20tables%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20AREA_CODE_WIDTH%2C%20PATHS%2C%20REGIONS%2C%20check_cases%2C%20try_it%0A%0A%20%20%20%20%23%20Each%20region's%20files%20in%20the%20database%20export%3A%20HOUSEHOLD_CSV%5B%2204%22%5D%20is%20data%2Fexport%2Fr04%2Fhousehold.csv%0A%20%20%20%20HOUSEHOLD_CSV%20%3D%20%7Breg%3A%20PATHS.export%20%2F%20f%22r%7Breg%7D%22%20%2F%20%22household.csv%22%20for%20reg%20in%20REGIONS%7D%0A%20%20%20%20BOAT_CSV%20%3D%20%7Breg%3A%20PATHS.export%20%2F%20f%22r%7Breg%7D%22%20%2F%20%22boat.csv%22%20for%20reg%20in%20REGIONS%7D%0A%0A%20%20%20%20%23%20The%20survey%20read%20the%20way%20module%202%20reads%20it%3A%20every%20region%20in%20one%20table%2C%20area%20codes%20as%20zero-padded%20text%0A%20%20%20%20households%20%3D%20course.table(%22household%22)%20%20%23%20removed%20households%20left%20out%0A%20%20%20%20selection%20%3D%20course.table(%22selection%22)%20%20%23%20each%20household's%20stratum%2C%20area%20and%20weights%3B%20removed%20ones%20left%20out%0A%20%20%20%20households_with_removed%20%3D%20course.table(%22household%22%2C%20clean%3DFalse)%20%20%23%20every%20household%2C%20the%20removed%20ones%20too%0A%20%20%20%20geography%20%3D%20course.codes(%22geography%22)%20%20%23%20the%20PSGC%20code%20list%3A%20each%20city%20or%20municipality's%20codes%20as%20text%2C%20and%20its%20name%0A%0A%20%20%20%20def%20_stored(reg%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%20%20%20%20with%20duckdb.connect(str(PATHS.region(reg))%2C%20read_only%3DTrue)%20as%20con%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20con.execute(%22SELECT%20*%20FROM%20household%22).df()%0A%0A%20%20%20%20%23%20Each%20region's%20households%20exactly%20as%20its%20database%20stores%20them%3A%2001%20and%2007%20keep%20area%20codes%20as%20numbers%2C%2004%20as%20text%0A%20%20%20%20stored%20%3D%20%7Breg%3A%20_stored(reg)%20for%20reg%20in%20REGIONS%7D%0A%0A%20%20%20%20def%20_folder_with(files%3A%20dict%5Bstr%2C%20str%5D)%20-%3E%20Path%3A%0A%20%20%20%20%20%20%20%20%22%22%22A%20new%20folder%20holding%20copies%20of%20regional%20databases%2C%20as%20%7Bits%20name%20in%20the%20folder%3A%20the%20region%20it%20is%7D.%22%22%22%0A%20%20%20%20%20%20%20%20folder%20%3D%20Path(tempfile.mkdtemp(prefix%3D%22survey-drill-d1-%22))%0A%20%20%20%20%20%20%20%20atexit.register(shutil.rmtree%2C%20folder%2C%20ignore_errors%3DTrue)%0A%20%20%20%20%20%20%20%20for%20name%2C%20reg%20in%20files.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20shutil.copy(PATHS.region(reg)%2C%20folder%20%2F%20name)%0A%20%20%20%20%20%20%20%20return%20folder%0A%0A%20%20%20%20%23%20Two%20folders%20for%20Drill%20D1.6%2C%20as%20a%20computer's%20Downloads%20folder%20can%20look%0A%20%20%20%20FOLDER_07_TWICE%20%3D%20_folder_with(%7B%22sfh2025_r01.duckdb%22%3A%20%2201%22%2C%20%22sfh2025_r04.duckdb%22%3A%20%2204%22%2C%20%22sfh2025_r07.duckdb%22%3A%20%2207%22%2C%20%22sfh2025_r07%20(1).duckdb%22%3A%20%2207%22%7D)%0A%20%20%20%20FOLDER_04_COPY%20%3D%20_folder_with(%7B%22sfh2025_r04.duckdb%22%3A%20%2204%22%2C%20%22sfh2025_r04%20-%20Copy.duckdb%22%3A%20%2204%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%20Debugging%20drill%3A%20a%20flat%20table%20that%20lies%0A%0A%20%20%20%20Six%20steps%20of%20a%20pipeline%20that%20builds%20the%20survey's%20flat%20table%2C%20each%20with%20**one**%20mistake%20in%20it.%20They%20are%20the%20mistakes%20anyone%20makes%20on%20the%20way%20from%20regional%20databases%20to%20one%20table%2C%20the%20step%20module%202%20teaches.%20Two%20of%20them%20stop%20with%20an%20error.%20The%20other%20four%20run%20without%20a%20word%20and%20hand%20back%20a%20table%20or%20a%20number%20that%20looks%20fine%20and%20is%20wrong%3A%20a%20flat%20table%20that%20lies%2C%20and%20every%20estimate%20made%20from%20it%20lies%20too.%20Each%20check%20below%20starts%20**red**%3A%20that%20is%20the%20point.%20For%20each%20step%3A%0A%0A%20%20%20%201.%20**predict**%3A%20read%20the%20task%20and%20the%20function%2C%20and%20write%20down%20what%20you%20expect%20it%20to%20give%3B%0A%20%20%20%202.%20**run**%20it%20with%20its%20**Try%20it**%20cell%2C%20and%20compare%20what%20it%20gives%20with%20what%20you%20predicted%3B%0A%20%20%20%203.%20**trace**%3A%20find%20the%20line%20that%20makes%20the%20difference%2C%20with%20the%20cell%20the%20drill%20gives%20you%20for%20it%3B%0A%20%20%20%204.%20**fix**%20it%2C%20run%20the%20function's%20cell%20again%2C%20and%20watch%20the%20check%20turn%20green%3B%0A%20%20%20%205.%20**say**%20what%20the%20mistake%20was%2C%20in%20one%20sentence%3A%20to%20your%20table%20in%20a%20training%2C%20or%20in%20a%20comment%20above%20your%20fix.%0A%0A%20%20%20%20When%20something%20stops%20with%20an%20error%2C%20read%20the%20error%20from%20its%20**last%20line**%20first%3A%20it%20names%20the%20kind%20of%20error%20and%20what%20went%20wrong%3B%20the%20lines%20above%20it%20say%20where.%20The%20hints%20say%20what%20you%20see%2C%20never%20what%20to%20change.%20Everything%20you%20need%20was%20taught%20in%20modules%201%20to%204%2C%20and%20in%20Arrays%20%26%20DataFrames%20before%20them%3B%20each%20task%20links%20to%20the%20section.%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%201%20%C2%B7%20A%20flat%20table%20that%20lies%3A%20six%20bugs%20to%20find%20and%20fix%0A%0A%20%20%20%20The%20setup%20cell%20at%20the%20top%20has%20read%20the%20survey%20the%20way%20module%202%20does%2C%20and%20kept%20a%20few%20raw%20copies%20for%20the%20drill%3A%0A%0A%20%20%20%20%7C%20name%20%7C%20what%20it%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60households%60%20%7C%20every%20region's%20households%20(%60household%60)%20in%20one%20table%2C%20the%20removed%20ones%20left%20out%2C%20area%20codes%20as%20zero-padded%20text%20%7C%0A%20%20%20%20%7C%20%60selection%60%20%7C%20every%20region's%20sample%20records%20(%60selection%60)%3A%20stratum%2C%20enumeration%20area%20and%20weights%2C%20the%20removed%20households%20left%20out%20%7C%0A%20%20%20%20%7C%20%60households_with_removed%60%20%7C%20every%20region's%20households%20with%20the%20removed%20ones%20still%20in%20%7C%0A%20%20%20%20%7C%20%60stored%60%20%7C%20each%20region's%20households%20exactly%20as%20its%20database%20stores%20them%3A%20%60stored%5B%2201%22%5D%60%2C%20%60stored%5B%2204%22%5D%60%2C%20%60stored%5B%2207%22%5D%60%20%7C%0A%20%20%20%20%7C%20%60geography%60%20%7C%20the%20PSGC%20code%20list%3A%20each%20city%20or%20municipality's%20codes%20as%20text%2C%20and%20its%20name%20%7C%0A%20%20%20%20%7C%20%60HOUSEHOLD_CSV%60%2C%20%60BOAT_CSV%60%20%7C%20where%20each%20region's%20export%20files%20are%3A%20%60HOUSEHOLD_CSV%5B%2204%22%5D%60%20is%20%60data%2Fexport%2Fr04%2Fhousehold.csv%60%20%7C%0A%20%20%20%20%7C%20%60FOLDER_07_TWICE%60%2C%20%60FOLDER_04_COPY%60%20%7C%20two%20folders%20of%20regional%20databases%2C%20for%20the%20last%20drill%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%23%20Drill%20D1.1%20%C2%B7%20A%20code%20that%20loses%20its%20leading%20zero%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%60hh_code%60%20is%20how%20a%20household%20is%20found%20in%20the%20listing%2C%20and%20its%20first%20seven%20digits%20are%20its%20city%20or%20municipality's%20place%20in%20the%20PSGC%20(the%20barangay's%20three%20digits%20after%20them%20are%20the%20course's%20own%20numbering).%20A%20code%20that%20loses%20a%20digit%20matches%20nothing%2C%20without%20an%20error.%0A%0A%20%20%20%20%60hh_code%60%20is%2013%20digits%20of%20text%3A%20the%20region%20(2)%2C%20province%20(3)%2C%20city%20or%20municipality%20(2)%20and%20barangay%20(3)%20codes%2C%20then%20the%20household's%20number%20in%20its%20barangay%20(3).%20Region%2004's%20first%20household%20is%20%600401028032001%60.%20%60with_psgc(path)%60%20should%20read%20a%20region's%20%60household.csv%60%20from%20the%20export%2C%20keep%20%60hh_code%60%20as%20that%2013-digit%20text%2C%20and%20add%20%60psgc%60%2C%20the%2010-digit%20PSGC%20code%20of%20the%20household's%20city%20or%20municipality%3A%20the%20first%20seven%20digits%20of%20%60hh_code%60%2C%20then%20%60000%60.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20a%20region's%20%60household.csv%60%20%E2%86%92%20its%20households%2C%20with%20%60hh_code%60%20as%20text%20and%20a%20%60psgc%60%20column.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60with_psgc(HOUSEHOLD_CSV%5B%2204%22%5D)%5B%22psgc%22%5D.head(3).tolist()%60%20%7C%20%60%5B'0401028000'%2C%20'0401028000'%2C%20'0401007000'%5D%60%20%7C%0A%20%20%20%20%7C%20%60with_psgc(HOUSEHOLD_CSV%5B%2204%22%5D)%5B%22psgc%22%5D.isin(geography%5B%22psgc%22%5D).sum()%60%20%7C%20%60736%60%3A%20every%20household's%20city%20or%20municipality%20is%20in%20the%20code%20list%20%7C%0A%0A%20%20%20%20Tools%3A%20codes%20kept%20whole%20(%5Bmodule%202%2C%20section%203%5D(%2F%3Ffile%3D10_flat_table.py)).%0A%0A%20%20%20%20**Predict.**%20What%20will%20the%20function%20give%20as%20the%20first%20household's%20%60psgc%60%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20with_psgc(path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20region's%20households%20from%20the%20export%2C%20hh_code%20as%2013-digit%20text%2C%20each%20with%20its%20city%20or%20municipality's%20PSGC%20code.%22%22%22%0A%20%20%20%20%23%20Drill%20D1.1%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20table%20%3D%20pd.read_csv(path%2C%20encoding%3D%22cp1252%22)%0A%20%20%20%20table%5B%22psgc%22%5D%20%3D%20table%5B%22hh_code%22%5D.astype(str).str%5B%3A7%5D%20%2B%20%22000%22%0A%20%20%20%20return%20table%0A%0A%0A%40app.function%0Adef%20drill_households_csv()%20-%3E%20io.StringIO%3A%0A%20%20%20%20%22%22%22A%20small%20household%20file%2C%20as%20text%3A%20one%20household%20in%20region%2004%2C%20one%20in%20region%2011.%22%22%22%0A%20%20%20%20return%20io.StringIO(%0A%20%20%20%20%20%20%20%20%22row_id%2Chh_id%2Chh_code%2Cregion_code%2Cprovince_code%2Ccitymun_code%2Cbarangay_code%2Chh_interviewer%2Chh_interview_date%2Chh_interview_minutes%2Chh_listed_members%2Chh_roster_members%2Chh_deleted_on%2Crow_void%5Cn%22%0A%20%20%20%20%20%20%20%20%221%2C5204%2C0401028032001%2C04%2C010%2C28%2C032%2CINT009%2C2026-04-13%2C66%2C3%2C3%2C%5C%5CN%2C%5C%5CN%5Cn%22%0A%20%20%20%20%20%20%20%20%222%2C8142%2C1102403005001%2C11%2C024%2C03%2C005%2CINT033%2C2026-03-20%2C48%2C4%2C4%2C%5C%5CN%2C%5C%5CN%5Cn%22%0A%20%20%20%20)%0A%0A%0A%40app.function%0Adef%20psgc_view(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Drill%20D1.1%20compares%3A%20each%20household's%20code%20and%20its%20PSGC%20code.%22%22%22%0A%20%20%20%20return%20table%5B%5B%22hh_code%22%2C%20%22psgc%22%5D%5D%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%22Drill%20D1.1%22%2C%20with_psgc%2C%0A%20%20%20%20%20%20%20%20%5B(%22region%2001's%20household.csv%22%2C%20(HOUSEHOLD_CSV%5B%2201%22%5D%2C))%2C%20(%22region%2004's%20household.csv%22%2C%20(HOUSEHOLD_CSV%5B%2204%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22a%20file%20with%20a%20region%2004%20household%20and%20a%20region%2011%20household%22%2C%20(drill_households_csv()%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22files%22%2C%20summarize%3Dpsgc_view%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Look%20at%20the%20%60hh_code%60%20values%20the%20function%20read%3A%20how%20many%20digits%20do%20they%20have%3F%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%20read%20region%2004's%20households%2C%20and%20look%20at%20hh_code%20and%20psgc%20in%20the%20first%20rows.%0A%20%20%20%20try_it(with_psgc%2C%20HOUSEHOLD_CSV%5B%2204%22%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**Trace.**%20The%20PSGC%20code%20is%20cut%20from%20%60hh_code%60%2C%20so%20look%20at%20%60hh_code%60%20as%20%60read_csv%60%20gives%20it%2C%20without%20any%20options%2C%20and%20at%20the%20type%20pandas%20chose%20for%20it%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%20Trace%3A%20hh_code%20as%20read_csv%20reads%20it%20by%20itself%2C%20and%20its%20type%0A%20%20%20%20_codes%20%3D%20pd.read_csv(HOUSEHOLD_CSV%5B%2204%22%5D%2C%20encoding%3D%22cp1252%22)%5B%22hh_code%22%5D%0A%20%20%20%20_codes.head(3).tolist()%2C%20_codes.dtype%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%20Twelve%20digits%2C%20and%20the%20type%20%60int64%60%3A%20whole%20numbers.%20The%20file%20says%20%600401028032001%60%2C%20but%20a%20number%20has%20no%20leading%20zero%20to%20keep%2C%20and%20every%20step%20after%20that%20worked%20on%20the%20wrong%20digits.%20Module%202%2C%20section%203%20shows%20how%20to%20tell%20%60read_csv%60%20the%20type%20of%20one%20column.%0A%0A%20%20%20%20DuckDB's%20%60read_csv%60%20reads%20this%20same%20file's%20%60hh_code%60%20as%20text%20(%60VARCHAR%60)%2C%20because%20the%20rows%20it%20samples%20start%20with%200.%20It%20decides%20from%20a%20sample%2C%20so%20a%20file%20whose%20first%20rows%20have%20no%20leading%20zero%20can%20still%20lose%20the%20zeros%20further%20down.%20Naming%20the%20type%2C%20with%20%60dtype%3D%60%20in%20pandas%20or%20%60types%3D%60%20in%20DuckDB%2C%20is%20the%20one%20way%20that%20does%20not%20depend%20on%20the%20rows.%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.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.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%60hh_code%60%20has%2012%20digits%2C%20%60psgc%60%20starts%20with%204%20%7C%20The%20column%20is%20still%20read%20as%20numbers.%20%7C%20%60read_csv%60's%20%60dtype%3D%60%20for%20%60hh_code%60%20%7C%0A%20%20%20%20%7C%20%60psgc%60%20is%20right%2C%20but%20%60hh_code%60%20still%20has%2012%20digits%20%7C%20The%20zero%20was%20put%20back%20in%20%60psgc%60%20only.%20The%20task%20keeps%20%60hh_code%60%20itself%20as%2013-digit%20text.%20%7C%20where%20%60hh_code%60%20is%20read%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20Can%20only%20use%20.str%20accessor%20with%20string%20values*%20%7C%20%60.astype(str)%60%20was%20removed%20while%20%60hh_code%60%20is%20still%20a%20number.%20%7C%20read%20%60hh_code%60%20as%20text%20from%20the%20start%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%23%20Drill%20D1.2%20%C2%B7%20A%20join%20that%20multiplied%20rows%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20flat%20table%20carries%20each%20household's%20weight%2C%20and%20every%20total%20is%20a%20sum%20of%20weights.%20A%20join%20that%20repeats%20a%20household%20counts%20it%20two%20or%20three%20times%2C%20and%20nothing%20about%20the%20table%20looks%20wrong.%0A%0A%20%20%20%20%60with_weight(households%2C%20selection)%60%20should%20give%20every%20household%20its%20final%20weight%2C%20%60sel_final_weight%60%2C%20from%20the%20%60selection%60%20table%3A%20one%20row%20per%20household%2C%20in%20the%20order%20of%20%60households%60.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20households%20and%20the%20selection%20table%20%E2%86%92%20the%20households%2C%20each%20with%20%60sel_final_weight%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60len(with_weight(households%2C%20selection))%60%20%7C%20%602180%60%3A%20as%20many%20rows%20as%20%60households%60%20%7C%0A%20%20%20%20%7C%20%60round(with_weight(households%2C%20selection)%5B%22sel_final_weight%22%5D.sum())%60%20%7C%20%6022485%60%3A%20the%20weighted%20number%20of%20fishing%20households%20in%20the%20three%20regions%20%7C%0A%0A%20%20%20%20Tools%3A%20the%20survey's%20keys%20(%5Bmodule%201%2C%20section%202%5D(%2F%3Ffile%3D00_start.py))%2C%20keys%20of%20several%20parts%20(%5Bmodule%203%2C%20section%203%5D(%2F%3Ffile%3D20_key_matching.py)).%0A%0A%20%20%20%20**Predict.**%20%60households%60%20has%202%2C180%20rows.%20How%20many%20will%20the%20joined%20table%20have%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20with_weight(households%3A%20pd.DataFrame%2C%20selection%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20household%20with%20its%20final%20weight%2C%20sel_final_weight%3A%20one%20row%20per%20household.%22%22%22%0A%20%20%20%20%23%20Drill%20D1.2%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20return%20households.merge(selection%5B%5B%22hh_id%22%2C%20%22sel_final_weight%22%5D%5D%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22)%0A%0A%0A%40app.function%0Adef%20drill_households()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Two%20households%20with%20the%20same%20id%20in%20two%20regions%2C%20as%20the%20survey's%20databases%20number%20them.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2204%22%5D%2C%20%22hh_id%22%3A%20%5B5250%2C%205250%5D%2C%20%22hh_code%22%3A%20%5B%220103310008004%22%2C%20%220401028032007%22%5D%7D)%0A%0A%0A%40app.function%0Adef%20drill_selection()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20selection%20records%20of%20drill_households()%2C%20each%20with%20its%20own%20weight.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2204%22%5D%2C%20%22hh_id%22%3A%20%5B5250%2C%205250%5D%2C%20%22sel_final_weight%22%3A%20%5B1.0%2C%2025.5%5D%7D)%0A%0A%0A%40app.function%0Adef%20weight_view(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Drill%20D1.2%20compares%3A%20each%20household's%20key%20and%20its%20weight.%22%22%22%0A%20%20%20%20return%20table%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22sel_final_weight%22%5D%5D%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%22Drill%20D1.2%22%2C%20with_weight%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20survey's%20households%20and%20selection%20records%22%2C%20(households%2C%20selection))%2C%0A%20%20%20%20%20%20%20%20%20(%22region%2004's%20households%20only%22%2C%20(households%5Bhouseholds%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D%2C%20selection))%2C%0A%20%20%20%20%20%20%20%20%20(%22household%205250%20in%20region%2001%20and%20household%205250%20in%20region%2004%22%2C%20(drill_households()%2C%20drill_selection()))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dweight_view%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20the%20rows%20the%20function%20gives%20back%2C%20and%20compare%20with%20the%20rows%20it%20was%20given.%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%20give%20every%20household%20its%20weight%2C%20and%20compare%20the%20number%20of%20rows%20with%20len(households).%0A%20%20%20%20try_it(with_weight%2C%20households%2C%20selection)%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**Trace.**%20More%20rows%20came%20out%20than%20went%20in%2C%20so%20some%20household%20met%20more%20than%20one%20selection%20record.%20A%20join%20key%20should%20name%20one%20row%20of%20the%20table%20it%20looks%20up.%20Count%20how%20often%20an%20id%20repeats%20in%20%60households%60%2C%20alone%20and%20with%20its%20region%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%20Trace%3A%20households%20whose%20id%20is%20also%20another%20household's%20id%2C%20by%20the%20id%20alone%20and%20by%20region%20and%20id%0A%20%20%20%20%7B%22households%22%3A%20len(households)%2C%0A%20%20%20%20%20%22repeats%20of%20hh_id%22%3A%20int(households.duplicated(%22hh_id%22).sum())%2C%0A%20%20%20%20%20%22repeats%20of%20region_code%20and%20hh_id%22%3A%20int(households.duplicated(%5B%22region_code%22%2C%20%22hh_id%22%5D).sum())%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%20Most%20ids%20are%20there%20more%20than%20once%3A%20each%20regional%20database%20numbers%20its%20own%20households%2C%20and%20their%20numbers%20overlap.%20With%20its%20region%2C%20no%20household%20repeats.%20Module%201%2C%20section%202%20names%20the%20survey's%20keys%2C%20and%20module%203%2C%20section%203%20joins%20on%20a%20key%20of%20several%20parts.%20The%20%60validate%3D%60%20that%20Arrays%20%26%20DataFrames%20taught%20in%20its%20module%204%20would%20have%20stopped%20this%20join%20instead%20of%20letting%20it%20multiply.%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.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.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%20More%20rows%20than%20%60households%60%2C%20weights%20summing%20to%20far%20more%20%7C%20The%20join%20is%20on%20%60hh_id%60%20alone%2C%20which%20repeats%20across%20regions.%20%7C%20%60on%3D%60%20with%20both%20key%20parts%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'region_code'*%20%7C%20%60on%3D%60%20names%20%60region_code%60%2C%20but%20the%20columns%20taken%20from%20%60selection%60%20don't%20include%20it.%20%7C%20the%20list%20of%20%60selection%60's%20columns%20%7C%0A%20%20%20%20%7C%20Columns%20%60region_code_x%60%20and%20%60region_code_y%60%20%7C%20%60region_code%60%20came%20from%20both%20tables%20but%20is%20not%20in%20%60on%3D%60.%20%7C%20%60on%3D%60%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20...%20not%20a%20one-to-one%20merge*%20%7C%20%60validate%3D%60%20found%20a%20key%20that%20repeats%3A%20the%20join%20is%20still%20on%20part%20of%20the%20key.%20%7C%20%60on%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_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D1.3%20%C2%B7%20A%20removal%20filter%20that%20keeps%20everything%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20removed%20household%20should%20not%20be%20in%20the%20survey's%20tables%20at%20all%3A%20a%20household%20interviewed%20twice%2C%20one%20out%20of%20scope%2C%20an%20interview%20withdrawn.%20Left%20in%2C%20it%20is%20counted%20with%20its%20weight%20like%20any%20other.%0A%0A%20%20%20%20The%20%60household%60%20table%20marks%20a%20removed%20household%20in%20two%20ways%3A%20%60hh_deleted_on%60%20holds%20the%20day%20it%20was%20deleted%20in%20the%20capture%20system%20(blank%20if%20it%20never%20was)%2C%20and%20%60row_void%20%3D%201%60%20says%20it%20was%20voided%20in%20editing%20(blank%20otherwise).%20%60without_removed(households)%60%20should%20give%20back%20the%20households%20that%20are%20marked%20**neither**%20way.%20(A%20third%20mark%2C%20%60sel_excluded%60%20in%20the%20%60selection%60%20table%2C%20belongs%20to%20the%20sampling%20side%3B%20%60course.table()%60%20leaves%20out%20all%20three%2C%20as%20module%202%20does.)%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20household%20table%20%E2%86%92%20the%20same%20rows%2C%20without%20any%20household%20removed%20either%20way.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60len(without_removed(households_with_removed))%60%20%7C%20%602186%60%20%7C%0A%20%20%20%20%7C%20%60without_removed(households_with_removed)%5B%22hh_deleted_on%22%5D.notna().sum()%60%20%7C%20%600%60%3A%20no%20household%20deleted%20in%20the%20capture%20system%20is%20left%20%7C%0A%0A%20%20%20%20Tools%3A%20deleted%20rows%20filtered%20(%5Bmodule%202%2C%20section%202%5D(%2F%3Ffile%3D10_flat_table.py)).%0A%0A%20%20%20%20**Predict.**%20Read%20the%20function's%20filter%20aloud.%20Which%20households%20does%20it%20keep%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20without_removed(households%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20households%20neither%20deleted%20in%20the%20capture%20system%20nor%20voided%20in%20editing.%22%22%22%0A%20%20%20%20deleted%20%3D%20households%5B%22hh_deleted_on%22%5D.notna()%20%20%23%20deleted%20in%20the%20capture%20system%3A%20the%20day%20is%20filled%20in%0A%20%20%20%20voided%20%3D%20households%5B%22row_void%22%5D.eq(1).fillna(False)%20%20%23%20voided%20in%20editing%3B%20a%20blank%20row_void%20is%20not%20voided%0A%20%20%20%20%23%20Drill%20D1.3%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20return%20households%5B~deleted%20%7C%20~voided%5D%0A%0A%0A%40app.function%0Adef%20drill_marks()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Four%20households%3A%20one%20kept%2C%20one%20deleted%20in%20the%20capture%20system%2C%20one%20voided%20in%20editing%2C%20one%20both.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2207%22%5D%2C%20%22hh_id%22%3A%20%5B1%2C%202%2C%203%2C%204%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%22hh_deleted_on%22%3A%20pd.to_datetime(%5BNone%2C%20%222026-05-20%22%2C%20None%2C%20%222026-06-02%22%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%22row_void%22%3A%20%5BNone%2C%20None%2C%201%2C%201%5D%7D).astype(%7B%22row_void%22%3A%20%22Int32%22%7D)%0A%0A%0A%40app.function%0Adef%20marks_view(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Drill%20D1.3%20compares%3A%20each%20household's%20key%20and%20its%20two%20marks.%22%22%22%0A%20%20%20%20return%20table%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22hh_deleted_on%22%2C%20%22row_void%22%5D%5D%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%22Drill%20D1.3%22%2C%20without_removed%2C%0A%20%20%20%20%20%20%20%20%5B(%22every%20household%2C%20the%20removed%20ones%20too%22%2C%20(households_with_removed%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22four%20households%3A%20kept%2C%20deleted%2C%20voided%2C%20both%22%2C%20(drill_marks()%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22households%20with%20nothing%20removed%22%2C%20(households%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dmarks_view%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20the%20rows%20that%20come%20back%2C%20and%20look%20for%20a%20household%20with%20a%20date%20in%20%60hh_deleted_on%60%20or%20%60row_void%60%201%20among%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%20leave%20out%20the%20removed%20households%2C%20and%20count%20the%20rows%20that%20come%20back.%0A%20%20%20%20try_it(without_removed%2C%20households_with_removed)%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**Trace.**%20The%20function%20gave%20back%20every%20row%20it%20was%20given.%20Count%20the%20households%20each%20mark%20points%20at%2C%20and%20the%20households%20that%20carry%20both%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%20Trace%3A%20how%20many%20households%20each%20mark%20removes%2C%20and%20how%20many%20carry%20both%20marks%0A%20%20%20%20_deleted%20%3D%20households_with_removed%5B%22hh_deleted_on%22%5D.notna()%0A%20%20%20%20_voided%20%3D%20households_with_removed%5B%22row_void%22%5D.eq(1).fillna(False)%0A%20%20%20%20%7B%22households%22%3A%20len(households_with_removed)%2C%20%22deleted%20in%20the%20capture%20system%22%3A%20int(_deleted.sum())%2C%0A%20%20%20%20%20%22voided%20in%20editing%22%3A%20int(_voided.sum())%2C%20%22both%22%3A%20int((_deleted%20%26%20_voided).sum())%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%20No%20household%20carries%20both%20marks.%20%60~deleted%60%20is%20True%20for%20every%20household%20not%20deleted%20in%20the%20capture%20system%2C%20the%20voided%20ones%20among%20them%2C%20and%20%60~voided%60%20for%20every%20household%20not%20voided%2C%20the%20deleted%20ones%20among%20them.%20Joined%20with%20%60%7C%60%2C%20one%20of%20the%20two%20is%20True%20for%20every%20household%20in%20the%20table.%20Say%20the%20rule%20in%20words%20first%2C%20%22leave%20out%20a%20household%20deleted%20in%20the%20capture%20system%20or%20voided%20in%20editing%22%2C%20and%20negate%20the%20whole%20of%20it.%20Module%202%2C%20section%202%20filters%20the%20removed%20rows%20in%20one%20query.%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.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.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%20Every%20row%20comes%20back%20%7C%20%60~deleted%20%5C%7C%20~voided%60%3A%20each%20household%20is%20missing%20one%20mark%20or%20the%20other.%20%7C%20%60~(deleted%20%5C%7C%20voided)%60%2C%20or%20%60%26%60%20between%20the%20two%20%60~%60%20%7C%0A%20%20%20%20%7C%20Only%20the%20removed%20households%20come%20back%20%7C%20The%20%60~%60%20was%20dropped%3A%20the%20filter%20now%20picks%20the%20households%20to%20leave%20out.%20%7C%20negate%20the%20condition%20%7C%0A%20%20%20%20%7C%20No%20household%20comes%20back%2C%20and%20no%20error%20%7C%20%60.fillna(False)%60%20was%20removed%3A%20a%20blank%20%60row_void%60%20gives%20%60%3CNA%3E%60%20instead%20of%20False%2C%20and%20a%20filter%20keeps%20no%20row%20whose%20condition%20is%20%60%3CNA%3E%60.%20%7C%20keep%20the%20line%20that%20builds%20%60voided%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%23%20Drill%20D1.4%20%C2%B7%20The%20null%20token%20read%20as%20text%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20boat's%20gross%20tonnage%20decides%20whether%20its%20fishing%20unit%20is%20small-scale%20or%20large-scale%2C%20split%20at%205.0%20gross%20tons.%20It%20is%20computed%20from%20the%20boat's%20dimensions%2C%20and%20in%20the%20export%20a%20dimension%20that%20was%20not%20measured%20is%20not%20blank%3A%20it%20is%20%60%5CN%60.%0A%0A%20%20%20%20The%20database%20export%20writes%20every%20missing%20value%20as%20%60%5CN%60%2C%20the%20**null%20token**.%20A%20boat's%20size%20comes%20from%20its%20registration%20papers%20(%60boat_measured_in%60%20%3D%201)%2C%20or%20it%20was%20measured%20in%20meters%20(2)%20or%20in%20feet%20(3)%3B%20%60boat_length%60%2C%20%60boat_beam%60%20and%20%60boat_depth%60%20hold%20the%20dimensions%20in%20that%20unit%2C%20and%20%60%5CN%60%20for%20a%20boat%20sized%20from%20its%20papers.%20%60boats_with_tonnage(path)%60%20should%20read%20a%20region's%20%60boat.csv%60%20and%20add%20%60gt%60%2C%20the%20gross%20tonnage%20from%20the%20dimensions%20as%20the%20survey's%20manual%20sets%20it%20(the%20codebook's%20rule%20B3)%3A%20length%20%C3%97%20beam%20%C3%97%20depth%20%C3%97%200.212%20in%20meters%2C%20or%20%C3%97%200.006%20in%20feet%2C%20and%20missing%20for%20a%20boat%20sized%20from%20its%20papers.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20a%20region's%20boats%20file%20%E2%86%92%20its%20boats%2C%20with%20%60gt%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60boats_with_tonnage(BOAT_CSV%5B%2201%22%5D)%5B%22gt%22%5D.round(2).head(3).tolist()%60%20%7C%20%60%5Bnan%2C%2047.66%2C%203.15%5D%60%3A%20the%20first%20boat%20is%20sized%20from%20its%20papers%20%7C%0A%20%20%20%20%7C%20%60boats_with_tonnage(BOAT_CSV%5B%2201%22%5D)%5B%22gt%22%5D.notna().sum()%60%20%7C%20%60807%60%3A%20every%20boat%20measured%20in%20meters%20or%20in%20feet%20%7C%0A%0A%20%20%20%20Tools%3A%20null%20tokens%20(%5Bmodule%202%2C%20section%203%5D(%2F%3Ffile%3D10_flat_table.py)).%0A%0A%20%20%20%20**Predict.**%20Will%20the%20function%20run%3F%20If%20it%20stops%2C%20on%20which%20line%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20boats_with_tonnage(path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20region's%20boats%20from%20the%20export%2C%20each%20with%20its%20gross%20tonnage%20computed%20from%20its%20dimensions.%22%22%22%0A%20%20%20%20%23%20Drill%20D1.4%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20boats%20%3D%20pd.read_csv(path%2C%20encoding%3D%22cp1252%22)%0A%20%20%20%20factor%20%3D%20boats%5B%22boat_measured_in%22%5D.map(%7B2%3A%200.212%2C%203%3A%200.006%7D)%20%20%23%20gross%20tons%20per%20cubic%20meter%2C%20per%20cubic%20foot%0A%20%20%20%20boats%5B%22gt%22%5D%20%3D%20boats%5B%22boat_length%22%5D%20*%20boats%5B%22boat_beam%22%5D%20*%20boats%5B%22boat_depth%22%5D%20*%20factor%0A%20%20%20%20return%20boats%0A%0A%0A%40app.function%0Adef%20drill_boats_csv()%20-%3E%20io.StringIO%3A%0A%20%20%20%20%22%22%22A%20small%20boats%20file%2C%20as%20text%3A%20one%20boat%20sized%20from%20its%20papers%2C%20one%20measured%20in%20meters%2C%20one%20in%20feet.%22%22%22%0A%20%20%20%20return%20io.StringIO(%0A%20%20%20%20%20%20%20%20%22row_id%2Chh_id%2Cboat_operator_line%2Cboat_unit_no%2Cboat_no%2Cboat_measured_in%2Cboat_length%2Cboat_beam%2Cboat_depth%5Cn%22%0A%20%20%20%20%20%20%20%20%221%2C5012%2C1%2C1%2C1%2C1%2C%5C%5CN%2C%5C%5CN%2C%5C%5CN%5Cn%22%0A%20%20%20%20%20%20%20%20%222%2C5012%2C1%2C1%2C2%2C2%2C7.0%2C1.0%2C0.6%5Cn%22%0A%20%20%20%20%20%20%20%20%223%2C5013%2C1%2C1%2C1%2C3%2C24.0%2C3.5%2C2.0%5Cn%22%0A%20%20%20%20)%0A%0A%0A%40app.function%0Adef%20tonnage_view(boats%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Drill%20D1.4%20compares%3A%20each%20boat's%20key%2C%20its%20unit%20and%20its%20tonnage%2C%20to%20three%20places.%22%22%22%0A%20%20%20%20return%20boats%5B%5B%22hh_id%22%2C%20%22boat_operator_line%22%2C%20%22boat_unit_no%22%2C%20%22boat_no%22%2C%20%22boat_measured_in%22%5D%5D.assign(gt%3Dboats%5B%22gt%22%5D.round(3))%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%22Drill%20D1.4%22%2C%20boats_with_tonnage%2C%0A%20%20%20%20%20%20%20%20%5B(%22region%2001's%20boats%22%2C%20(BOAT_CSV%5B%2201%22%5D%2C))%2C%20(%22region%2004's%20boats%22%2C%20(BOAT_CSV%5B%2204%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22three%20boats%3A%20from%20papers%2C%20in%20meters%2C%20in%20feet%22%2C%20(drill_boats_csv()%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22files%22%2C%20summarize%3Dtonnage_view%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Read%20the%20error's%20last%20line%3A%20what%20kind%20of%20value%20is%20being%20multiplied%3F%20Then%20look%20at%20the%20type%20of%20a%20dimension%20column.%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%20compute%20region%2001's%20tonnages%2C%20and%20read%20the%20error's%20last%20line.%0A%20%20%20%20try_it(boats_with_tonnage%2C%20BOAT_CSV%5B%2201%22%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**Trace.**%20The%20error's%20last%20line%20says%20*Can%20only%20string%20multiply%20by%20an%20integer*%3A%20Python%20was%20asked%20to%20multiply%20**text**.%20Look%20at%20a%20dimension%20column%20as%20%60read_csv%60%20reads%20it%2C%20and%20at%20its%20type%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%20Trace%3A%20one%20dimension%20column%2C%20its%20type%2C%20and%20its%20first%20values%0A%20%20%20%20_length%20%3D%20pd.read_csv(BOAT_CSV%5B%2201%22%5D%2C%20encoding%3D%22cp1252%22)%5B%22boat_length%22%5D%0A%20%20%20%20_length.dtype%2C%20_length.head(4).tolist()%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%20type%20is%20%60str%60%2C%20and%20among%20the%20lengths%20is%20%60'%5C%5CN'%60%3A%20the%20export's%20missing%20value%2C%20which%20pandas%20took%20for%20a%20word.%20One%20word%20in%20a%20column%20makes%20the%20whole%20column%20text.%20Expect%20a%20second%20error%20if%20you%20try%20%60.astype(float)%60%20on%20the%20column%3A%20*could%20not%20convert%20string%20to%20float%3A%20'%5C%5CN'*%2C%20the%20same%20value%2C%20named%20this%20time.%20Module%202%2C%20section%203%20shows%20how%20to%20tell%20%60read_csv%60%20what%20the%20export%20writes%20for%20a%20missing%20value.%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.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.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*TypeError%3A%20Can%20only%20string%20multiply%20by%20an%20integer*%20%7C%20The%20dimension%20columns%20are%20text%2C%20because%20of%20%60%5CN%60.%20%7C%20%60read_csv%60's%20%60na_values%3D%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20could%20not%20convert%20string%20to%20float%3A%20'%5C%5CN'*%20%7C%20%60.astype(float)%60%20on%20a%20column%20that%20still%20holds%20%60%5CN%60.%20%7C%20read%20%60%5CN%60%20as%20missing%20in%20the%20first%20place%20%7C%0A%20%20%20%20%7C%20*SyntaxError%3A%20(unicode%20error)%20...%20malformed%20%5CN%20character%20escape*%20%7C%20%60%22%5CN%22%60%20without%20the%20%60r%60%3A%20in%20a%20normal%20string%2C%20%60%5CN%60%20starts%20a%20named%20character.%20%7C%20%60r%22%5CN%22%60%2C%20or%20%60%22%5C%5CN%22%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%23%20Drill%20D1.5%20%C2%B7%20A%20key%20part%20compared%20as%20a%20number%20against%20text%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20published%20table%20names%20places%2C%20not%20codes.%20The%20names%20come%20from%20the%20PSGC%20code%20list%2C%20joined%20on%20the%20area%20codes%2C%20and%20a%20region%20whose%20codes%20are%20stored%20another%20way%20gets%20no%20names%2C%20or%20stops%20the%20pipeline.%0A%0A%20%20%20%20The%20regional%20databases%20do%20not%20agree%20on%20how%20they%20store%20area%20codes%3A%20regions%2001%20and%2007%20store%20%60region_code%60%2C%20%60province_code%60%20and%20%60citymun_code%60%20as%20numbers%20(%601%60%2C%20%6033%60%2C%20%6010%60)%2C%20region%2004%20as%20zero-padded%20text%20(%60'04'%60%2C%20%60'010'%60%2C%20%60'28'%60).%20The%20code%20list%20%60geography%60%20has%20them%20as%20zero-padded%20text.%20%60with_citymun(households)%60%20should%20take%20one%20region's%20households%20as%20its%20database%20stores%20them%20and%20give%20each%20household's%20city%20or%20municipality%20name.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20region's%20%60household%60%20table%20as%20stored%20%E2%86%92%20%60hh_id%60%20and%20%60name%60%2C%20one%20row%20per%20household.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60with_citymun(stored%5B%2201%22%5D)%5B%22name%22%5D.head(3).tolist()%60%20%7C%20%60%5B'Luna'%2C%20'Santo%20Tomas'%2C%20'Rosario'%5D%60%20%7C%0A%20%20%20%20%7C%20%60with_citymun(stored%5B%2201%22%5D)%5B%22name%22%5D.isna().sum()%60%20%7C%20%600%60%3A%20every%20household%20named%20%7C%0A%0A%20%20%20%20Tools%3A%20text%20types%20and%20zero-padding%20(%5Bmodule%202%2C%20section%203%5D(%2F%3Ffile%3D10_flat_table.py)).%0A%0A%20%20%20%20**Predict.**%20The%20function%20was%20written%20and%20tried%20on%20region%2004.%20What%20will%20it%20do%20on%20region%2001%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20with_citymun(households%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20region's%20households%2C%20as%20its%20database%20stores%20them%2C%20each%20with%20its%20city%20or%20municipality's%20name.%22%22%22%0A%20%20%20%20keys%20%3D%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22citymun_code%22%5D%0A%20%20%20%20table%20%3D%20households%5B%5B%22hh_id%22%2C%20*keys%5D%5D.copy()%0A%20%20%20%20%23%20Drill%20D1.5%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20named%20%3D%20table.merge(geography%5B%5B*keys%2C%20%22name%22%5D%5D%2C%20on%3Dkeys%2C%20how%3D%22left%22)%0A%20%20%20%20return%20named%5B%5B%22hh_id%22%2C%20%22name%22%5D%5D%0A%0A%0A%40app.function%0Adef%20drill_stored()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Three%20households%20with%20their%20codes%20stored%20as%20numbers%3A%20in%20La%20Union%20(33)%2C%20Quezon%20(56)%20and%20Cebu%20(22).%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B1%2C%202%2C%203%5D%2C%20%22region_code%22%3A%20%5B1%2C%204%2C%207%5D%2C%20%22province_code%22%3A%20%5B33%2C%2056%2C%2022%5D%2C%20%22citymun_code%22%3A%20%5B10%2C%2047%2C%2044%5D%7D).astype(%22int32%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%22Drill%20D1.5%22%2C%20with_citymun%2C%0A%20%20%20%20%20%20%20%20%5B(%22region%2004's%20households%22%2C%20(stored%5B%2204%22%5D%2C))%2C%20(%22region%2001's%20households%22%2C%20(stored%5B%2201%22%5D%2C))%2C%20(%22region%2007's%20households%22%2C%20(stored%5B%2207%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22three%20households%20in%20La%20Union%2C%20Quezon%20and%20Cebu%2C%20codes%20as%20numbers%22%2C%20(drill_stored()%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20hint%3D%22Read%20the%20error's%20last%20line%3A%20which%20two%20types%20are%20being%20compared%3F%20Once%20it%20runs%2C%20count%20the%20missing%20names.%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%20name%20region%2001's%20cities%20and%20municipalities%2C%20and%20read%20the%20error's%20last%20line.%0A%20%20%20%20try_it(with_citymun%2C%20stored%5B%2201%22%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**Trace.**%20The%20error%20names%20two%20types%2C%20%60int32%60%20and%20%60string%60%2C%20and%20a%20key%2C%20%60region_code%60.%20Look%20at%20the%20area%20codes%20of%20region%2001%20and%20region%2004%20as%20their%20databases%20store%20them%2C%20and%20at%20the%20code%20list's%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%20Trace%3A%20the%20codes%20of%20one%20household%20in%20region%2001%2C%20one%20in%20region%2004%20and%20one%20row%20of%20the%20code%20list%2C%20and%20the%20type%20of%20each%0A%20%20%20%20_keys%20%3D%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22citymun_code%22%5D%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region%2001%22%3A%20%5B*stored%5B%2201%22%5D%5B_keys%5D.iloc%5B0%5D.tolist()%2C%20str(stored%5B%2201%22%5D%5B%22province_code%22%5D.dtype)%5D%2C%0A%20%20%20%20%20%20%20%20%22region%2004%22%3A%20%5B*stored%5B%2204%22%5D%5B_keys%5D.iloc%5B0%5D.tolist()%2C%20str(stored%5B%2204%22%5D%5B%22province_code%22%5D.dtype)%5D%2C%0A%20%20%20%20%20%20%20%20%22geography%22%3A%20%5B*geography%5B_keys%5D.iloc%5B0%5D.tolist()%2C%20str(geography%5B%22province_code%22%5D.dtype)%5D%2C%0A%20%20%20%20%7D%2C%20index%3D%5B*_keys%2C%20%22type%22%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%20Region%2001%20stores%20whole%20numbers%3B%20region%2004%20and%20the%20code%20list%20store%20text%20with%20its%20zeros.%20A%20key%20part%20has%20to%20be%20the%20same%20type%20on%20both%20sides%20of%20a%20join%2C%20and%20for%20codes%20the%20same%20text%2C%20zeros%20included.%20Module%202%2C%20section%203%20brings%20every%20region's%20codes%20to%20one%20form%20before%20anything%20joins%20on%20them.%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.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.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%20*ValueError%3A%20You%20are%20trying%20to%20merge%20on%20int32%20and%20string%20columns*%20%7C%20The%20codes%20are%20numbers%20in%20the%20households%20and%20text%20in%20the%20code%20list.%20%7C%20turn%20the%20codes%20into%20text%20before%20the%20join%20%7C%0A%20%20%20%20%7C%20Every%20name%20missing%2C%20no%20error%20%7C%20%60.astype(str)%60%20without%20padding%20(%60'1'%60%20is%20not%20%60'01'%60)%2C%20or%20a%20column%20padded%20to%20another's%20width%3A%20%60province_code%60%20has%203%20digits%2C%20the%20others%202.%20%7C%20%60.str.zfill()%60%20with%20each%20column's%20own%20width%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'name'*%20%7C%20The%20code%20list's%20columns%20were%20not%20all%20taken.%20%7C%20%60geography%5B%5B*keys%2C%20%22name%22%5D%5D%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%23%20Drill%20D1.6%20%C2%B7%20A%20region's%20file%20attached%20twice%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20flat%20table%20is%20built%20by%20stacking%20every%20region's%20database.%20A%20region%20stacked%20twice%20doubles%20its%20share%20of%20every%20count%20and%20every%20total%2C%20and%20each%20of%20its%20rows%20is%20a%20real%20row.%0A%0A%20%20%20%20%60households_of_every_region(folder)%60%20should%20attach%20every%20regional%20database%20in%20a%20folder%2C%20named%20%60sfh2025_r%60%20and%20the%20region's%20two%20digits%20(%60sfh2025_r01.duckdb%60)%2C%20whichever%20regions%20the%20folder%20holds%2C%20and%20give%20every%20household%20once%3A%20%60region_code%60%2C%20%60hh_id%60%20and%20%60hh_code%60%2C%20sorted%20by%20region%20and%20id.%20The%20setup%20made%20two%20folders%20as%20a%20computer's%20Downloads%20folder%20can%20look%3A%20%60FOLDER_07_TWICE%60%20holds%20the%20three%20regions'%20databases%20and%20region%2007's%20downloaded%20a%20second%20time%2C%20which%20the%20browser%20saved%20as%20%60sfh2025_r07%20(1).duckdb%60%3B%20%60FOLDER_04_COPY%60%20holds%20region%2004's%20and%20a%20copy%2C%20%60sfh2025_r04%20-%20Copy.duckdb%60.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20folder%20%E2%86%92%20one%20row%20per%20household%20of%20each%20region%20in%20it.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60households_of_every_region(PATHS.data).groupby(%22region_code%22).size().to_dict()%60%20%7C%20%60%7B'01'%3A%20744%2C%20'04'%3A%20736%2C%20'07'%3A%20715%7D%60%20%7C%0A%20%20%20%20%7C%20%60households_of_every_region(FOLDER_07_TWICE).groupby(%22region_code%22).size().to_dict()%60%20%7C%20%60%7B'01'%3A%20744%2C%20'04'%3A%20736%2C%20'07'%3A%20715%7D%60%3A%20the%20same%20households%20%7C%0A%0A%20%20%20%20Tools%3A%20every%20region%20in%20one%20query%20(%5Bmodule%202%2C%20section%202%5D(%2F%3Ffile%3D10_flat_table.py))%2C%20duplicates%20by%20key%20(%5Bmodule%204%2C%20section%205%5D(%2F%3Ffile%3D30_edit_rules.py)).%0A%0A%20%20%20%20**Predict.**%20Run%20**Try%20it**%20below%2C%20on%20the%20course's%20own%20%60data%2F%60%20folder.%20Is%20anything%20wrong%20with%20what%20it%20gives%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20households_of_every_region(folder)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20household%20of%20every%20regional%20database%20in%20the%20folder%2C%20once%2C%20sorted%20by%20region%20and%20household%20id.%22%22%22%0A%20%20%20%20%23%20Drill%20D1.6%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20files%20%3D%20sorted(folder.glob(%22sfh2025_r*.duckdb%22))%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20for%20k%2C%20path%20in%20enumerate(files)%3A%0A%20%20%20%20%20%20%20%20con.execute(f%22ATTACH%20'%7Bpath.as_posix()%7D'%20AS%20db%7Bk%7D%20(READ_ONLY)%22)%0A%20%20%20%20select%20%3D%20%22SELECT%20lpad(CAST(region_code%20AS%20VARCHAR)%2C%202%2C%20'0')%20AS%20region_code%2C%20hh_id%2C%20hh_code%20FROM%20%7Bdb%7D.household%22%0A%20%20%20%20query%20%3D%20%22%20UNION%20ALL%20BY%20NAME%20%22.join(select.format(db%3Df%22db%7Bk%7D%22)%20for%20k%20in%20range(len(files)))%0A%20%20%20%20table%20%3D%20con.execute(query%20%2B%20%22%20ORDER%20BY%20region_code%2C%20hh_id%22).df()%0A%20%20%20%20con.close()%0A%20%20%20%20return%20table%0A%0A%0A%40app.function%0Adef%20stacked_view(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Drill%20D1.6%20compares%3A%20each%20household's%20key%20and%20code%2C%20numbered%20from%200.%22%22%22%0A%20%20%20%20return%20table%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22hh_code%22%5D%5D.reset_index(drop%3DTrue)%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%22Drill%20D1.6%22%2C%20households_of_every_region%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20course's%20data%20folder%22%2C%20(PATHS.data%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22a%20folder%20with%20sfh2025_r07.duckdb%20and%20sfh2025_r07%20(1).duckdb%22%2C%20(FOLDER_07_TWICE%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22a%20folder%20with%20sfh2025_r04.duckdb%20and%20sfh2025_r04%20-%20Copy.duckdb%22%2C%20(FOLDER_04_COPY%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22folders%22%2C%20summarize%3Dstacked_view%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20names%20the%20first%20folder%20that%20goes%20wrong.%20Count%20that%20folder's%20households%20by%20region%2C%20and%20look%20at%20the%20files%20the%20function%20attached.%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%20stack%20every%20region%20of%20the%20course's%20own%20data%20folder.%0A%20%20%20%20try_it(households_of_every_region%2C%20PATHS.data)%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**It%20looks%20right.**%20On%20the%20course's%20own%20folder%20there%20is%20one%20household%20per%20row%2C%202%2C195%20of%20them%2C%20and%20each%20region's%20count%20is%20its%20database's.%20Its%20check%20is%20red%20all%20the%20same.%20The%20check%20runs%20the%20function%20on%20the%20other%20two%20folders%20too.%20List%20what%20each%20folder%20holds%2C%20with%20each%20file's%20own%20count%20of%20households%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%20Trace%3A%20every%20file%20the%20function's%20pattern%20finds%20in%20each%20folder%2C%20with%20the%20households%20its%20household%20table%20holds%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_label%2C%20_folder%20in%20%5B(%22data%2F%22%2C%20PATHS.data)%2C%20(%22FOLDER_07_TWICE%22%2C%20FOLDER_07_TWICE)%2C%20(%22FOLDER_04_COPY%22%2C%20FOLDER_04_COPY)%5D%3A%0A%20%20%20%20%20%20%20%20for%20_path%20in%20sorted(_folder.glob(%22sfh2025_r*.duckdb%22))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20with%20duckdb.connect(str(_path)%2C%20read_only%3DTrue)%20as%20_con%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_count%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_rows.append(%7B%22folder%22%3A%20_label%2C%20%22file%22%3A%20_path.name%2C%20%22households%22%3A%20_count%7D)%0A%20%20%20%20pd.DataFrame(_rows)%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**Trace.**%20%60*%60%20in%20a%20file%20pattern%20stands%20for%20any%20characters%2C%20any%20number%20of%20them%2C%20so%20%60sfh2025_r*.duckdb%60%20finds%20the%20copies%20as%20well%20as%20the%20originals.%20Each%20copy%20is%20a%20whole%20regional%20database%3A%20attached%2C%20it%20adds%20every%20one%20of%20its%20region's%20households%20again.%20Now%20count%20how%20often%20a%20household's%20key%20repeats%20in%20what%20the%20function%20gives%20for%20%60FOLDER_07_TWICE%60%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%20Trace%3A%20households%20whose%20region%20and%20id%20are%20already%20in%20the%20table%20above%20them%0A%20%20%20%20_stacked%20%3D%20course.attempt(households_of_every_region%2C%20FOLDER_07_TWICE)%0A%20%20%20%20%7B%22rows%22%3A%20len(_stacked)%2C%20%22repeated%20keys%22%3A%20int(_stacked.duplicated(%5B%22region_code%22%2C%20%22hh_id%22%5D).sum())%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%20A%20key%20that%20repeats%20in%20a%20table%20that%20should%20hold%20each%20household%20once%20is%20the%20sign%20of%20a%20source%20read%20twice.%20Module%202%2C%20section%202%20stacks%20the%20regions%20in%20one%20query%3B%20module%204%2C%20section%205%20finds%20duplicates%20by%20key%2C%20the%20check%20that%20belongs%20after%20every%20stacking.%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.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.6%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%20One%20region%20with%20twice%20its%20households%20%7C%20The%20file%20pattern%20also%20matches%20a%20copy%20of%20that%20region's%20database.%20%7C%20the%20pattern%20in%20%60glob%60%20%7C%0A%20%20%20%20%7C%20*IOException*%20about%20a%20file%20that%20does%20not%20exist%20%7C%20The%20files%20are%20named%20from%20%60REGIONS%60%2C%20but%20the%20folder%20holds%20other%20regions.%20%7C%20read%20the%20regions%20the%20folder%20holds%20%7C%0A%20%20%20%20%7C%20Right%20counts%20on%20these%20folders%2C%20but%20the%20cause%20still%20there%20%7C%20Duplicates%20were%20dropped%20after%20stacking.%20That%20works%20only%20while%20the%20copy%20is%20identical%20to%20its%20original.%20%7C%20find%20each%20region's%20file%20once%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**What%20the%20six%20mistakes%20have%20in%20common.**%20Two%20stopped%20with%20an%20error%2C%20and%20each%20error's%20last%20line%20named%20what%20was%20wrong%3A%20text%20multiplied%20as%20numbers%2C%20a%20number%20joined%20to%20text.%20The%20other%20four%20ran%20without%20a%20word%20and%20gave%20back%20a%20flat%20table%20that%20looked%20like%20an%20answer%3A%20codes%20missing%20a%20digit%2C%20households%20counted%20two%20and%20three%20times%2C%20removed%20households%20kept%2C%20a%20region%20stacked%20twice.%20Three%20of%20the%20six%20came%20from%20how%20a%20file%20or%20a%20database%20stores%20its%20values%2C%20before%20any%20analysis%20began%2C%20and%20the%20first%20fix%20that%20comes%20to%20mind%20for%20an%20error%2C%20%60.astype(...)%60%2C%20turned%20two%20of%20them%20into%20silent%20ones.%20The%20last%20was%20right%20on%20the%20course's%20folder%20and%20wrong%20on%20a%20folder%20anyone%20could%20have%2C%20which%20only%20a%20check%20on%20more%20than%20one%20folder%20could%20show.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%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%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20keep%20a%20flat%20table%20honest%20%7C%20do%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20read%20codes%20from%20a%20CSV%20export%20%7C%20name%20their%20type%3A%20%60dtype%3D%7B%22hh_code%22%3A%20str%7D%60%20in%20pandas%2C%20%60types%3D%60%20in%20DuckDB%20%7C%20the%20codes'%20width%20after%20reading%20%7C%0A%20%20%20%20%7C%20read%20the%20export's%20missing%20values%20%7C%20%60na_values%3D%5Br%22%5CN%22%5D%60%20%7C%20%60dtypes%60%3A%20a%20number%20column%20read%20as%20%60str%60%20still%20holds%20a%20token%20%7C%0A%20%20%20%20%7C%20join%20on%20the%20survey's%20keys%20%7C%20%60on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%60%2C%20and%20%60validate%3D%60%20%7C%20rows%20out%20against%20rows%20in%20%7C%0A%20%20%20%20%7C%20leave%20out%20removed%20households%20%7C%20negate%20the%20whole%20rule%3A%20%60~(deleted%20%5C%7C%20voided)%60%20%7C%20how%20many%20rows%20were%20left%20out%2C%20beside%20how%20many%20were%20kept%20%7C%0A%20%20%20%20%7C%20join%20codes%20stored%20as%20numbers%20and%20as%20text%20%7C%20pad%20to%20text%20first%3A%20%60.astype(str).str.zfill(width)%60%20%7C%20missing%20names%20after%20the%20join%20%7C%0A%20%20%20%20%7C%20stack%20regional%20databases%20%7C%20find%20exactly%20one%20file%20per%20region%20%7C%20%60duplicated(%5B%22region_code%22%2C%20%22hh_id%22%5D).sum()%60%20is%200%20%7C%0A%0A%20%20%20%20Next%2C%20module%205%20(%6040_recode_and_tabulate.py%60)%20turns%20the%20flat%20table%20into%20categories%20from%20codes%2C%20and%20into%20distinct%20counts%20at%20national%2C%20regional%20and%20provincial%20levels.%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
80347909de92775e9d22e886dad89a07