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%22Recode%20and%20tabulate%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%20matplotlib.pyplot%20as%20plt%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%20as%20module%202%20reads%20it%3A%20every%20region's%20rows%20in%20one%20table%2C%20removed%20households%20and%20voided%20rows%20left%20out%2C%20area%20codes%20as%20text%0A%20%20%20%20households%20%3D%20course.households()%20%20%23%20one%20row%20per%20household%3A%20its%20area%2C%20interview%2C%20stratum%2C%20enumeration%20area%20and%20weights%0A%20%20%20%20members%20%3D%20course.members()%20%20%23%20one%20row%20per%20household%20member%0A%20%20%20%20units%20%3D%20course.units()%20%20%23%20one%20row%20per%20fishing%20unit%20(a%20member%20can%20run%20more%20than%20one)%0A%20%20%20%20catch%20%3D%20course.child(%22catch%22)%20%20%23%20one%20row%20per%20species%20a%20unit%20caught%0A%20%20%20%20boats%20%3D%20course.child(%22boat%22)%20%20%23%20one%20row%20per%20boat%20a%20unit%20used%0A%20%20%20%20species%20%3D%20course.codes(%22species%22)%20%20%23%20the%20species%20code%20list%3A%20code%2C%20name%2C%20group%0A%20%20%20%20design%20%3D%20course.design_frame()%20%20%23%20the%20households%20with%20the%20design%20columns%20module%206%20estimates%20with%0A%0A%20%20%20%20%23%20An%20operator%20is%20a%20member%20who%20runs%20a%20fishing%20unit%20of%20the%20household%20(mem_role_operator%20%3D%201)%3A%20the%20unit%20of%20the%20operators'%20tables%0A%20%20%20%20operators%20%3D%20members%5Bmembers%5B%22mem_role_operator%22%5D%20%3D%3D%201%5D%0A%20%20%20%20%23%20The%20members%20who%20fished%20in%202025%20(mem_fished%20%3D%201)%3A%20the%20ones%20asked%20the%20four%20questions%20about%20their%20role%0A%20%20%20%20fishers%20%3D%20members%5Bmembers%5B%22mem_fished%22%5D%20%3D%3D%201%5D%0A%0A%20%20%20%20%23%20The%20two%20categories%20every%20recode%20here%20ends%20with%3A%20a%20missing%20answer%2C%20and%20an%20answer%20that%20is%20not%20a%20valid%20code%20or%20value%0A%20%20%20%20NO_ANSWER%2C%20INVALID%20%3D%20%22No%20answer%22%2C%20%22Invalid%20value%22%0A%20%20%20%20%23%20A%20member's%20role%20in%20fishing%2C%20in%20the%20order%20the%20four%20flags%20are%20read%0A%20%20%20%20ROLES%20%3D%20%5B%22Runs%20a%20unit%22%2C%20%22Crew%20in%20the%20household's%20unit%22%2C%20%22Crew%20for%20others%22%2C%20%22On%20shore%20only%22%5D%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%20Recode%20and%20tabulate%0A%0A%20%20%20%20**Where%20this%20is%20in%20processing%20a%20survey.**%20Once%20the%20data%20is%20in%20one%20place%20(module%202)%2C%20its%20keys%20match%20(module%203)%20and%20its%20values%20are%20checked%20and%20edited%20(module%204)%2C%20new%20variables%20are%20derived%20from%20the%20answers%3A%20categories%20from%20codes%2C%20and%20counts%20of%20the%20units%20a%20table%20is%20about.%20This%20module%20is%20that%20step.%20If%20you%20have%20written%20it%20in%20R%2C%20it%20is%20the%20middle%20of%20a%20tabulation%20script%3A%20the%20%60mutate(case_when(...))%60%20lines%20with%20their%20catch-all%20categories%2C%20%60cut()%60%20for%20age%20groups%2C%20the%20top%20species%20and%20%22Other%22%2C%20and%20%60n_distinct()%60%20by%20group.%0A%0A%20%20%20%20A%20statistical%20table%20asks%20two%20things%20of%20the%20survey%20that%20its%20tables%20cannot%20answer%20as%20stored.%20First%2C%20**words%20instead%20of%20codes**%3A%20a%20reader%20wants%20%22Secondary%22%2C%20not%20%6021%60%2C%20and%20%22Runs%20a%20unit%22%2C%20not%20%60mem_role_operator%20%3D%201%60.%20Second%2C%20**units%20instead%20of%20rows**%3A%20%22how%20many%20operators%20in%20Pangasinan%22%20is%20a%20count%20of%20people%2C%20and%20the%20survey%20stores%20them%20in%20tables%20where%20one%20person%20can%20sit%20on%20several%20rows.%20This%20module%20derives%20the%20categories%2C%20counts%20the%20units%2C%20and%20ends%20with%20the%20one%20table%20module%206%20estimates%20from.%0A%0A%20%20%20%20%7C%20section%20%7C%20what%20you%20do%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20Categories%20from%20codes%20%7C%20education%20groups%20from%20a%20code%20list%2C%20a%20member's%20role%20from%20four%20flags%2C%20age%20groups%20from%20ages%2C%20the%20top%20eight%20species%20and%20%22Other%22%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Distinct%20counts%20by%20group%20%7C%20households%2C%20fishing%20units%20and%20operators%20counted%20once%20each%2C%20at%20national%2C%20regional%20and%20provincial%20levels%2C%20in%20pandas%20and%20polars%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20The%20tabulation%20frame%20%7C%20one%20row%20per%20operator%2C%20with%20its%20categories%20and%20the%20design%20columns%2C%20and%20the%20unweighted%20table%20it%20gives%20%7C%0A%0A%20%20%20%20**The%20tables.**%20The%20setup%20cell%20at%20the%20top%20reads%20the%20survey%20with%20the%20course's%20helpers%2C%20as%20module%202%20built%20them%3A%20%60households%60%2C%20%60members%60%2C%20%60units%60%20(the%20fishing%20units)%2C%20%60catch%60%20(the%20species%20each%20unit%20caught)%20and%20%60boats%60%2C%20every%20region's%20rows%20in%20one%20table%2C%20removed%20households%20and%20voided%20rows%20left%20out%2C%20area%20codes%20as%20zero-padded%20text.%20%60operators%60%20is%20the%20members%20who%20run%20a%20fishing%20unit%20of%20their%20household%20(%60mem_role_operator%60%20is%201)%2C%20and%20%60fishers%60%20the%20members%20who%20fished%20in%202025%20(%60mem_fished%60%20is%201).%20The%20rows%20module%204%20would%20edit%20are%20still%20here%20(an%20education%20code%20not%20in%20the%20list%2C%20an%20age%20over%20110%2C%20a%20catch%20row%20entered%20twice)%2C%20so%20every%20recode%20and%20count%20in%20this%20module%20has%20to%20cope%20with%20them%2C%20as%20a%20table%20run%20before%20the%20edits%20are%20applied%20must.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20modules%20before%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(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%225.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Categories%20from%20codes%3A%20mapping%20tables%2C%20np.select%2C%20pd.cut%2C%20top-N%20with%20other%0A%0A%20%20%20%20**The%20problem.**%20The%20survey%20stores%20answers%20as%20codes.%20Sex%20is%20%601%60%20or%20%602%60%3B%20the%20highest%20level%20of%20education%20completed%20is%20a%20code%20from%20a%20list%20of%20twelve%3B%20a%20member's%20role%20in%20fishing%20is%20four%20yes%2Fno%20flags.%20A%20table%20for%20readers%20needs%20categories%2C%20and%20each%20kind%20of%20code%20needs%20its%20own%20tool%3A%0A%0A%20%20%20%20%7C%20the%20codes%20%7C%20what%20makes%20the%20category%20%7C%20the%20tool%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20a%20code%20list%20with%20a%20column%20for%20the%20category%20(%60education.csv%60's%20%60group%60)%20%7C%20a%20lookup%3A%20code%20%E2%86%92%20category%20%7C%20a%20**mapping%20table**%2C%20with%20%60.map()%60%20%7C%0A%20%20%20%20%7C%20several%20columns%2C%20read%20in%20order%20(four%20flags)%20%7C%20conditions%2C%20the%20first%20true%20one%20wins%20%7C%20**%60np.select%60**%20%7C%0A%20%20%20%20%7C%20a%20number%20(age%2C%20gross%20tonnage)%20%7C%20which%20range%20it%20falls%20in%20%7C%20**%60pd.cut%60**%20%7C%0A%20%20%20%20%7C%20many%20categories%2C%20a%20few%20with%20most%20of%20the%20rows%20(the%20species)%20%7C%20the%20largest%20few%2C%20the%20rest%20pooled%20%7C%20**top-N%20with%20%22Other%22**%20%7C%0A%0A%20%20%20%20**Two%20categories%20every%20recode%20needs.**%20Every%20recode%20in%20this%20course%20ends%20with%20two%20catch-alls.%20**No%20answer**%20is%20a%20missing%20answer%3A%20a%20child%20under%206%20is%20not%20asked%20about%20education%2C%20so%20their%20%60mem_education%60%20is%20empty.%20**Invalid%20value**%20is%20an%20answer%20that%20is%20there%20but%20is%20not%20a%20valid%20code%20or%20value%3A%20an%20education%20code%20of%20%6099%60%2C%20an%20age%20of%20130.%20They%20are%20different%20facts.%20A%20missing%20answer%20is%20often%20expected%3B%20an%20invalid%20value%20is%20an%20error%20module%204%20should%20have%20caught%2C%20and%20a%20table%20that%20pools%20the%20two%20hides%20it.%0A%0A%20%20%20%20%23%23%23%20The%20simplest%20mapping%3A%20a%20dictionary%0A%0A%20%20%20%20Sex%20has%20two%20valid%20codes%2C%20so%20a%20dictionary%20is%20the%20whole%20code%20list.%20%60.map(dictionary)%60%20replaces%20each%20code%20with%20its%20value%20and%20gives%20a%20missing%20value%20for%20a%20code%20the%20dictionary%20lacks%2C%20and%20for%20a%20missing%20code.%20Those%20two%20missing%20values%20are%20then%20told%20apart%20by%20looking%20back%20at%20the%20code%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20sex_category(codes%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Sex%20as%20an%20ordered%20category%3A%20Male%2C%20Female%2C%20then%20Invalid%20value%20(a%20code%20that%20is%20not%201%20or%202)%20and%20No%20answer.%22%22%22%0A%20%20%20%20labels%20%3D%20codes.map(%7B1%3A%20%22Male%22%2C%202%3A%20%22Female%22%7D)%20%20%23%20a%20code%20the%20dictionary%20lacks%2C%20or%20a%20missing%20code%2C%20gives%20a%20missing%20value%0A%20%20%20%20labels%20%3D%20labels.mask(codes.notna()%20%26%20labels.isna()%2C%20INVALID)%20%20%23%20a%20code%20was%20there%2C%20but%20not%20a%20valid%20one%0A%20%20%20%20labels%20%3D%20labels.mask(codes.isna()%2C%20NO_ANSWER)%20%20%23%20no%20code%20at%20all%0A%20%20%20%20return%20pd.Series(pd.Categorical(labels%2C%20categories%3D%5B%22Male%22%2C%20%22Female%22%2C%20INVALID%2C%20NO_ANSWER%5D%2C%20ordered%3DTrue)%2C%20index%3Dcodes.index)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Every%20member's%20sex%3A%20a%20few%20members%20have%20a%20sex%20code%20that%20is%20not%201%20or%202%2C%20which%20module%204's%20rule%20M1%20finds%0A%20%20%20%20sex_category(members%5B%22mem_sex%22%5D).value_counts(sort%3DFalse).rename(%22members%22).to_frame()%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%20result%20is%20a%20**categorical**%20column%20(pandas'%20%60category%60%20type)%3A%20text%20values%20from%20a%20fixed%20list%20of%20categories%2C%20in%20a%20fixed%20order.%20%60value_counts(sort%3DFalse)%60%20lists%20them%20in%20that%20order%2C%20the%20empty%20%60No%20answer%60%20included%2C%20which%20is%20how%20a%20published%20table%20lists%20them.%20Section%203%20comes%20back%20to%20why%20the%20order%20and%20the%20empty%20categories%20matter.%0A%0A%20%20%20%20%23%23%23%20A%20mapping%20table%3A%20the%20code%20list%20is%20the%20recode%0A%0A%20%20%20%20**The%20problem.**%20The%20highest%20level%20of%20education%20completed%20(%60mem_education%60)%20is%20coded%20from%20a%20ladder%20of%20twelve%20levels%2C%20from%20no%20schooling%20to%20a%20postgraduate%20degree.%20A%20table%20reports%20far%20fewer%20groups%20than%20there%20are%20codes.%20The%20R%20way%20writes%20every%20code%20into%20a%20%60case_when%60%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20mutate(education%20%3D%20case_when(%0A%20%20%20%20%20%20mem_education%20%25in%25%20c(1%2C%202)%20~%20%22None%22%2C%0A%20%20%20%20%20%20mem_education%20%25in%25%20c(10%2C%2011)%20~%20%22Primary%22%2C%0A%20%20%20%20%20%20mem_education%20%25in%25%20c(20%2C%2021%2C%2030%2C%2031)%20~%20%22Secondary%22%2C%0A%20%20%20%20%20%20%23%20...%20a%20line%20for%20every%20group%20...%0A%20%20%20%20%20%20is.na(mem_education)%20~%20%22No%20answer%22%2C%0A%20%20%20%20%20%20TRUE%20~%20%22Invalid%20value%22))%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Every%20code%20is%20typed%20twice%3A%20once%20in%20the%20code%20list%2C%20once%20in%20the%20script.%20When%20the%20code%20list%20gains%20a%20code%2C%20the%20script%20does%20not%20know%2C%20and%20the%20new%20code%20lands%20in%20%22Invalid%20value%22%20without%20an%20error.%0A%0A%20%20%20%20**The%20tool.**%20The%20code%20list%20%60codes%2Feducation.csv%60%20already%20has%20a%20%60group%60%20column%3A%20the%20recode%20is%20a%20column%20of%20the%20code%20list.%20Read%20the%20list%2C%20turn%20it%20into%20a%20lookup%20from%20code%20to%20group%2C%20and%20%60.map()%60%20the%20codes%20through%20it.%20A%20new%20code%20then%20needs%20one%20new%20row%20in%20the%20code%20list%20and%20no%20change%20to%20the%20code.%0A%0A%20%20%20%20**Read%20the%20code%20list%20with%20care.**%20The%20group%20of%20the%20lowest%20levels%20is%20named%20%60None%60.%20%60pd.read_csv%60%20reads%20the%20text%20%60None%60%2C%20%60NA%60%20and%20%60null%60%20as%20**missing**%20by%20default%2C%20so%20those%20codes%20would%20lose%20their%20group%2C%20and%20every%20member%20with%20no%20schooling%20would%20land%20in%20%22Invalid%20value%22.%20%60keep_default_na%3DFalse%60%20reads%20every%20cell%20as%20the%20text%20it%20is.%20The%20cell%20below%20reads%20the%20list%20both%20ways.%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%20code%20list%20read%20twice%3A%20with%20pandas'%20default%20missing-value%20words%2C%20and%20with%20every%20cell%20read%20as%20its%20text%0A%20%20%20%20_default%20%3D%20pd.read_csv(PATHS.codes%20%2F%20%22education.csv%22)%0A%20%20%20%20education_codes%20%3D%20pd.read_csv(PATHS.codes%20%2F%20%22education.csv%22%2C%20keep_default_na%3DFalse)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22Read%20with%20the%20defaults%2C%20**%7B_default%5B'group'%5D.isna().sum()%7D%20codes%20have%20no%20group**%3A%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B'%2C%20'.join(_default.loc%5B_default%5B'group'%5D.isna()%2C%20'label'%5D)%7D.%20With%20%60keep_default_na%3DFalse%60%2C%20%7Beducation_codes%5B'group'%5D.isna().sum()%7D%20do.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20list%20has%20**%7Blen(education_codes)%7D%20codes%20in%20%7Beducation_codes%5B'group'%5D.nunique()%7D%20groups**%3A%22)%2C%0A%20%20%20%20%20%20%20%20education_codes.groupby(%22group%22%2C%20sort%3DFalse)%5B%22code%22%5D.agg(codes%3Dlambda%20c%3A%20%22%2C%20%22.join(map(str%2C%20c))).reset_index()%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(education_codes%2C)%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%60education_codes.set_index(%22code%22)%5B%22group%22%5D%60%20is%20the%20lookup%3A%20a%20Series%20whose%20index%20is%20the%20code%20and%20whose%20values%20are%20the%20groups.%20Give%20it%20to%20%60.map()%60%20and%20every%20code%20is%20looked%20up%20in%20it%2C%20in%20the%20members'%20own%20row%20order%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20lookup%20%3D%20education_codes.set_index(%22code%22)%5B%22group%22%5D%0A%20%20%20%20members%5B%22mem_education%22%5D.map(lookup)%20%20%20%20%23%20one%20group%20per%20member%3B%20missing%20for%20a%20blank%20code%20and%20for%20a%20code%20not%20in%20the%20list%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20A%20%60merge%60%20would%20do%20the%20same%20lookup%20as%20a%20join%20(%60members.merge(education_codes%2C%20left_on%3D%22mem_education%22%2C%20right_on%3D%22code%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%60).%20It%20brings%20every%20column%20of%20the%20code%20list%20along%20and%20returns%20a%20new%20table%3B%20%60.map()%60%20returns%20one%20column%20that%20lines%20up%20with%20the%20rows%20it%20came%20from.%20Use%20%60merge%60%20when%20you%20need%20several%20columns%20of%20the%20code%20list%20at%20once%2C%20%60.map()%60%20for%20one.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.1.%20Education%20groups%20from%20the%20code%20list%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20table%20of%20members%20or%20operators%20by%20education%20is%20one%20of%20the%20survey's%20standard%20tables.%20Driven%20by%20the%20code%20list%2C%20the%20recode%20stays%20right%20when%20the%20list%20changes%3B%20split%20into%20No%20answer%20and%20Invalid%20value%2C%20it%20shows%20the%20invalid%20codes%20module%204%20still%20has%20to%20fix.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20members%20table%20(its%20%60mem_education%60%20column)%20and%20the%20code%20list%20%E2%86%92%20a%20Series%20with%20one%20group%20per%20member%2C%20in%20the%20members'%20order%3A%0A%0A%20%20%20%20%7C%20mem_education%20%7C%20group%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2021%20%7C%20Secondary%20%7C%0A%20%20%20%20%7C%202%20%7C%20None%20%7C%0A%20%20%20%20%7C%20*(blank)*%20%7C%20No%20answer%20%7C%0A%20%20%20%20%7C%2099%20%7C%20Invalid%20value%20%7C%0A%0A%20%20%20%20A%20code%20in%20the%20list%20gets%20its%20%60group%60%3B%20a%20blank%20code%20gets%20%60%22No%20answer%22%60%3B%20a%20code%20not%20in%20the%20list%20gets%20%60%22Invalid%20value%22%60.%0A%0A%20%20%20%20**Tools%3A**%20%60set_index%60%2C%20%60.map()%60%2C%20%60.isna()%60%2C%20%60.mask()%60%20and%20%60.fillna()%60%2C%20as%20%60sex_category%60%20above%20uses%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20education_group(members%3A%20pd.DataFrame%2C%20education_codes%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20member's%20education%20group%20from%20the%20code%20list%3A%20its%20group%2C%20No%20answer%20for%20no%20code%2C%20Invalid%20value%20for%20a%20code%20not%20in%20the%20list.%22%22%22%0A%20%20%20%20%23%20Exercise%205.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20lookup%20from%20code%20to%20group%3A%20the%20code%20list%20with%20code%20as%20its%20index%2C%20its%20group%20column%0A%20%20%20%20%23%20Step%202%3A%20map%20each%20member's%20mem_education%20through%20it%0A%20%20%20%20%23%20Step%203%3A%20%22No%20answer%22%20where%20mem_education%20is%20missing%3B%20%22Invalid%20value%22%20where%20a%20code%20was%20there%20but%20found%20no%20group%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.1%22)%0A%0A%0A%40app.function%0Adef%20as_labels(result)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22A%20column%20of%20categories%20as%20a%20list%20of%20texts%2C%20for%20the%20checks%3A%20its%20name%2C%20index%20and%20type%20don't%20count.%22%22%22%0A%20%20%20%20return%20%5BNone%20if%20pd.isna(value)%20else%20str(value)%20for%20value%20in%20list(result)%5D%0A%0A%0A%40app.function%0Adef%20try_counted(function%2C%20*args%2C%20unit%3A%20str)%3A%0A%20%20%20%20%22%22%22A%20Try%20it%20for%20an%20exercise%20that%20gives%20one%20category%20per%20row%3A%20the%20categories%20counted%20(in%20their%20own%20order%20when%20the%0A%20%20%20%20result%20is%20a%20category)%2C%20or%2C%20while%20the%20function%20is%20not%20written%20or%20raises%20an%20error%2C%20what%20try_it%20says%20about%20it.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20result%20%3D%20pd.Series(function(*args))%0A%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20return%20try_it(function%2C%20*args)%0A%20%20%20%20return%20result.value_counts(sort%3Dnot%20isinstance(result.dtype%2C%20pd.CategoricalDtype)).rename(unit).to_frame()%0A%0A%0A%40app.function%0Adef%20members_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Nine%20members%2C%20typed%20as%20the%20survey%20types%20them%2C%20used%20by%20the%20checks%20of%20Exercises%205.1%2C%205.2%2C%205.3%20and%205.6%3A%20a%20blank%0A%20%20%20%20education%20code%2C%20codes%20in%20the%20group%20named%20None%2C%20codes%20not%20in%20the%20list%2C%20ages%20at%20the%20groups'%20edges%2C%20a%20blank%20age%20and%20an%0A%20%20%20%20age%20of%20130%2C%20members%20with%20two%20roles%2C%20with%20no%20role%20answered%2C%20with%20every%20role%200%2C%20and%20with%20one%20role%20answered.%22%22%22%0A%20%20%20%20table%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2204%22%2C%20%2204%22%2C%20%2207%22%2C%20%2207%22%2C%20%2207%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B5101%2C%205101%2C%205101%2C%205101%2C%205102%2C%205102%2C%205103%2C%205103%2C%205103%5D%2C%0A%20%20%20%20%20%20%20%20%22mem_line%22%3A%20%5B1%2C%202%2C%203%2C%201%2C%201%2C%202%2C%201%2C%202%2C%203%5D%2C%0A%20%20%20%20%20%20%20%20%22mem_sex%22%3A%20%5B1%2C%202%2C%208%2C%201%2C%202%2C%201%2C%201%2C%202%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22mem_age%22%3A%20%5B44%2C%204%2C%20130%2C%2010%2C%2018%2C%2060%2C%20None%2C%209%2C%20110%5D%2C%0A%20%20%20%20%20%20%20%20%22mem_education%22%3A%20%5B21%2C%20None%2C%202%2C%201%2C%2099%2C%2060%2C%2051%2C%2010%2C%2045%5D%2C%0A%20%20%20%20%20%20%20%20%22mem_role_operator%22%3A%20%5B1%2C%20None%2C%200%2C%200%2C%201%2C%200%2C%201%2C%200%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22mem_role_crew%22%3A%20%5B1%2C%20None%2C%200%2C%201%2C%200%2C%200%2C%200%2C%200%2C%20None%5D%2C%0A%20%20%20%20%20%20%20%20%22mem_role_hired%22%3A%20%5B0%2C%20None%2C%201%2C%201%2C%200%2C%200%2C%200%2C%200%2C%20None%5D%2C%0A%20%20%20%20%20%20%20%20%22mem_role_shore%22%3A%20%5B0%2C%20None%2C%201%2C%200%2C%200%2C%200%2C%201%2C%201%2C%20None%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20flags%20%3D%20%5B%22mem_sex%22%2C%20%22mem_age%22%2C%20%22mem_education%22%2C%20%22mem_role_operator%22%2C%20%22mem_role_crew%22%2C%20%22mem_role_hired%22%2C%20%22mem_role_shore%22%5D%0A%20%20%20%20return%20table.astype(%7Bcolumn%3A%20%22Int32%22%20for%20column%20in%20flags%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(education_codes)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.1%22%2C%20education_group%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22nine%20members%3A%20a%20blank%20code%2C%20codes%20in%20the%20group%20named%20None%2C%20codes%20not%20in%20the%20list%22%2C%20(members_fixture()%2C%20education_codes))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20members%22%2C%20(members%2C%20education_codes))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20operators%22%2C%20(operators%2C%20education_codes))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20members%20of%20region%2007%22%2C%20(members%5Bmembers%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C%20education_codes))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20members%22%2C%20summarize%3Das_labels%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Map%20mem_education%20through%20education_codes.set_index('code')%5B'group'%5D%3B%20No%20answer%20where%20the%20code%20is%20missing%3B%20Invalid%20value%20where%20a%20code%20found%20no%20group.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(education_codes)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20groups%20for%20the%20operators%2C%20counted%0A%20%20%20%20try_counted(education_group%2C%20operators%2C%20education_codes%2C%20unit%3D%22operators%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%205.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%60Primary%20completed%60%2C%20%60Bachelor's%20degree%60%3A%20twelve%20different%20values%20%7C%20The%20%60label%60%20column%20was%20used%2C%20not%20%60group%60.%20%7C%20%60education_codes.set_index(%22code%22)%5B%22group%22%5D%60%20%7C%0A%20%20%20%20%7C%20No%20%60No%20answer%60%2C%20many%20more%20%60Invalid%20value%60%20%7C%20Every%20missing%20group%20filled%20with%20one%20category%3A%20blank%20codes%20and%20unknown%20codes%20pooled.%20%7C%20%60.mask(codes.isna()%2C%20%22No%20answer%22)%60%20before%20%60.fillna(...)%60%20%7C%0A%20%20%20%20%7C%20No%20%60Invalid%20value%60%2C%20more%20%60No%20answer%60%20%7C%20The%20same%2C%20the%20other%20way%20round.%20%7C%20fill%20the%20blanks%20from%20the%20code%2C%20the%20rest%20after%20%7C%0A%20%20%20%20%7C%20Missing%20values%20in%20the%20result%20where%20%60No%20answer%60%20and%20%60Invalid%20value%60%20should%20be%20%7C%20Nothing%20filled%3A%20%60.map()%60%20leaves%20both%20kinds%20missing.%20%7C%20the%20two%20fills%20%7C%0A%20%20%20%20%7C%20Members%20with%20no%20schooling%20are%20%60Invalid%20value%60%20%7C%20The%20code%20list%20was%20read%20again%20inside%20the%20function%20with%20the%20defaults%2C%20so%20the%20group%20%60None%60%20became%20missing.%20%7C%20use%20the%20%60education_codes%60%20you%20are%20given%20%7C%0A%20%20%20%20%7C%20More%20values%20than%20members%20%7C%20A%20merge%20with%20a%20code%20list%20whose%20codes%20repeat%2C%20or%20a%20merge%20that%20changed%20the%20row%20order.%20%7C%20%60.map()%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%20Rules%20with%20conditions%3A%20%60np.select%60%0A%0A%20%20%20%20**The%20problem.**%20A%20member's%20role%20in%20fishing%20is%20not%20one%20code.%20The%20questionnaire%20asks%20every%20member%2010%20or%20over%20who%20fished%20four%20yes%2Fno%20questions%20(%601%60%20yes%2C%20%600%60%20no)%3A%20did%20they%20run%20a%20fishing%20unit%20of%20the%20household%20(%60mem_role_operator%60)%2C%20work%20in%20one%20(%60mem_role_crew%60)%2C%20work%20for%20pay%20or%20a%20share%20in%20someone%20else's%20unit%20(%60mem_role_hired%60)%2C%20or%20glean%2C%20process%20or%20sell%20the%20catch%20without%20going%20out%20(%60mem_role_shore%60)%3F%20A%20member%20can%20say%20yes%20to%20several%2C%20and%20a%20table%20by%20role%20puts%20each%20member%20in%20one%20row.%20So%20the%20role%20is%20read%20from%20the%20four%20flags%20in%20an%20order%3A%20someone%20who%20runs%20a%20unit%20is%20counted%20as%20running%20it%2C%20whatever%20else%20they%20did.%20That%20is%20a%20rule%20with%20conditions%2C%20not%20a%20lookup.%0A%0A%20%20%20%20**The%20tool.**%20%60np.select(conditions%2C%20choices%2C%20default%3D...)%60%20is%20NumPy's%20%60case_when%60.%20It%20takes%20a%20list%20of%20conditions%20(True%2FFalse%20columns)%20and%20a%20list%20of%20the%20values%20to%20give%2C%20one%20for%20each%3B%20each%20row%20gets%20the%20choice%20of%20the%20**first**%20condition%20that%20is%20true%20for%20it%2C%20and%20%60default%60%20where%20none%20is%3A%0A%0A%20%20%20%20%7C%20R%2C%20%60dplyr%3A%3Acase_when%60%20%7C%20Python%2C%20%60np.select%60%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60mem_role_operator%20%3D%3D%201%20~%20%22Runs%20a%20unit%22%2C%60%20%7C%20conditions%20%60%5Boperator%20%3D%3D%201%2C%20crew%20%3D%3D%201%2C%20hired%20%3D%3D%201%2C%20shore%20%3D%3D%201%5D%60%20%7C%0A%20%20%20%20%7C%20%60mem_role_crew%20%3D%3D%201%20~%20%22Crew%20in%20the%20household's%20unit%22%2C%60%20%7C%20choices%20%60%5B%22Runs%20a%20unit%22%2C%20%22Crew%20in%20the%20household's%20unit%22%2C%20%22Crew%20for%20others%22%2C%20%22On%20shore%20only%22%5D%60%20%7C%0A%20%20%20%20%7C%20%60mem_role_hired%20%3D%3D%201%20~%20%22Crew%20for%20others%22%2C%60%20%7C%20(the%20same%20order%3A%20the%20first%20true%20condition%20wins)%20%7C%0A%20%20%20%20%7C%20%60mem_role_shore%20%3D%3D%201%20~%20%22On%20shore%20only%22%2C%60%20%7C%20%7C%0A%20%20%20%20%7C%20%60TRUE%20~%20%22Invalid%20value%22%60%20%7C%20%60default%3D%22Invalid%20value%22%60%20%7C%0A%0A%20%20%20%20The%20setup%20cell%20keeps%20the%20four%20roles%2C%20in%20this%20order%2C%20in%20%60ROLES%60.%20%60np.select%60%20returns%20a%20NumPy%20array%2C%20not%20a%20pandas%20column%3A%20wrap%20it%20in%20%60pd.Series(...%2C%20index%3Dtable.index)%60%20to%20put%20it%20back%20beside%20the%20rows%20it%20came%20from.%0A%0A%20%20%20%20**A%20missing%20flag%20breaks%20the%20condition.**%20The%20flags%20are%20nullable%20integers%20(%60Int32%60)%2C%20so%20%60mem_role_operator%20%3D%3D%201%60%20is%20missing%2C%20not%20False%2C%20where%20the%20flag%20is%20blank.%20%60np.select%60%20needs%20plain%20True%20and%20False%2C%20and%20stops%20on%20a%20missing%20one.%20Read%20the%20error's%20last%20line%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%20np.select%20on%20conditions%20that%20still%20hold%20missing%20values%3A%20the%20members%20who%20were%20not%20asked%20about%20their%20role%20make%20them%20so%0A%20%20%20%20_condition%20%3D%20members%5B%22mem_role_operator%22%5D%20%3D%3D%201%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20np.select(%5B_condition%5D%2C%20%5BROLES%5B0%5D%5D%2C%20default%3DINVALID)%0A%20%20%20%20%20%20%20%20_message%20%3D%20%22no%20error%22%0A%20%20%20%20except%20TypeError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_message%20%3D%20f%22TypeError%3A%20%7B_error%7D%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60members%5B'mem_role_operator'%5D%20%3D%3D%201%60%20is%20of%20type%20%60%7B_condition.dtype%7D%60%2C%20with%20**%7B_condition.isna().sum()%3A%2C%7D%20missing%20values**%3A%20the%20members%20under%2010%20and%20those%20who%20did%20not%20fish%2C%20who%20were%20not%20asked.%20Given%20to%20%60np.select%60%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%60%60%5Cn%7B_message%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22*Should%20be%20boolean%20ndarray*%3A%20NumPy%20wants%20True%20or%20False%20in%20every%20row.%20%60.eq(1).fillna(False)%60%20gives%20it%20that%3A%20a%20blank%20flag%20is%20not%20a%20yes.%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%60.eq(1)%60%20is%20%60%3D%3D%201%60%20written%20as%20a%20method%2C%20so%20the%20%60.fillna(False)%60%20can%20follow%20on%20the%20same%20line.%20With%20the%20conditions%20made%20True%20or%20False%2C%20a%20blank%20flag%20counts%20as%20%22not%20yes%22%2C%20which%20is%20right%20for%20every%20condition%20but%20one%3A%20a%20member%20whose%20**four%20flags%20are%20all%20blank**%20was%20not%20asked%20or%20did%20not%20answer%2C%20and%20that%20is%20%60No%20answer%60%2C%20not%20%60Invalid%20value%60.%20That%20is%20one%20more%20condition%2C%20%60table%5Bflags%5D.isna().all(axis%3D1)%60%2C%20true%20where%20all%20four%20are%20missing.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.2.%20A%20member's%20role%20from%20four%20flags%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20role%20is%20a%20column%20of%20the%20survey's%20tables%20of%20members%20who%20fish.%20A%20misread%20order%20moves%20a%20member%20between%20two%20roles%2C%20and%20both%20rows%20of%20the%20table%20change%3A%20a%20member%20who%20runs%20a%20unit%20and%20also%20crews%20in%20it%20is%20one%20operator%2C%20not%20one%20crew%20member.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20members%20table%20(its%20four%20%60mem_role_%60%20flags)%20%E2%86%92%20a%20Series%20with%20one%20role%20per%20row%2C%20in%20the%20table's%20order%20and%20with%20the%20table's%20index%2C%20by%20the%20**first**%20rule%20that%20holds%3A%0A%0A%20%20%20%20%7C%20rule%2C%20in%20this%20order%20%7C%20role%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60mem_role_operator%60%20is%201%20%7C%20Runs%20a%20unit%20%7C%0A%20%20%20%20%7C%20%60mem_role_crew%60%20is%201%20%7C%20Crew%20in%20the%20household's%20unit%20%7C%0A%20%20%20%20%7C%20%60mem_role_hired%60%20is%201%20%7C%20Crew%20for%20others%20%7C%0A%20%20%20%20%7C%20%60mem_role_shore%60%20is%201%20%7C%20On%20shore%20only%20%7C%0A%20%20%20%20%7C%20all%20four%20flags%20blank%20%7C%20No%20answer%20%7C%0A%20%20%20%20%7C%20anything%20else%20(no%20flag%20is%201)%20%7C%20Invalid%20value%20%7C%0A%0A%20%20%20%20%7C%20operator%20%7C%20crew%20%7C%20hired%20%7C%20shore%20%7C%20role%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%7C%201%20%7C%200%20%7C%200%20%7C%20Runs%20a%20unit%20%7C%0A%20%20%20%20%7C%200%20%7C%201%20%7C%201%20%7C%200%20%7C%20Crew%20in%20the%20household's%20unit%20%7C%0A%20%20%20%20%7C%20*(blank)*%20%7C%20*(blank)*%20%7C%20*(blank)*%20%7C%20*(blank)*%20%7C%20No%20answer%20%7C%0A%20%20%20%20%7C%200%20%7C%200%20%7C%200%20%7C%200%20%7C%20Invalid%20value%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60.eq(1).fillna(False)%60%2C%20%60.isna().all(axis%3D1)%60%2C%20%60np.select%60%2C%20%60ROLES%60%20and%20%60pd.Series(...%2C%20index%3D...)%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20fishing_role(members%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20member's%20role%20from%20the%20flags%20mem_role_operator%2C%20mem_role_crew%2C%20mem_role_hired%20and%20mem_role_shore%2C%20in%20that%0A%20%20%20%20order%3B%20No%20answer%20if%20all%20four%20are%20blank%2C%20Invalid%20value%20if%20none%20is%201.%22%22%22%0A%20%20%20%20%23%20Exercise%205.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20five%20conditions%2C%20in%20the%20table's%20order%3A%20mem_role_operator%2C%20mem_role_crew%2C%20mem_role_hired%20and%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20mem_role_shore%20each%20equal%20to%201%20(.eq(1).fillna(False))%2C%20and%20all%20four%20flags%20missing%20(.isna().all(axis%3D1))%0A%20%20%20%20%23%20Step%202%3A%20the%20five%20roles%20in%20the%20same%20order%3A%20the%20four%20of%20ROLES%2C%20then%20%22No%20answer%22%0A%20%20%20%20%23%20Step%203%3A%20np.select%20with%20default%20%22Invalid%20value%22%2C%20wrapped%20in%20pd.Series%20with%20the%20members'%20index%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.2%22)%0A%0A%0A%40app.function%0Adef%20labels_by_row(result)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22A%20column%20of%20categories%20for%20the%20checks%2C%20with%20the%20row%20labels%20it%20carries%3A%20a%20role%20must%20sit%20beside%20its%20own%20member.%22%22%22%0A%20%20%20%20column%20%3D%20result%20if%20isinstance(result%2C%20pd.Series)%20else%20pd.Series(result)%0A%20%20%20%20return%20%7B%22rows%22%3A%20%5Bint(label)%20for%20label%20in%20column.index%5D%2C%20%22labels%22%3A%20as_labels(column)%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%22Exercise%205.2%22%2C%20fishing_role%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22nine%20members%3A%20two%20roles%2C%20every%20flag%20blank%2C%20every%20flag%200%2C%20one%20flag%20answered%22%2C%20(members_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20members%20who%20fished%22%2C%20(fishers%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20member%2C%20those%20not%20asked%20included%22%2C%20(members%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20members%20of%20region%2004%22%2C%20(members%5Bmembers%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20members%22%2C%20summarize%3Dlabels_by_row%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Conditions%20in%20order%3A%20the%20operator%2C%20crew%2C%20hired%20and%20shore%20flags%20equal%20to%201%20(each%20.eq(1).fillna(False))%2C%20then%20all%20four%20blank%3B%20default%20Invalid%20value.%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%20your%20roles%20for%20the%20members%20who%20fished%2C%20counted%0A%20%20%20%20try_counted(fishing_role%2C%20fishers%2C%20unit%3D%22members%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%205.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*TypeError%3A%20invalid%20entry%200%20in%20condlist%3A%20should%20be%20boolean%20ndarray*%20%7C%20A%20condition%20still%20has%20missing%20values.%20%7C%20%60.eq(1).fillna(False)%60%20on%20each%20flag%20%7C%0A%20%20%20%20%7C%20The%20first%20case's%20member%201%20is%20%60Crew%20in%20the%20household's%20unit%60%20%7C%20Crew%20came%20before%20running%20a%20unit%20in%20the%20lists.%20%7C%20the%20operator's%20flag%20first%20%7C%0A%20%20%20%20%7C%20The%20first%20case's%20member%204%20is%20%60Crew%20for%20others%60%20%7C%20Hired%20came%20before%20crew.%20%7C%20the%20order%20of%20%60ROLES%60%20%7C%0A%20%20%20%20%7C%20No%20%60No%20answer%60%3A%20the%20members%20with%20every%20flag%20blank%20are%20%60Invalid%20value%60%20%7C%20The%20%22all%20blank%22%20condition%20is%20missing.%20%7C%20%60members%5Bflags%5D.isna().all(axis%3D1)%60%20%7C%0A%20%20%20%20%7C%20Right%20values%2C%20but%20the%20check%20fails%20on%20a%20filtered%20table%20%7C%20The%20result%20has%20a%20fresh%20index%20(0%2C%201%2C%202...)%20instead%20of%20the%20members'.%20%7C%20%60pd.Series(...%2C%20index%3Dmembers.index)%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%20Ranges%3A%20%60pd.cut%60%0A%0A%20%20%20%20**The%20problem.**%20A%20table%20of%20members%20by%20age%20sorts%20ages%20into%20groups.%20A%20long%20%60case_when%60%20(%60mem_age%20%3E%3D%2010%20%26%20mem_age%20%3C%2018%20~%20%2210-17%22%60%2C%20and%20so%20on)%20can%20do%20it%2C%20and%20each%20line%20is%20a%20chance%20to%20write%20%60%3C%3D%60%20where%20%60%3C%60%20was%20meant%2C%20so%20a%20member%20aged%2018%20lands%20in%20two%20groups%20or%20in%20none.%0A%0A%20%20%20%20**The%20tool.**%20%60pd.cut(values%2C%20bins%3D...%2C%20labels%3D...)%60%20sorts%20numbers%20into%20the%20ranges%20between%20consecutive%20**bin%20edges**%2C%20R's%20%60cut()%60.%20Three%20arguments%20decide%20everything%3A%0A%0A%20%20%20%20%7C%20argument%20%7C%20what%20it%20says%20%7C%20here%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60bins%3D%5B0%2C%2010%2C%2018%2C%2030%2C%2045%2C%2060%2C%20111%5D%60%20%7C%20the%20edges%3B%20n%20%2B%201%20edges%20make%20n%20groups%20%7C%200%20to%2010%2C%2010%20to%2018%2C%20...%2C%2060%20to%20111%20%7C%0A%20%20%20%20%7C%20%60right%3DFalse%60%20%7C%20each%20range%20includes%20its%20**left**%20edge%20and%20excludes%20its%20right%3A%20%60%5B10%2C%2018)%60%20%7C%2010%20is%20in%20%2210-17%22%2C%2018%20is%20not.%20The%20default%2C%20%60right%3DTrue%60%2C%20puts%2018%20in%20%2210-17%22%20%7C%0A%20%20%20%20%7C%20%60labels%3D%5B...%5D%60%20%7C%20a%20name%20per%20range%20%7C%20%60%22Under%2010%22%60%2C%20%60%2210-17%22%60%2C%20...%2C%20%60%2260%20and%20over%22%60%20%7C%0A%0A%20%20%20%20A%20value%20outside%20every%20range%20(an%20age%20of%20130%2C%20or%20a%20negative%20one)%20gets%20a%20**missing**%20group%2C%20and%20so%20does%20a%20missing%20age%3A%20%60pd.cut%60%20cannot%20tell%20them%20apart.%20Look%20back%20at%20the%20age%2C%20as%20for%20the%20codes%20above.%0A%0A%20%20%20%20**The%20result%20is%20ordered.**%20%60pd.cut%60%20returns%20a%20categorical%20whose%20categories%20are%20the%20labels%2C%20**in%20the%20order%20of%20the%20bins**%2C%20and%20the%20order%20means%20something%3A%20%22Under%2010%22%20%3C%20%2210-17%22.%20Sorted%20as%20text%2C%20%60%2210-17%22%60%20would%20come%20before%20%60%22Under%2010%22%60%2C%20and%20so%20would%20%60%2260%20and%20over%22%60%2C%20and%20the%20table%20would%20read%20out%20of%20order.%20%60.cat.add_categories(%5B...%5D)%60%20adds%20the%20two%20catch-all%20categories%20at%20the%20end%2C%20so%20they%20can%20be%20assigned.%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%20operators'%20ages%20in%20the%20survey's%20age%20groups%3A%20the%20categories'%20order%2C%20against%20the%20same%20labels%20sorted%20as%20text%0A%20%20%20%20_groups%20%3D%20pd.cut(operators%5B%22mem_age%22%5D%2C%20bins%3D%5B0%2C%2010%2C%2018%2C%2030%2C%2045%2C%2060%2C%20111%5D%2C%20right%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20labels%3D%5B%22Under%2010%22%2C%20%2210-17%22%2C%20%2218-29%22%2C%20%2230-44%22%2C%20%2245-59%22%2C%20%2260%20and%20over%22%5D)%0A%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22**As%20categories**%20(the%20bins'%20order)%22)%2C%20_groups.value_counts(sort%3DFalse%2C%20dropna%3DFalse).rename(%22operators%22).to_frame()%5D)%2C%0A%20%20%20%20%20%20%20%20mo.vstack(%5Bmo.md(%22**As%20text**%20(sorted%20by%20letter)%22)%2C%20_groups.astype(%22string%22).value_counts(dropna%3DFalse).sort_index().rename(%22operators%22).to_frame()%5D)%2C%0A%20%20%20%20%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20What%20the%20two%20ends%20of%20the%20operators'%20ages%20hold%3A%20the%20missing%20group%20and%20the%20operators%20under%2010%0A%20%20%20%20_ages%20%3D%20operators%5B%22mem_age%22%5D%0A%20%20%20%20_over%20%3D%20sorted(_ages%5B_ages%20%3E%20110%5D.tolist())%0A%20%20%20%20_under%20%3D%20sorted(_ages%5B_ages%20%3C%2010%5D.tolist())%0A%20%20%20%20mo.md(f%22The%20missing%20group%20(%60NaN%60)%20is%20the%20**%7Blen(_over)%7D%20operators%20aged%20over%20110**%20(%7B'%2C%20'.join(map(str%2C%20_over))%7D)%2C%20which%20module%204's%20rule%20M2%20finds.%20%22%0A%20%20%20%20%20%20%20%20%20%20f%22The%20**%7Blen(_under)%7D%20operators%20under%2010**%20(%7B'%2C%20'.join(map(str%2C%20_under))%7D)%20are%20there%20too%3A%20the%20questionnaire%20asks%20the%20fishing%20questions%20only%20from%2010%2C%20and%20a%20member%20who%20runs%20a%20unit%20is%2016%20or%20over%2C%20so%20they%20break%20rules%20M3%20and%20M4.%20The%20table%20shows%20them%20in%20a%20group%20of%20their%20own%20rather%20than%20hiding%20them.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%205.3.%20Age%20groups%20as%20an%20ordered%20category%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20age%20group%20is%20a%20column%20of%20most%20of%20the%20survey's%20tables%20of%20people%2C%20and%20it%20is%20the%20column%20that%20shows%20most%20often%20when%20a%20table's%20rows%20are%20sorted%20wrong.%20Built%20once%20as%20an%20ordered%20category%2C%20it%20sorts%20right%20in%20every%20table%2C%20chart%20and%20export%20after%20it.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20Series%20of%20ages%20(such%20as%20%60mem_age%60)%20%E2%86%92%20a%20Series%20of%20type%20%60category%60%2C%20**ordered**%2C%20with%20these%20eight%20categories%20in%20this%20order%2C%20one%20value%20per%20age%2C%20in%20the%20ages'%20order%3A%0A%0A%20%20%20%20%7C%20category%20%7C%20ages%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Under%2010%20%7C%200%20to%209%20%7C%0A%20%20%20%20%7C%2010-17%2C%2018-29%2C%2030-44%2C%2045-59%20%7C%20groups%20that%20each%20include%20their%20lower%20age%20%7C%0A%20%20%20%20%7C%2060%20and%20over%20%7C%2060%20to%20110%20%7C%0A%20%20%20%20%7C%20Invalid%20value%20%7C%20an%20age%20below%200%20or%20over%20110%20%7C%0A%20%20%20%20%7C%20No%20answer%20%7C%20a%20missing%20age%20%7C%0A%0A%20%20%20%20Every%20category%20is%20there%20even%20when%20no%20age%20falls%20in%20it.%0A%0A%20%20%20%20**Tools%3A**%20%60pd.cut%60%20with%20%60bins%3D%60%2C%20%60right%3DFalse%60%20and%20%60labels%3D%60%2C%20%60.cat.add_categories%60%2C%20%60.mask%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20age_group(ages%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Ages%20in%20the%20survey's%20age%20groups%2C%20as%20an%20ordered%20category%3A%20Under%2010%20to%2060%20and%20over%2C%20then%20Invalid%20value%20and%20No%20answer.%22%22%22%0A%20%20%20%20%23%20Exercise%205.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20pd.cut%20the%20ages%20with%20bins%20%5B0%2C%2010%2C%2018%2C%2030%2C%2045%2C%2060%2C%20111%5D%2C%20right%3DFalse%2C%20and%20the%20six%20labels%0A%20%20%20%20%23%20Step%202%3A%20add%20the%20two%20categories%20%22Invalid%20value%22%20and%20%22No%20answer%22%20(.cat.add_categories)%0A%20%20%20%20%23%20Step%203%3A%20%22Invalid%20value%22%20where%20an%20age%20is%20there%20but%20got%20no%20group%3B%20%22No%20answer%22%20where%20the%20age%20is%20missing%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.3%22)%0A%0A%0A%40app.function%0Adef%20as_category(result)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22A%20categorical%20column%20for%20the%20checks%3A%20its%20values%20as%20texts%2C%20its%20categories%20in%20order%2C%20and%20whether%20it%20is%20ordered.%22%22%22%0A%20%20%20%20return%20%7B%22values%22%3A%20as_labels(result)%2C%20%22categories%22%3A%20%5Bstr(c)%20for%20c%20in%20result.cat.categories%5D%2C%20%22ordered%22%3A%20bool(result.cat.ordered)%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%22Exercise%205.3%22%2C%20age_group%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22nine%20ages%20at%20the%20groups'%20edges%3A%204%2C%209%2C%2010%2C%2018%2C%2044%2C%2060%2C%20110%2C%20130%20and%20a%20blank%22%2C%20(members_fixture()%5B%22mem_age%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20operators'%20ages%22%2C%20(operators%5B%22mem_age%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20member's%20age%22%2C%20(members%5B%22mem_age%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22ages%20from%20-1%20to%20120%22%2C%20(pd.Series(range(-1%2C%20121)%2C%20dtype%3D%22Int32%22)%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22series%20of%20ages%22%2C%20summarize%3Das_category%2C%0A%20%20%20%20%20%20%20%20hint%3D%22pd.cut%20with%20bins%20%5B0%2C%2010%2C%2018%2C%2030%2C%2045%2C%2060%2C%20111%5D%20and%20right%3DFalse%3B%20add%20the%20two%20categories%3B%20mask%20by%20whether%20the%20age%20is%20there.%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%20your%20age%20groups%20for%20the%20operators%2C%20counted%20in%20the%20categories'%20order%0A%20%20%20%20try_counted(age_group%2C%20operators%5B%22mem_age%22%5D%2C%20unit%3D%22operators%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%205.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%2010%20is%20%60Under%2010%60%20and%2018%20is%20%6010-17%60%20%7C%20%60right%3DTrue%60%2C%20the%20default.%20%7C%20%60right%3DFalse%60%20%7C%0A%20%20%20%20%7C%20110%20is%20%60Invalid%20value%60%20%7C%20The%20last%20edge%20is%20110%3A%20with%20%60right%3DFalse%60%2C%20110%20is%20outside%20%60%5B60%2C%20110)%60.%20%7C%20the%20last%20edge%2C%20111%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20Cannot%20setitem%20on%20a%20Categorical%20with%20a%20new%20category*%20%7C%20A%20catch-all%20assigned%20before%20it%20was%20added%20as%20a%20category.%20%7C%20%60.cat.add_categories%60%20first%20%7C%0A%20%20%20%20%7C%20The%20check%20shows%20an%20*AttributeError*%20about%20%60.cat%60%20%7C%20The%20result%20is%20text%2C%20not%20a%20category.%20%7C%20return%20what%20%60pd.cut%60%20gave%2C%20with%20its%20categories%20%7C%0A%20%20%20%20%7C%20Categories%20in%20another%20order%2C%20or%20not%20ordered%20%7C%20The%20categories%20were%20rebuilt%20from%20the%20values%2C%20which%20sorts%20them%20as%20text.%20%7C%20keep%20%60pd.cut%60's%20categories%20and%20add%20to%20them%20%7C%0A%20%20%20%20%7C%20130%20is%20%60No%20answer%60%2C%20or%20the%20blank%20age%20is%20%60Invalid%20value%60%20%7C%20Both%20catch-alls%20taken%20from%20the%20group's%20missing%20value.%20%7C%20look%20at%20%60ages.isna()%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%20A%20unit's%20scale%20from%20its%20boats%0A%0A%20%20%20%20The%20fishing%20unit%20table%20holds%20its%20own%20scale%3A%20%60unit_scale%60%2C%20%601%60%20for%20small-scale%20(no%20boat%2C%20or%20every%20boat%20under%205.0%20gross%20tons)%20and%20%602%60%20for%20large-scale%20(a%20boat%20of%205.0%20gross%20tons%20or%20more).%20Two%20codes%20are%20a%20dictionary%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20units%5B%22unit_scale%22%5D.map(%7B1%3A%20%22Small-scale%22%2C%202%3A%20%22Large-scale%22%7D)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20boat%20table%20holds%20what%20the%20scale%20is%20worked%20out%20from.%20A%20boat's%20gross%20tonnage%20is%20length%20%C3%97%20beam%20%C3%97%20depth%20%C3%97%200.212%20when%20it%20was%20measured%20in%20meters%2C%20%C3%97%200.006%20when%20it%20was%20measured%20in%20feet%2C%20or%20the%20figure%20on%20its%20registration%20papers%20(the%20survey's%20rule%20B3).%20Which%20of%20the%20three%20applies%20depends%20on%20%60boat_measured_in%60%2C%20a%20rule%20with%20conditions%2C%20so%20%60np.select%60%20picks%20it.%20The%20unit's%20largest%20boat%20is%20a%20%60groupby%60%20maximum%2C%20and%20%60pd.cut%60%20with%20one%20edge%20at%205%20and%20%60right%3DFalse%60%20makes%20the%20scale%2C%20so%20a%20boat%20of%20exactly%205.0%20gross%20tons%20is%20large-scale.%20Rule%20U3%20in%20module%204%20is%20this%20comparison%3B%20the%20table%20below%20is%20its%20count.%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%20Each%20boat's%20gross%20tonnage%20by%20how%20it%20was%20measured%20(np.select)%2C%20then%20each%20unit's%20largest%20boat%2C%20set%20beside%20the%20unit's%20own%20scale%0A%20%20%20%20_measured%20%3D%20boats%5B%22boat_measured_in%22%5D%0A%20%20%20%20_volume%20%3D%20boats%5B%22boat_length%22%5D%20*%20boats%5B%22boat_beam%22%5D%20*%20boats%5B%22boat_depth%22%5D%0A%20%20%20%20_tons%20%3D%20np.select(%0A%20%20%20%20%20%20%20%20%5B_measured.eq(1)%2C%20_measured.eq(2)%2C%20_measured.eq(3)%5D%2C%0A%20%20%20%20%20%20%20%20%5Bboats%5B%22boat_tonnage_reported%22%5D%2C%20_volume%20*%200.212%2C%20_volume%20*%200.006%5D%2C%0A%20%20%20%20%20%20%20%20default%3Dnp.nan%2C%0A%20%20%20%20)%0A%20%20%20%20_largest%20%3D%20boats.assign(gross_tons%3D_tons).groupby(UNIT_KEY%2C%20as_index%3DFalse)%5B%22gross_tons%22%5D.max()%0A%20%20%20%20_largest%5B%22from%20the%20boats%22%5D%20%3D%20pd.cut(_largest%5B%22gross_tons%22%5D%2C%20bins%3D%5B0%2C%205%2C%20np.inf%5D%2C%20right%3DFalse%2C%20labels%3D%5B%22Small-scale%22%2C%20%22Large-scale%22%5D)%0A%20%20%20%20_both%20%3D%20units.merge(_largest%2C%20on%3DUNIT_KEY%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20_both%5B%22as%20stored%20(unit_scale)%22%5D%20%3D%20_both%5B%22unit_scale%22%5D.map(%7B1%3A%20%22Small-scale%22%2C%202%3A%20%22Large-scale%22%7D)%0A%20%20%20%20_both%5B%22from%20the%20boats%22%5D%20%3D%20_both%5B%22from%20the%20boats%22%5D.astype(%22string%22).fillna(%22No%20boat%22)%0A%20%20%20%20pd.crosstab(_both%5B%22as%20stored%20(unit_scale)%22%5D%2C%20_both%5B%22from%20the%20boats%22%5D%2C%20margins%3DTrue%2C%20margins_name%3D%22Total%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%20Most%20units%20agree%2C%20and%20a%20unit%20with%20no%20boat%20is%20small-scale%20by%20the%20survey's%20rule.%20A%20unit%20in%20a%20cell%20where%20the%20two%20scales%20differ%20has%20boats%20that%20contradict%20its%20stored%20scale%3A%20that%20is%20what%20rule%20U3%20in%20module%204%20looks%20for.%20A%20table%20counts%20units%20by%20%60unit_scale%60%20once%20those%20are%20edited%2C%20which%20is%20why%20the%20edit%20comes%20before%20the%20recode.%0A%0A%20%20%20%20%23%23%23%20The%20top%20eight%20species%2C%20and%20%22Other%22%0A%0A%20%20%20%20**The%20problem.**%20The%20species%20list%20has%20dozens%20of%20codes.%20A%20table%20of%20fishing%20units%20by%20species%20with%20a%20row%20for%20each%20is%20long%2C%20and%20most%20of%20its%20rows%20are%20small%20numbers%2C%20so%20a%20table%20shows%20the%20eight%20species%20caught%20most%20and%20pools%20the%20rest%20as%20%22Other%22.%20Three%20decisions%20hide%20in%20%22the%20eight%20caught%20most%22%2C%20and%20each%20changes%20the%20table%3A%0A%0A%20%20%20%20-%20**Ranked%20by%20what%3F**%20By%20catch%20rows%20here%3A%20each%20row%20is%20one%20species%20reported%20by%20one%20unit.%0A%20%20%20%20-%20**Which%20codes%20may%20rank%3F**%20%22Other%20species%2C%20to%20be%20named%22%20(997)%20and%20%22Not%20identified%22%20(998)%20are%20codes%20in%20the%20list%2C%20in%20its%20group%20%60Other%60%2C%20but%20not%20species%20a%20reader%20can%20name%3A%20they%20rank%20with%20nothing%20and%20go%20to%20%22Other%22.%20A%20code%20not%20in%20the%20list%20is%20an%20error%2C%20kept%20as%20%22Invalid%20value%22%20so%20it%20stays%20visible.%0A%20%20%20%20-%20**Ties.**%20Two%20species%20with%20the%20same%20count%20at%20eighth%20place%3A%20a%20rule%20must%20pick%20one.%20Here%20the%20smaller%20code%20wins.%0A%0A%20%20%20%20In%20R%2C%20%60forcats%3A%3Afct_lump_n(name%2C%20n%20%3D%208%2C%20other_level%20%3D%20%22Other%22)%60%20does%20the%20pooling%2C%20and%20its%20default%20for%20ties%20(%60ties.method%20%3D%20%22min%22%60)%20**keeps%20every%20species%20tied%20at%20the%20boundary**%2C%20so%20%22the%20top%20eight%22%20can%20come%20back%20as%20nine%3A%20forcats'%20reference%20for%20%60fct_lump%60%20says%20that%20with%20ties%20%22you%20will%20get%20at%20least%20%60abs(n)%60%20values%22%20(forcats%201.0.1%2C%20checked%202026-11-20).%20The%20cell%20below%20shows%20the%20survey's%20ranks%20around%20eighth%20place%2C%20and%20a%20place%20in%20one%20region%20where%20two%20species%20tie%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%20Catch%20rows%20per%20species%2C%20ranking%20only%20the%20list's%20named%20species%3A%20the%20ranks%20around%20eighth%20place%2C%20and%20region%2004's%20first%20tie%0A%20%20%20%20def%20_ranks(rows)%3A%0A%20%20%20%20%20%20%20%20named%20%3D%20species.loc%5Bspecies%5B%22group%22%5D%20!%3D%20%22Other%22%2C%20%5B%22code%22%2C%20%22name%22%5D%5D%0A%20%20%20%20%20%20%20%20counts%20%3D%20rows.loc%5Brows%5B%22catch_species%22%5D.isin(named%5B%22code%22%5D)%2C%20%22catch_species%22%5D.value_counts().rename_axis(%22code%22).reset_index()%0A%20%20%20%20%20%20%20%20counts%20%3D%20counts.sort_values(%5B%22count%22%2C%20%22code%22%5D%2C%20ascending%3D%5BFalse%2C%20True%5D%2C%20ignore_index%3DTrue).merge(named%2C%20on%3D%22code%22%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20%20%20%20%20counts.index%20%3D%20counts.index%20%2B%201%20%20%23%20rank%201%20for%20the%20species%20caught%20most%0A%20%20%20%20%20%20%20%20return%20counts%0A%0A%20%20%20%20def%20_place(counts%2C%20place%2C%20where)%3A%0A%20%20%20%20%20%20%20%20here%2C%20after%20%3D%20counts.loc%5Bplace%5D%2C%20counts.loc%5Bplace%20%2B%201%5D%0A%20%20%20%20%20%20%20%20if%20here%5B%22count%22%5D%20%3D%3D%20after%5B%22count%22%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20(f%22%7Bwhere%7D%2C%20**%7Bhere%5B'name'%5D%7D%20(%7Bhere%5B'code'%5D%7D)%20and%20%7Bafter%5B'name'%5D%7D%20(%7Bafter%5B'code'%5D%7D)%20tie%20at%20place%20%7Bplace%7D**%2C%20with%20%7Bhere%5B'count'%5D%7D%20catch%20rows%20each%3A%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22the%20smaller%20code%2C%20%7Bhere%5B'name'%5D%7D%2C%20takes%20the%20place.%22)%0A%20%20%20%20%20%20%20%20return%20f%22%7Bwhere%7D%2C%20place%20%7Bplace%7D%20is%20not%20tied%3A%20%7Bhere%5B'name'%5D%7D%20has%20%7Bhere%5B'count'%5D%7D%20catch%20rows%2C%20%7Bafter%5B'name'%5D%7D%20%7Bafter%5B'count'%5D%7D.%22%0A%0A%20%20%20%20_all%20%3D%20_ranks(catch)%0A%20%20%20%20_region%20%3D%20_ranks(catch%5Bcatch%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D)%0A%20%20%20%20_tied%20%3D%20%5Bplace%20for%20place%20in%20range(1%2C%2010)%20if%20_region.loc%5Bplace%2C%20%22count%22%5D%20%3D%3D%20_region.loc%5Bplace%20%2B%201%2C%20%22count%22%5D%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Bcatch%5B'catch_species'%5D.nunique()%7D%20different%20codes**%20in%20the%20catch%20table%3B%20the%20list%20has%20%7Bspecies%5B'group'%5D.ne('Other').sum()%7D%20named%20species.%20Ranks%206%20to%2011%20by%20catch%20rows%3A%22)%2C%0A%20%20%20%20%20%20%20%20_all.loc%5B6%3A11%2C%20%5B%22code%22%2C%20%22name%22%2C%20%22count%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20mo.md(_place(_all%2C%208%2C%20%22In%20the%20whole%20survey%22))%2C%0A%20%20%20%20%20%20%20%20mo.md(_place(_region%2C%20_tied%5B0%5D%2C%20%22In%20region%2004%22)%20if%20_tied%20else%20%22In%20region%2004%2C%20no%20two%20of%20the%20ten%20species%20caught%20most%20tie.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%205.4.%20The%20top%20species%20and%20%22Other%22%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20table%20of%20fishing%20units%20by%20species%20names%20the%20species%20that%20matter%20to%20the%20fishery%20and%20pools%20the%20rest.%20A%20ranking%20rule%20written%20down%2C%20ties%20included%2C%20gives%20the%20same%20eight%20species%20every%20time%20the%20table%20is%20run%2C%20and%20an%20invalid%20code%20stays%20visible%20instead%20of%20disappearing%20into%20%22Other%22.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20catch%20table%20(its%20%60catch_species%60)%2C%20the%20species%20code%20list%20and%20%60n%60%20%E2%86%92%20a%20Series%20with%20one%20label%20per%20catch%20row%2C%20in%20the%20catch%20table's%20order%3A%0A%0A%20%20%20%201.%20**Names.**%20A%20code%20in%20the%20species%20list%20has%20its%20%60name%60.%0A%20%20%20%202.%20**Rank.**%20Count%20the%20catch%20rows%20of%20each%20code%20that%20is%20in%20the%20list%20**and%20whose%20%60group%60%20is%20not%20%60%22Other%22%60**.%20The%20%60n%60%20codes%20with%20the%20most%20rows%20are%20the%20top%3B%20at%20a%20tie%2C%20the%20smaller%20code%20first.%0A%20%20%20%203.%20**Labels.**%20A%20top%20code%20keeps%20its%20name.%20A%20missing%20code%20is%20%60%22No%20answer%22%60%3B%20a%20code%20not%20in%20the%20list%20is%20%60%22Invalid%20value%22%60.%20Every%20other%20row%20is%20%60%22Other%22%60.%0A%0A%20%20%20%20%7C%20catch_species%20%7C%20label%2C%20n%20%3D%202%20(in%20a%20table%20where%20304%20and%20101%20are%20caught%20most)%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20304%20%7C%20Threadfin%20breams%20%7C%0A%20%20%20%20%7C%20101%20%7C%20Sardines%20%7C%0A%20%20%20%20%7C%20203%20%7C%20Other%20%7C%0A%20%20%20%20%7C%20998%20%7C%20Other%20%7C%0A%20%20%20%20%7C%20150%20%7C%20Invalid%20value%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60.isin()%60%2C%20%60value_counts()%60%2C%20%60sort_values%60%20with%20two%20columns%20and%20%60ascending%3D%5BFalse%2C%20True%5D%60%2C%20%60.head(n)%60%2C%20%60.map()%60%2C%20%60.where()%60%20and%20%60.mask()%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20top_species(catch%3A%20pd.DataFrame%2C%20species%3A%20pd.DataFrame%2C%20n%3A%20int)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20catch%20row's%20species%20name%20if%20it%20is%20one%20of%20the%20n%20named%20species%20caught%20most%20(ties%3A%20smaller%20code%20first)%2C%20else%20Other%3B%0A%20%20%20%20No%20answer%20for%20a%20missing%20code%20and%20Invalid%20value%20for%20a%20code%20not%20in%20the%20list.%22%22%22%0A%20%20%20%20%23%20Exercise%205.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20row's%20name%3A%20map%20catch_species%20through%20the%20species%20list%20(code%20as%20index%2C%20name%20as%20values)%0A%20%20%20%20%23%20Step%202%3A%20the%20codes%20that%20may%20rank%3A%20in%20the%20list%2C%20with%20group%20not%20%22Other%22%3B%20count%20their%20catch%20rows%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20sort%20by%20count%20(largest%20first)%20then%20code%20(smallest%20first)%2C%20and%20keep%20the%20first%20n%20codes%0A%20%20%20%20%23%20Step%203%3A%20the%20name%20where%20the%20code%20is%20a%20top%20code%2C%20else%20%22Other%22%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20then%20%22Invalid%20value%22%20where%20a%20code%20is%20there%20but%20has%20no%20name%2C%20%22No%20answer%22%20where%20the%20code%20is%20missing%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.4%22)%0A%0A%0A%40app.function%0Adef%20catch_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Thirteen%20catch%20rows%20of%20four%20fishing%20units%2C%20used%20by%20the%20check%20of%20Exercise%205.4%3A%20two%20species%20tied%20at%20second%20place%2C%0A%20%20%20%20the%20larger%20code%20met%20first%2C%20Not%20identified%20caught%20most%2C%20a%20code%20not%20in%20the%20list%2C%20a%20missing%20code%2C%20and%20a%20row%20entered%20twice.%22%22%22%0A%20%20%20%20table%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%5D%20*%206%20%2B%20%5B%2204%22%5D%20*%206%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B5101%2C%205101%2C%205101%2C%205101%2C%205102%2C%205102%2C%205101%2C%205101%2C%205101%2C%205101%2C%205101%2C%205101%5D%2C%0A%20%20%20%20%20%20%20%20%22operator_line%22%3A%20%5B1%5D%20*%204%20%2B%20%5B2%5D%20*%202%20%2B%20%5B1%5D%20*%206%2C%0A%20%20%20%20%20%20%20%20%22unit_no%22%3A%20%5B1%5D%20*%2012%2C%0A%20%20%20%20%20%20%20%20%22catch_species%22%3A%20%5B304%2C%20203%2C%20998%2C%20101%2C%20998%2C%20998%2C%20203%2C%20101%2C%20998%2C%20150%2C%20None%2C%20304%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20table.loc%5B12%5D%20%3D%20table.loc%5B0%5D%20%20%23%20the%20first%20row%20entered%20twice%0A%20%20%20%20return%20table.astype(%7B%22catch_species%22%3A%20%22Int32%22%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%22Exercise%205.4%22%2C%20top_species%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22thirteen%20rows%3A%20a%20tie%20at%20second%20place%2C%20Not%20identified%20caught%20most%2C%20an%20invalid%20and%20a%20missing%20code%2C%20n%20%3D%202%22%2C%20(catch_fixture()%2C%20species%2C%202))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20catch%2C%20n%20%3D%208%22%2C%20(catch%2C%20species%2C%208))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20catch%2C%20n%20%3D%205%22%2C%20(catch%2C%20species%2C%205))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20catch%20of%20region%2004%2C%20n%20%3D%206%22%2C%20(catch%5Bcatch%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D%2C%20species%2C%206))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22catch%20tables%22%2C%20summarize%3Das_labels%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Rank%20only%20named%20species%20(group%20not%20Other)%20by%20catch%20rows%2C%20ties%20to%20the%20smaller%20code%3B%20top%20names%20kept%2C%20the%20rest%20Other%3B%20then%20Invalid%20value%20and%20No%20answer.%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%20your%20labels%20for%20the%20survey's%20catch%2C%20counted%0A%20%20%20%20try_counted(top_species%2C%20catch%2C%20species%2C%208%2C%20unit%3D%22catch%20rows%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%205.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%60Not%20identified%60%20is%20among%20the%20top%20%7C%20The%20group%20filter%20is%20missing%3A%20codes%20997%20and%20998%20were%20ranked.%20%7C%20%60species%5B%22group%22%5D%20!%3D%20%22Other%22%60%20%7C%0A%20%20%20%20%7C%20The%20first%20case%20is%20red%20at%20second%20place%2C%20or%20the%20case%20of%20region%2004%20at%20sixth%20%7C%20A%20tie%20broken%20by%20the%20order%20pandas%20met%20the%20codes%20in%2C%20not%20by%20code.%20%7C%20%60sort_values(%5B%22count%22%2C%20%22code%22%5D%2C%20ascending%3D%5BFalse%2C%20True%5D)%60%20%7C%0A%20%20%20%20%7C%20No%20%60Invalid%20value%60%2C%20more%20%60Other%60%20%7C%20The%20invalid%20codes%20pooled%20into%20Other%3A%20the%20catch-alls%20were%20set%20before%20%60.where%60%2C%20or%20not%20at%20all.%20%7C%20the%20two%20%60.mask%60%20calls%20last%20%7C%0A%20%20%20%20%7C%20%60No%20answer%60%20where%20a%20code%20is%20invalid%20%7C%20The%20two%20catch-alls%20swapped%3A%20look%20at%20whether%20the%20code%20itself%20is%20missing.%20%7C%20%60codes.isna()%60%20%7C%0A%20%20%20%20%7C%20Codes%20(%60304%60)%20instead%20of%20names%20%7C%20The%20top%20codes%20kept%20as%20they%20are.%20%7C%20%60.where%60%20on%20the%20names%2C%20not%20the%20codes%20%7C%0A%20%20%20%20%7C%20n%20%2B%201%20or%20more%20names%20%7C%20Every%20species%20tied%20at%20the%20boundary%20kept.%20%7C%20%60.head(n)%60%20after%20sorting%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%20Labels%20for%20areas%3A%20a%20mapping%20table%20with%20a%20key%20of%20two%20parts%0A%0A%20%20%20%20Area%20codes%20need%20names%20too%2C%20and%20the%20code%20list%20%60codes%2Fgeography.csv%60%20has%20them%3A%20one%20row%20per%20city%20or%20municipality%2C%20with%20its%20province's%20and%20region's%20names.%20A%20province%20is%20identified%20by%20its%20region%20**and**%20its%20province%20code%20together%20(%60region_code%60%20and%20%60province_code%60)%2C%20so%20the%20lookup%20is%20a%20join%20on%20both%2C%20after%20the%20list%20is%20cut%20down%20to%20one%20row%20per%20province.%20%60validate%3D%22many_to_one%22%60%20stops%20the%20join%20if%20the%20cut%20left%20a%20province%20twice.%0A%0A%20%20%20%20A%20table%20sometimes%20recodes%20areas%20for%20**one%20region%20only**%2C%20such%20as%20the%20districts%20of%20a%20region%20whose%20tables%20are%20published%20by%20district.%20That%20is%20the%20same%20lookup%20applied%20to%20that%20region's%20rows%3A%20%60np.where(table%5B%22region_code%22%5D%20%3D%3D%20%2204%22%2C%20recoded%2C%20table%5B%22area%22%5D)%60%20keeps%20the%20other%20regions%20as%20they%20are.%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%20row%20per%20province%20with%20its%20name%20and%20its%20region's%20name%2C%20then%20joined%20to%20the%20households%20on%20both%20key%20parts%0A%20%20%20%20_provinces%20%3D%20course.codes(%22geography%22)%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22province%22%2C%20%22region%22%5D%5D.drop_duplicates()%0A%20%20%20%20_named%20%3D%20households.merge(_provinces%2C%20on%3D%5B%22region_code%22%2C%20%22province_code%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20code%20list%20has%20**%7Blen(_provinces)%7D%20provinces**%3B%20the%20survey's%20households%20are%20in%20%7Bhouseholds.groupby(%5B'region_code'%2C%20'province_code'%5D).ngroups%7D%20of%20them.%20Joined%20to%20the%20%7Blen(households)%3A%2C%7D%20households%3A%20%7Blen(_named)%3A%2C%7D%20rows%2C%20**%7B_named%5B'province'%5D.isna().sum()%7D%20without%20a%20name**.%22)%2C%0A%20%20%20%20%20%20%20%20_named.groupby(%5B%22region_code%22%2C%20%22region%22%2C%20%22province_code%22%2C%20%22province%22%5D).size().rename(%22households%22).reset_index()%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%202%20%C2%B7%20Distinct%20counts%20by%20group%20at%20national%2C%20regional%20and%20provincial%20levels%0A%0A%20%20%20%20**The%20problem.**%20%22How%20many%20households%20fish%20in%20Batangas%3F%22%20is%20a%20count%20of%20households.%20The%20survey%20answers%20it%20from%20tables%20whose%20rows%20are%20not%20households%3A%20the%20fishing%20unit%20table%20has%20a%20row%20per%20unit%2C%20the%20catch%20table%20a%20row%20per%20species%20caught.%20Counting%20rows%20gives%20the%20wrong%20number%20for%20three%20reasons%2C%20all%20of%20them%20in%20this%20survey%3A%0A%0A%20%20%20%20%7C%20why%20rows%20are%20not%20units%20%7C%20where%20%7C%20what%20a%20row%20count%20does%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20a%20unit%20has%20several%20rows%20by%20design%20%7C%20a%20household%20with%20two%20fishing%20units%3B%20a%20unit%20with%20five%20species%20%7C%20counts%20the%20units%2C%20or%20the%20species%2C%20not%20the%20households%20%7C%0A%20%20%20%20%7C%20a%20row%20is%20there%20twice%20by%20mistake%20%7C%20the%20fishing%20unit%20entered%20twice%20that%20module%204%20finds%20%7C%20counts%20the%20duplicate%20%7C%0A%20%20%20%20%7C%20the%20unit's%20id%20is%20not%20unique%20alone%20%7C%20%60hh_id%60%20repeats%20across%20regions%3A%20each%20regional%20database%20numbers%20its%20own%20households%20%7C%20a%20national%20count%20of%20ids%20merges%20different%20households%20%7C%0A%0A%20%20%20%20**The%20tool.**%20Count%20**distinct%20units**%3A%20the%20number%20of%20different%20values%20of%20the%20unit's%20key%2C%20where%20the%20key%20is%20every%20column%20that%20identifies%20the%20unit.%20In%20R%20it%20is%20%60n_distinct()%60%2C%20which%20takes%20several%20columns%20and%20counts%20their%20distinct%20combinations.%20The%20cell%20below%20counts%20the%20households%20that%20run%20a%20fishing%20unit%20in%20three%20ways.%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%20Households%20counted%20from%20the%20fishing%20unit%20table%3A%20rows%2C%20ids%2C%20and%20distinct%20households%20by%20their%20whole%20key%0A%20%20%20%20_by_region%20%3D%20units.groupby(%22region_code%22).agg(%0A%20%20%20%20%20%20%20%20rows%3D(%22hh_id%22%2C%20%22size%22)%2C%0A%20%20%20%20%20%20%20%20ids%3D(%22hh_id%22%2C%20%22nunique%22)%2C%0A%20%20%20%20)%0A%20%20%20%20_by_region%5B%22households%22%5D%20%3D%20units%5B%5B%22region_code%22%2C%20%22hh_id%22%5D%5D.drop_duplicates().groupby(%22region_code%22).size()%0A%20%20%20%20_by_region.loc%5B%22national%22%5D%20%3D%20%5B%0A%20%20%20%20%20%20%20%20len(units)%2C%0A%20%20%20%20%20%20%20%20units%5B%22hh_id%22%5D.nunique()%2C%0A%20%20%20%20%20%20%20%20len(units%5B%5B%22region_code%22%2C%20%22hh_id%22%5D%5D.drop_duplicates())%2C%0A%20%20%20%20%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_by_region%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22Within%20a%20region%2C%20%60nunique%60%20of%20the%20id%20is%20right%3A%20an%20id%20is%20unique%20in%20its%20own%20database.%20Nationally%20it%20is%20not%3A%20**%7B_by_region.loc%5B'national'%2C%20'ids'%5D%3A%2C%7D%20ids**%20for%20**%7B_by_region.loc%5B'national'%2C%20'households'%5D%3A%2C%7D%20households**%2C%20because%20the%20same%20id%20numbers%20a%20different%20household%20in%20each%20region.%20The%20key%20of%20a%20household%20is%20%60region_code%60%20**and**%20%60hh_id%60.%20The%20%60households%60%20table%2C%20one%20row%20per%20household%2C%20has%20%7Blen(households)%3A%2C%7D.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Counting%20distinct%20keys%20of%20several%20columns%3A%20pandas%2C%20polars%20and%20R%0A%0A%20%20%20%20%60nunique%60%20counts%20the%20distinct%20values%20of%20**one**%20column.%20Given%20several%20(%60groupby(...)%5B%5B%22region_code%22%2C%20%22hh_id%22%5D%5D.nunique()%60)%2C%20it%20counts%20each%20column%20on%20its%20own%20and%20gives%20a%20column%20per%20key%20part%2C%20not%20the%20number%20of%20combinations.%20For%20a%20key%20of%20several%20columns%2C%20keep%20each%20combination%20once%20with%20%60drop_duplicates%60%2C%20then%20count%20rows%3A%20every%20row%20left%20is%20one%20unit.%0A%0A%20%20%20%20%7C%20to%20count%20%7C%20R%2C%20dplyr%20%7C%20pandas%20%7C%20polars%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20rows%20%7C%20%60count(region_code)%60%20%7C%20%60groupby(%22region_code%22).size()%60%20%7C%20%60group_by(%22region_code%22).len()%60%20%7C%0A%20%20%20%20%7C%20distinct%20values%20of%20one%20column%20%7C%20%60summarise(n%20%3D%20n_distinct(hh_id))%60%20%7C%20%60groupby(%22region_code%22)%5B%22hh_id%22%5D.nunique()%60%20%7C%20%60group_by(%22region_code%22).agg(pl.col(%22hh_id%22).n_unique())%60%20%7C%0A%20%20%20%20%7C%20distinct%20combinations%20of%20several%20%7C%20%60summarise(n%20%3D%20n_distinct(region_code%2C%20hh_id))%60%20%7C%20%60drop_duplicates(%5B*by%2C%20*key%5D).groupby(by).size()%60%20%7C%20%60group_by(by).agg(pl.struct(key).n_unique())%60%20%7C%0A%20%20%20%20%7C%20distinct%20combinations%2C%20overall%20%7C%20%60n_distinct(region_code%2C%20hh_id)%60%20%7C%20%60len(table%5Bkey%5D.drop_duplicates())%60%20%7C%20%60table.select(pl.struct(key).n_unique()).item()%60%20%7C%0A%0A%20%20%20%20polars'%20%60pl.struct(...)%60%20bundles%20several%20columns%20into%20one%20value%20per%20row%2C%20and%20%60n_unique%60%20counts%20the%20distinct%20bundles%3A%20the%20closest%20thing%20to%20R's%20%60n_distinct%60%20with%20several%20columns.%20One%20habit%20from%20R%20needs%20changing%3A%20pasting%20the%20key's%20parts%20into%20one%20text%20(%60paste0(region_code%2C%20hh_id)%60)%20works%20in%20pandas%20too%2C%20but%20two%20parts%20pasted%20without%20a%20separator%20can%20collide%20(%221%22%20%2B%20%2223%22%20and%20%2212%22%20%2B%20%223%22)%2C%20and%20a%20list%20of%20columns%20cannot.%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%20same%20counts%20in%20polars%3A%20rows%2C%20and%20distinct%20fishing%20units%20by%20their%20whole%20key%2C%20per%20region%20and%20nationally%0A%20%20%20%20_units%20%3D%20pl.from_pandas(units)%0A%20%20%20%20_per_region%20%3D%20_units.group_by(%22region_code%22).agg(%0A%20%20%20%20%20%20%20%20pl.len().alias(%22rows%22)%2C%0A%20%20%20%20%20%20%20%20pl.struct(UNIT_KEY).n_unique().alias(%22fishing%20units%22)%2C%0A%20%20%20%20).sort(%22region_code%22)%0A%20%20%20%20_distinct%20%3D%20_units.select(pl.struct(UNIT_KEY).n_unique()).item()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_per_region%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22Nationally%3A%20**%7Blen(_units)%3A%2C%7D%20rows%2C%20%7B_distinct%3A%2C%7D%20distinct%20fishing%20units**.%20The%20%7Blen(_units)%20-%20_distinct%7D%20rows%20between%20them%20are%20units%20entered%20twice%2C%20which%20module%204's%20rule%20U1%20finds.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20trap%20after%20a%20join%3A%20rows%20multiply%0A%0A%20%20%20%20A%20join%20brings%20a%20column%20from%20another%20table%2C%20and%20it%20repeats%20each%20row%20once%20per%20match.%20That%20is%20the%20point%20of%20a%20join%2C%20and%20it%20is%20also%20why%20counting%20rows%20after%20one%20counts%20the%20wrong%20thing.%20A%20table%20of%20operators%20by%20scale%20takes%20the%20scale%20from%20the%20fishing%20unit%20table%3B%20an%20operator%20who%20runs%20two%20units%20matches%20two%20rows%20and%20is%20counted%20twice%2C%20and%20an%20operator%20whose%20two%20units%20are%20of%20different%20scales%20is%20counted%20in%20both.%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%20Operators%20joined%20to%20the%20units%20they%20run%20(a%20member's%20mem_line%20is%20the%20unit's%20operator_line)%0A%20%20%20%20_key%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22mem_line%22%5D%0A%20%20%20%20_joined%20%3D%20operators%5B_key%5D.merge(%0A%20%20%20%20%20%20%20%20units%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22operator_line%22%2C%20%22unit_no%22%2C%20%22unit_scale%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20left_on%3D_key%2C%20right_on%3D%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22operator_line%22%5D%2C%0A%20%20%20%20%20%20%20%20how%3D%22inner%22%2C%20validate%3D%22one_to_many%22%2C%0A%20%20%20%20)%0A%20%20%20%20_joined%5B%22scale%22%5D%20%3D%20_joined%5B%22unit_scale%22%5D.map(%7B1%3A%20%22Small-scale%22%2C%202%3A%20%22Large-scale%22%7D)%0A%20%20%20%20_counts%20%3D%20_joined.groupby(%22scale%22).agg(rows%3D(%22mem_line%22%2C%20%22size%22))%0A%20%20%20%20_counts%5B%22operators%22%5D%20%3D%20_joined%5B%5B*_key%2C%20%22scale%22%5D%5D.drop_duplicates().groupby(%22scale%22).size()%0A%20%20%20%20_counts.loc%5B%22both%20scales%22%5D%20%3D%20%5Blen(_joined)%2C%20len(_joined%5B_key%5D.drop_duplicates())%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(operators)%3A%2C%7D%20operators**%20joined%20to%20the%20units%20they%20run%20give%20**%7Blen(_joined)%3A%2C%7D%20rows**%3A%22)%2C%0A%20%20%20%20%20%20%20%20_counts%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20rows%20count%20fishing%20units%3B%20the%20distinct%20keys%20count%20operators.%20The%20operators%20of%20the%20two%20scales%20add%20up%20to%20more%20than%20all%20operators%20when%20someone%20runs%20units%20of%20both%20scales%3A%20a%20person%20can%20be%20in%20two%20rows%20of%20a%20table%20by%20scale%2C%20and%20a%20table%20that%20says%20so%20in%20a%20note%20is%20right.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Three%20levels%3A%20national%2C%20regional%20and%20provincial%0A%0A%20%20%20%20A%20survey%20table%20is%20published%20at%20three%20levels%2C%20and%20a%20script%20computes%20each%20with%20its%20own%20%60group_by%60%3A%20nothing%2C%20the%20region%2C%20the%20region%20and%20province.%20The%20three%20are%20stacked%20into%20one%20long%20table%20with%20a%20column%20that%20says%20the%20level%2C%20so%20one%20table%20holds%20all%20three%20and%20a%20reader%20filters%20it.%0A%0A%20%20%20%20The%20fishing%20unit%2C%20catch%20and%20member%20tables%20have%20no%20province%20column%3A%20the%20province%20is%20the%20household's%2C%20in%20%60households%60.%20%60with_province%60%20below%20brings%20it%20in%20with%20a%20join%20on%20the%20household's%20whole%20key%2C%20%60validate%3D%22many_to_one%22%60%20making%20sure%20each%20row%20finds%20one%20household%20at%20most.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20with_province(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20table%20with%20its%20household's%20province%20(province_code)%20beside%20each%20row%2C%20from%20the%20households%20table.%22%22%22%0A%20%20%20%20return%20table.merge(households%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22province_code%22%5D%5D%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%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%23%23%20Exercise%205.5.%20Distinct%20units%20at%20three%20levels%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20first%20table%20of%20every%20survey%20report%3A%20how%20many%20households%2C%20fishing%20units%20or%20operators%2C%20nationally%2C%20by%20region%20and%20by%20province.%20Written%20once%20for%20any%20unit%2C%20it%20gives%20all%20three%20counts%20from%20any%20of%20the%20survey's%20tables%2C%20and%20none%20of%20the%20three%20ways%20rows%20mislead%20can%20reach%20it.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20with%20%60region_code%60%20and%20%60province_code%60%2C%20and%20%60key%60%2C%20the%20list%20of%20columns%20that%20identify%20the%20unit%20**within%20its%20region's%20database**%20(%60%5B%22hh_id%22%5D%60%20for%20a%20household)%20%E2%86%92%20one%20row%20per%20area%2C%20with%20three%20columns%3A%0A%0A%20%20%20%20%7C%20level%20%7C%20area%20%7C%20units%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20National%20%7C%20All%20regions%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20Region%20%7C%2001%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20Province%20%7C%2001033%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20-%20%60area%60%3A%20%60%22All%20regions%22%60%20for%20the%20national%20row%3B%20the%20region%20code%20for%20a%20region%3B%20the%20region%20and%20province%20codes%20joined%20(%60%2201%22%60%20%2B%20%60%22033%22%60)%20for%20a%20province.%0A%20%20%20%20-%20%60units%60%3A%20the%20number%20of%20distinct%20units.%20A%20unit%20is%20%60region_code%60%20**plus**%20the%20key%3A%20the%20same%20%60hh_id%60%20in%20two%20regions%20is%20two%20households.%0A%0A%20%20%20%20**Tools%3A**%20%60drop_duplicates%60%2C%20%60groupby(...).size()%60%2C%20%60len%60%2C%20%60pd.concat%60%20(module%202)%2C%20above.%20The%20row%20order%20does%20not%20matter.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20distinct_by_level(table%3A%20pd.DataFrame%2C%20key%3A%20list)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20number%20of%20distinct%20units%20(region_code%20plus%20key)%20nationally%2C%20by%20region%20and%20by%20province%3A%20level%2C%20area%2C%20units.%22%22%22%0A%20%20%20%20%23%20Exercise%205.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20unit's%20whole%20key%3A%20region_code%20plus%20the%20key%3B%20keep%20each%20unit%20once%2C%20with%20its%20province%20(drop_duplicates)%0A%20%20%20%20%23%20Step%202%3A%20three%20counts%3A%20all%20units%20(national)%2C%20units%20per%20region_code%2C%20units%20per%20region_code%20and%20province_code%0A%20%20%20%20%23%20Step%203%3A%20each%20as%20a%20table%20with%20level%2C%20area%20and%20units%3B%20the%20province's%20area%20is%20region_code%20%2B%20province_code%0A%20%20%20%20%23%20Step%204%3A%20stack%20the%20three%20(pd.concat%2C%20ignore_index%3DTrue)%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.5%22)%0A%0A%0A%40app.function%0Adef%20as_rows(result%2C%20keys%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20result%20table%20in%20one%20shape%20for%20the%20checks%3A%20a%20polars%20table%20as%20pandas%2C%20its%20keys%20as%20columns%2C%20rows%20sorted%20by%20them%2C%0A%20%20%20%20columns%20in%20name%20order%2C%20numbers%20rounded.%20So%20a%20correct%20answer%20passes%20whatever%20its%20row%20order%20or%20index.%22%22%22%0A%20%20%20%20table%20%3D%20result.to_pandas()%20if%20type(result).__module__.startswith(%22polars%22)%20else%20result%0A%20%20%20%20table%20%3D%20table.to_frame()%20if%20isinstance(table%2C%20pd.Series)%20else%20table%0A%20%20%20%20keys%20%3D%20%5Bkeys%5D%20if%20isinstance(keys%2C%20str)%20else%20list(keys)%0A%20%20%20%20if%20any(name%20in%20keys%20for%20name%20in%20table.index.names)%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20table.reset_index()%0A%20%20%20%20table%20%3D%20table.sort_values(keys%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%5Bsorted(table.columns)%5D%0A%20%20%20%20numbers%20%3D%20table.select_dtypes(%22number%22).columns%0A%20%20%20%20table%5Bnumbers%5D%20%3D%20table%5Bnumbers%5D.astype(%22float64%22).round(digits)%0A%20%20%20%20return%20table%0A%0A%0A%40app.function%0Adef%20level_rows(result)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%205.5's%20table%20for%20the%20check%2C%20its%20rows%20sorted%20by%20level%20and%20area%3A%20a%20right%20answer%20in%20another%20order%20passes.%22%22%22%0A%20%20%20%20return%20as_rows(result%2C%20%5B%22level%22%2C%20%22area%22%5D)%0A%0A%0A%40app.function%0Adef%20areas_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Seven%20fishing%20unit%20rows%20of%20four%20households%2C%20used%20by%20the%20check%20of%20Exercise%205.5%3A%20households%205101%20in%20region%2001%20and%20in%0A%20%20%20%20region%2004%20are%20two%20households%3B%20one%20household%20runs%20two%20units%3B%20two%20units%20are%20entered%20twice.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2204%22%2C%20%2204%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province_code%22%3A%20%5B%22033%22%2C%20%22033%22%2C%20%22033%22%2C%20%22055%22%2C%20%22010%22%2C%20%22010%22%2C%20%22056%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B5101%2C%205101%2C%205101%2C%205102%2C%205101%2C%205101%2C%205103%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%2C%201%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22unit_no%22%3A%20%5B1%2C%202%2C%202%2C%201%2C%201%2C%201%2C%201%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_unit%20%3D%20%5B%22hh_id%22%2C%20%22operator_line%22%2C%20%22unit_no%22%5D%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.5%22%2C%20distinct_by_level%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22seven%20rows%3A%20the%20same%20id%20in%20two%20regions%2C%20a%20household%20with%20two%20units%2C%20units%20entered%20twice%22%2C%20(areas_fixture()%2C%20%5B%22hh_id%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20seven%20rows%2C%20counting%20fishing%20units%22%2C%20(areas_fixture()%2C%20_unit))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22households%2C%20from%20the%20survey's%20fishing%20unit%20table%22%2C%20(with_province(units)%2C%20%5B%22hh_id%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22fishing%20units%2C%20from%20the%20survey's%20catch%20table%22%2C%20(with_province(catch)%2C%20_unit))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22operators%2C%20from%20the%20survey's%20members%22%2C%20(with_province(operators)%2C%20%5B%22hh_id%22%2C%20%22mem_line%22%5D))%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%20summarize%3Dlevel_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20unit%20is%20region_code%20plus%20the%20key%3B%20drop%20duplicate%20units%2C%20then%20count%20rows%20nationally%2C%20per%20region_code%2C%20and%20per%20region_code%20and%20province_code.%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%20households%2C%20fishing%20units%20and%20operators%20at%20three%20levels%2C%20side%20by%20side%0A%20%20%20%20def%20_try_levels()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_parts%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22households%22%3A%20distinct_by_level(with_province(units)%2C%20%5B%22hh_id%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22fishing%20units%22%3A%20distinct_by_level(with_province(units)%2C%20%5B%22hh_id%22%2C%20%22operator_line%22%2C%20%22unit_no%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22operators%22%3A%20distinct_by_level(with_province(operators)%2C%20%5B%22hh_id%22%2C%20%22mem_line%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20except%20Exception%3A%20%20%23%20not%20written%20yet%2C%20or%20an%20error%3A%20try_it%20says%20which%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20try_it(distinct_by_level%2C%20with_province(units)%2C%20%5B%22hh_id%22%5D)%0A%20%20%20%20%20%20%20%20_tables%20%3D%20%5Bpart.set_index(%5B%22level%22%2C%20%22area%22%5D)%5B%22units%22%5D.rename(name)%20for%20name%2C%20part%20in%20_parts.items()%5D%0A%20%20%20%20%20%20%20%20return%20pd.concat(_tables%2C%20axis%3D1).reset_index()%0A%0A%20%20%20%20_try_levels()%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%205.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%20The%20national%20count%20is%20below%20the%20regions'%20sum%20%7C%20The%20region%20is%20not%20in%20the%20unit's%20key%2C%20so%20ids%20shared%20by%20two%20regions%20were%20counted%20once.%20%7C%20%60%5B%22region_code%22%2C%20*key%5D%60%20%7C%0A%20%20%20%20%7C%20Counts%20equal%20the%20table's%20rows%20%7C%20Rows%20counted%2C%20not%20units.%20%7C%20%60drop_duplicates%60%20first%20%7C%0A%20%20%20%20%7C%20One%20column%20per%20key%20part%2C%20or%20*TypeError%3A%20Index(...)%20must%20be%20called%20with%20a%20collection%20of%20some%20kind*%20at%20%60.rename(%22units%22)%60%20%7C%20%60nunique%60%20given%20several%20columns%20counts%20each%20column%20on%20its%20own%2C%20and%20gives%20a%20table%2C%20not%20one%20column.%20%7C%20%60drop_duplicates(...)%60%2C%20then%20%60size()%60%20%7C%0A%20%20%20%20%7C%20The%20fixture's%20fishing%20units%20are%204%2C%20not%205%20%7C%20Duplicates%20were%20dropped%20on%20fewer%20columns%20than%20the%20key%2C%20so%20two%20units%20of%20one%20household%20merged.%20%7C%20%60%5B%22region_code%22%2C%20*key%5D%60%2C%20every%20part%20%7C%0A%20%20%20%20%7C%20A%20province's%20area%20is%20%60033%60%20%7C%20The%20region%20code%20was%20not%20joined%20to%20the%20province%20code.%20%7C%20%60region_code%20%2B%20province_code%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'province_code'*%20in%20the%20Try%20it%20cell%20%7C%20The%20table%20was%20given%20without%20its%20province.%20%7C%20%60with_province(table)%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%20Fishing%20units%20by%20species%3A%20one%20unit%2C%20many%20species%0A%0A%20%20%20%20**The%20problem.**%20The%20species%20table%20counts%20fishing%20units%2C%20and%20a%20unit%20catches%20several%20species%2C%20so%20it%20belongs%20to%20several%20rows%20of%20the%20table.%20That%20is%20a%20**multiple-response**%20table%3A%20its%20rows%20add%20up%20to%20more%20than%20the%20number%20of%20units%2C%20and%20its%20total%20row%20is%20not%20the%20sum%20of%20the%20rows%20above%20it.%20Two%20traps%20sit%20in%20it%3A%0A%0A%20%20%20%20-%20A%20unit%20that%20caught%20two%20species%20pooled%20into%20%22Other%22%20has%20two%20catch%20rows%20labeled%20%22Other%22.%20Counting%20rows%20counts%20it%20twice%20in%20%22Other%22%3B%20counting%20distinct%20units%20counts%20it%20once.%0A%20%20%20%20-%20The%20**Total**%20is%20the%20number%20of%20units%20that%20caught%20anything%3A%20distinct%20units%20over%20the%20whole%20table%2C%20which%20is%20fewer%20than%20the%20sum%20of%20the%20rows.%0A%0A%20%20%20%20**The%20tool.**%20Count%20distinct%20units%2C%20by%20their%20whole%20key%2C%20in%20every%20row%20of%20the%20table%2C%20and%20once%20more%20over%20the%20whole%20table%20for%20the%20total.%20%60units_by_label%60%2C%20below%2C%20does%20it%20in%20polars%3A%20%60pl.struct(UNIT_KEY).n_unique()%60%20counts%20the%20distinct%20keys.%20In%20pandas%2C%20%60drop_duplicates(%5B*UNIT_KEY%2C%20%22species_label%22%5D)%60%20and%20then%20%60groupby(%22species_label%22).size()%60%20count%20the%20same.%20The%20stretch%20S2%2C%20%5Btabulation%5D(%2F%3Ffile%3D61_stretch_tabulation.py)%2C%20has%20it%20as%20an%20exercise.%20The%20chart%20after%20it%20is%20the%20table%20of%20fishing%20units%20by%20the%20eight%20species%20caught%20most%2C%20unweighted%2C%20from%20your%20Exercise%205.4's%20labels.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20units_by_label(catch%3A%20pl.DataFrame)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22Distinct%20fishing%20units%20per%20species_label%2C%20and%20a%20last%20row%20Total%3A%20the%20distinct%20units%20in%20the%20whole%20table.%22%22%22%0A%20%20%20%20by_label%20%3D%20catch.group_by(%22species_label%22).agg(pl.struct(UNIT_KEY).n_unique().alias(%22units%22))%0A%20%20%20%20total%20%3D%20pl.DataFrame(%7B%22species_label%22%3A%20%5B%22Total%22%5D%2C%20%22units%22%3A%20%5Bcatch.select(pl.struct(UNIT_KEY).n_unique()).item()%5D%7D)%0A%20%20%20%20return%20pl.concat(%5Bby_label%2C%20total%5D%2C%20how%3D%22vertical_relaxed%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20survey's%20catch%2C%20labeled%20by%20your%20Exercise%205.4%3A%20the%20top%20eight%20species%2C%20Other%2C%20and%20the%20two%20catch-alls%0A%20%20%20%20_labels%20%3D%20attempt(top_species%2C%20catch%2C%20species%2C%208)%0A%20%20%20%20species_catch%20%3D%20NOT_YET%20if%20_labels%20is%20NOT_YET%20else%20pl.from_pandas(catch%5BUNIT_KEY%5D.assign(species_label%3D_labels))%0A%20%20%20%20return%20(species_catch%2C)%0A%0A%0A%40app.cell%0Adef%20_(species_catch)%3A%0A%20%20%20%20%23%20Fishing%20units%20by%20the%20top%20eight%20species%20(unweighted)%2C%20largest%20first%2C%20with%20Other%20and%20the%20catch-alls%20at%20the%20bottom%0A%20%20%20%20def%20_table_or_not_yet()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20NOT_YET%20if%20species_catch%20is%20NOT_YET%20else%20units_by_label(species_catch)%0A%20%20%20%20%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20NOT_YET%0A%0A%20%20%20%20_table%20%3D%20_table_or_not_yet()%0A%20%20%20%20mo.stop(_table%20is%20NOT_YET%2C%20waiting_for(%22Exercise%205.4%22))%0A%20%20%20%20_table%20%3D%20_table.to_pandas()%0A%20%20%20%20_total%20%3D%20int(_table.loc%5B_table%5B%22species_label%22%5D%20%3D%3D%20%22Total%22%2C%20%22units%22%5D.iloc%5B0%5D)%0A%20%20%20%20_rows%20%3D%20_table%5B_table%5B%22species_label%22%5D%20!%3D%20%22Total%22%5D.copy()%0A%20%20%20%20_rows%5B%22last%22%5D%20%3D%20_rows%5B%22species_label%22%5D.isin(%5B%22Other%22%2C%20INVALID%2C%20NO_ANSWER%5D)%0A%20%20%20%20_rows%20%3D%20_rows.sort_values(%5B%22last%22%2C%20%22units%22%5D%2C%20ascending%3D%5BFalse%2C%20True%5D)%20%20%23%20barh%20draws%20from%20the%20bottom%20up%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%203.8))%0A%20%20%20%20_ax.barh(_rows%5B%22species_label%22%5D%2C%20_rows%5B%22units%22%5D%2C%20color%3Dnp.where(_rows%5B%22last%22%5D%2C%20%22%239e9e9e%22%2C%20%22%230072b2%22))%0A%20%20%20%20_ax.set_xlabel(f%22fishing%20units%20that%20caught%20the%20species%20(of%20%7B_total%3A%2C%7D%20units%2C%20unweighted)%22)%0A%20%20%20%20_ax.set_title(%22Fishing%20units%20by%20the%20eight%20species%20caught%20most%2C%20survey%20sample%22)%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_ax%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%20bars%20add%20up%20to%20more%20than%20the%20total%20in%20the%20axis%20label%3A%20each%20unit%20is%20in%20a%20bar%20for%20every%20species%20it%20caught.%20A%20note%20under%20a%20published%20table%20says%20so%20(%22a%20fishing%20unit%20may%20report%20more%20than%20one%20species%22)%2C%20so%20no%20reader%20adds%20the%20rows.%0A%0A%20%20%20%20%23%23%203%20%C2%B7%20The%20tabulation%20frame%20an%20estimate%20is%20computed%20from%0A%0A%20%20%20%20**The%20problem.**%20Module%206%20estimates%20totals%20with%20their%20standard%20errors%20in%20svy%2C%20and%20svy%20takes%20**one%20table**%3A%20one%20row%20per%20unit%20of%20the%20estimate%2C%20with%20every%20column%20a%20table%20will%20be%20cut%20by%20and%20the%20columns%20that%20describe%20how%20the%20unit%20was%20sampled.%20The%20survey%20stores%20none%20of%20it%20in%20one%20place%3A%20the%20operator%20is%20in%20the%20members%20table%2C%20the%20stratum%20and%20weight%20in%20the%20selection%20table%20beside%20each%20household.%20A%20careless%20build%20makes%20every%20estimate%20after%20it%20wrong%20without%20an%20error%3A%20an%20operator%20on%20two%20rows%20has%20their%20weight%20counted%20twice.%0A%0A%20%20%20%20**The%20tool.**%20The%20**tabulation%20frame**%3A%20one%20row%20per%20operator%2C%20built%20from%20the%20members%20with%20one%20join%20to%20the%20household's%20design%20columns%2C%20then%20given%20its%20categories.%20Five%20design%20columns%20come%20with%20it%2C%20made%20by%20%60course.design_frame()%60%20(module%206%20builds%20them%20step%20by%20step)%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20what%20it%20is%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60stratum_id%60%20%7C%20the%20stratum%3A%20region%2C%20province%20and%20stratum%20letter%20(%60sel_stratum%60)%2C%20such%20as%20%6001-033-C%60.%20Each%20stratum%20was%20sampled%20on%20its%20own%20%7C%0A%20%20%20%20%7C%20%60psu_id%60%20%7C%20the%20primary%20sampling%20unit%3A%20the%20enumeration%20area.%20In%20stratum%20F%2C%20where%20every%20registered%20large%20fishing%20unit%20is%20taken%2C%20each%20household%20is%20its%20own%20PSU%20%7C%0A%20%20%20%20%7C%20%60psus_in_stratum%60%20%7C%20the%20number%20of%20PSUs%20the%20stratum%20has%20in%20the%20frame%2C%20for%20the%20finite%20population%20correction.%20A%20count%2C%20not%20a%20fraction%3A%20R's%20%60svydesign%60%20takes%20either%2C%20and%20the%20course%20gives%20svy%20counts%20%7C%0A%20%20%20%20%7C%20%60sel_final_weight%60%20%7C%20the%20final%20weight%3A%20how%20many%20households%20in%20the%20population%20the%20household%20stands%20for%20%7C%0A%20%20%20%20%7C%20%60province_code%60%20%7C%20the%20province%2C%20which%20the%20frame%20needs%20for%20provincial%20tables%20and%20the%20members%20table%20lacks%20%7C%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%20How%20the%20design%20differs%20by%20stratum%3A%20PSUs%20in%20the%20frame%20and%20in%20the%20sample%2C%20and%20the%20weights'%20range%0A%20%20%20%20_strata%20%3D%20design.groupby(%22sel_stratum%22).agg(%0A%20%20%20%20%20%20%20%20strata%3D(%22stratum_id%22%2C%20%22nunique%22)%2C%0A%20%20%20%20%20%20%20%20households%3D(%22hh_id%22%2C%20%22size%22)%2C%0A%20%20%20%20%20%20%20%20psus_sampled%3D(%22psu_id%22%2C%20%22nunique%22)%2C%0A%20%20%20%20%20%20%20%20smallest_weight%3D(%22sel_final_weight%22%2C%20%22min%22)%2C%0A%20%20%20%20%20%20%20%20largest_weight%3D(%22sel_final_weight%22%2C%20%22max%22)%2C%0A%20%20%20%20).round(2)%0A%20%20%20%20%23%20psus_in_stratum%20is%20the%20stratum's%2C%20repeated%20on%20each%20of%20its%20households%3A%20count%20it%20once%20per%20stratum%0A%20%20%20%20_strata.insert(3%2C%20%22psus_in_frame%22%2C%20design.drop_duplicates(%22stratum_id%22).groupby(%22sel_stratum%22)%5B%22psus_in_stratum%22%5D.sum())%0A%20%20%20%20_strata%20%3D%20_strata.loc%5B%5B%22F%22%2C%20%22A%22%2C%20%22B%22%2C%20%22C%22%5D%5D%0A%20%20%20%20%23%20The%20strata%20whose%20sample%20has%20more%20PSUs%20than%20their%20frame%0A%20%20%20%20_per_stratum%20%3D%20design.groupby(%22stratum_id%22).agg(sampled%3D(%22psu_id%22%2C%20%22nunique%22)%2C%20frame%3D(%22psus_in_stratum%22%2C%20%22first%22))%0A%20%20%20%20_over%20%3D%20_per_stratum.index%5B_per_stratum%5B%22sampled%22%5D%20%3E%20_per_stratum%5B%22frame%22%5D%5D.tolist()%0A%20%20%20%20if%20_over%3A%0A%20%20%20%20%20%20%20%20_note%20%3D%20(f%22Taken%20one%20by%20one%2C%20**%7Blen(_over)%7D%20of%20the%20%7Blen(_per_stratum)%7D%20strata%20%7B'has'%20if%20len(_over)%20%3D%3D%201%20else%20'have'%7D%20more%20PSUs%20in%20the%20sample%20than%20in%20the%20frame**%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22(%7B'%2C%20'.join(_over)%7D)%3A%20a%20household%20copied%20under%20another%20id%2C%20which%20module%206%20finds%20before%20it%20estimates.%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_note%20%3D%20f%22Taken%20one%20by%20one%2C%20none%20of%20the%20%7Blen(_per_stratum)%7D%20strata%20has%20more%20PSUs%20in%20the%20sample%20than%20in%20the%20frame.%22%0A%20%20%20%20mo.vstack(%5B_strata%2C%20mo.md(_note)%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%20Stratum%20F%20takes%20every%20registered%20large%20fishing%20unit%2C%20so%20its%20households%20stand%20for%20about%20themselves%3B%20strata%20A%2C%20B%20and%20C%20take%20a%20sample%20of%20their%20areas%2C%20at%20rates%20that%20fall%20from%20A%20to%20C%2C%20and%20a%20household%20in%20stratum%20C%20stands%20for%20many.%20An%20unweighted%20count%20gives%20each%20interviewed%20operator%20the%20same%20say%2C%20so%20it%20describes%20the%20**sample**.%20The%20weights%20turn%20it%20into%20an%20estimate%20for%20the%20**population**%2C%20and%20module%206%20does%20that%20with%20the%20strata%20and%20PSUs%2C%20which%20give%20the%20estimate%20its%20standard%20error.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.6.%20One%20row%20per%20operator%2C%20with%20the%20design%20columns%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20join%20every%20estimate%20in%20module%206%20rests%20on.%20One%20row%20too%20many%20and%20an%20operator's%20weight%20counts%20twice%3B%20one%20join%20key%20too%20few%20and%20households%20of%20different%20regions%20trade%20weights.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20members%20table%20and%20the%20design%20frame%20%E2%86%92%20**one%20row%20per%20operator**%20(a%20member%20whose%20%60mem_role_operator%60%20is%201)%2C%20with%20at%20least%20these%20columns%3A%0A%0A%20%20%20%20%7C%20from%20the%20members%20%7C%20from%20the%20design%20frame%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60region_code%60%2C%20%60hh_id%60%2C%20%60mem_line%60%20(the%20operator's%20key)%20%7C%20%60province_code%60%20%7C%0A%20%20%20%20%7C%20%60mem_sex%60%2C%20%60mem_age%60%2C%20%60mem_education%60%20(sex%2C%20age%2C%20education)%20%7C%20%60stratum_id%60%2C%20%60psu_id%60%2C%20%60psus_in_stratum%60%20%7C%0A%20%20%20%20%7C%20%7C%20%60sel_final_weight%60%20%7C%0A%0A%20%20%20%20Keep%20**every**%20operator%2C%20even%20one%20whose%20household%20has%20no%20row%20in%20the%20design%20frame%3A%20its%20design%20columns%20stay%20missing%2C%20so%20the%20gap%20shows%20instead%20of%20the%20operator%20disappearing.%20Join%20on%20the%20household's%20whole%20key%2C%20and%20let%20pandas%20check%20that%20each%20operator%20finds%20one%20household%20at%20most.%0A%0A%20%20%20%20**Tools%3A**%20a%20boolean%20filter%20(%60%3D%3D%201%60)%2C%20column%20selection%2C%20and%20%60merge%60%20with%20%60on%3D%60%2C%20%60how%3D%60%20and%20%60validate%3D%60%20(module%202)%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20operator_frame(members%3A%20pd.DataFrame%2C%20design%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20operator%3A%20key%2C%20sex%2C%20age%20and%20education%20from%20the%20members%3B%20province%2C%20stratum_id%2C%20psu_id%2C%20psus_in_stratum%20and%20sel_final_weight%20from%20the%20design%20frame.%22%22%22%0A%20%20%20%20%23%20Exercise%205.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20operators%3A%20members%20whose%20mem_role_operator%20is%201%3B%20keep%20the%20six%20member%20columns%0A%20%20%20%20%23%20Step%202%3A%20from%20the%20design%20frame%2C%20the%20household's%20key%20(region_code%2C%20hh_id)%20and%20the%20five%20design%20columns%0A%20%20%20%20%23%20Step%203%3A%20join%20them%20on%20region_code%20and%20hh_id%2C%20keeping%20every%20operator%2C%20with%20validate%3D%22many_to_one%22%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.6%22)%0A%0A%0A%40app.function%0Adef%20design_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Four%20households%20of%20three%20regions%2C%20all%20numbered%205101%20or%205102%2C%20used%20by%20the%20check%20of%20Exercise%205.6.%20Household%205103%20of%0A%20%20%20%20region%2007%20is%20not%20here%2C%20so%20its%20operators%20find%20no%20design%20row.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2204%22%2C%20%2204%22%2C%20%2207%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B5101%2C%205101%2C%205102%2C%205101%5D%2C%0A%20%20%20%20%20%20%20%20%22province_code%22%3A%20%5B%22033%22%2C%20%22010%22%2C%20%22010%22%2C%20%22022%22%5D%2C%0A%20%20%20%20%20%20%20%20%22stratum_id%22%3A%20%5B%2201-033-C%22%2C%20%2204-010-F%22%2C%20%2204-010-A%22%2C%20%2207-022-B%22%5D%2C%0A%20%20%20%20%20%20%20%20%22psu_id%22%3A%20%5B%2201-033-C-3004%22%2C%20%2204-hh-5101%22%2C%20%2204-010-A-1003%22%2C%20%2207-022-B-2007%22%5D%2C%0A%20%20%20%20%20%20%20%20%22psus_in_stratum%22%3A%20%5B150%2C%2010%2C%2010%2C%2045%5D%2C%0A%20%20%20%20%20%20%20%20%22sel_final_weight%22%3A%20%5B15.2%2C%201.0%2C%202.6%2C%205.1%5D%2C%0A%20%20%20%20%20%20%20%20%22sel_stratum%22%3A%20%5B%22C%22%2C%20%22F%22%2C%20%22A%22%2C%20%22B%22%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20frame_rows(result)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20columns%20Exercise%205.6%20asks%20for%2C%20rows%20sorted%20by%20the%20operator's%20key%2C%20for%20the%20check%3A%20other%20columns%20don't%20count.%22%22%22%0A%20%20%20%20wanted%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22mem_line%22%2C%20%22mem_sex%22%2C%20%22mem_age%22%2C%20%22mem_education%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22province_code%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%5D%0A%20%20%20%20return%20as_rows(result%5Bwanted%5D%2C%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22mem_line%22%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%22Exercise%205.6%22%2C%20operator_frame%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22nine%20members%20of%20three%20regions%2C%20in%20households%20numbered%205101%20to%205103%2C%20one%20household%20with%20no%20design%20row%22%2C%20(members_fixture()%2C%20design_fixture()))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20members%20and%20design%20frame%22%2C%20(members%2C%20design))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20members%20of%20region%2004%20and%20the%20whole%20design%20frame%22%2C%20(members%5Bmembers%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D%2C%20design))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22pairs%20of%20tables%22%2C%20summarize%3Dframe_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20the%20members%20whose%20mem_role_operator%20is%201%3B%20left-join%20the%20design%20columns%20on%20region_code%20and%20hh_id%20with%20validate%3D'many_to_one'.%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%20your%20frame%20for%20the%20survey%0A%20%20%20%20try_it(operator_frame%2C%20members%2C%20design)%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%205.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%20*MergeError%3A%20Merge%20keys%20are%20not%20unique%20in%20right%20dataset*%20%7C%20Joined%20on%20%60hh_id%60%20alone%3A%20the%20id%20repeats%20across%20regions%2C%20and%20%60validate%3D%60%20caught%20it.%20%7C%20%60on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20%22%5B'region_code'%5D%20not%20in%20index%22*%20in%20the%20check%20%7C%20Joined%20on%20%60hh_id%60%20alone%20without%20%60validate%3D%60%3A%20both%20tables'%20%60region_code%60%20came%20back%20as%20%60region_code_x%60%20and%20%60region_code_y%60.%20%7C%20the%20join%20key%20%7C%0A%20%20%20%20%7C%20The%20fixture's%20operators%20in%20household%205103%20of%20region%2007%20are%20missing%20%7C%20An%20inner%20join%20dropped%20them.%20%7C%20%60how%3D%22left%22%60%20%7C%0A%20%20%20%20%7C%20Thousands%20of%20rows%3A%20every%20member%20%7C%20The%20operators%20were%20not%20filtered.%20%7C%20%60members%5B%22mem_role_operator%22%5D%20%3D%3D%201%60%20%7C%0A%20%20%20%20%7C%20*KeyError*%20naming%20a%20design%20column%20%7C%20The%20design%20frame's%20column%20was%20not%20selected%2C%20or%20its%20name%20was%20misread%20(%60sel_ea%60%20is%20the%20area's%20number%3B%20%60psu_id%60%20is%20the%20course's%20PSU%20id).%20%7C%20the%20five%20design%20columns%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%20The%20frame%20with%20its%20categories%0A%0A%20%20%20%20The%20frame%20holds%20codes.%20The%20cell%20below%20gives%20it%20the%20categories%20of%20section%201%2C%20sex%2C%20age%20group%20and%20education%20group%2C%20with%20your%20functions%20from%20Exercises%205.1%2C%205.3%20and%205.6%2C%20and%20then%20checks%20what%20the%20frame%20promises%3A%20one%20row%20per%20operator%2C%20every%20operator%20with%20a%20weight.%20A%20member's%20role%20(Exercise%205.2)%20is%20not%20among%20its%20columns%3A%20every%20row%20of%20this%20frame%20runs%20a%20unit.%20The%20role%20is%20a%20column%20of%20the%20tables%20of%20members%20who%20fish%2C%20and%20the%20cell%20after%20Exercise%205.7%20makes%20one.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(education_codes)%3A%0A%20%20%20%20%23%20The%20tabulation%20frame%3A%20the%20operators%20with%20their%20design%20columns%20(5.6)%2C%20and%20their%20sex%2C%20age%20group%20(5.3)%20and%20education%20(5.1)%0A%20%20%20%20def%20_build()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20base%20%3D%20operator_frame(members%2C%20design)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20base.assign(sex%3Dsex_category(base%5B%22mem_sex%22%5D)%2C%20education%3Deducation_group(base%2C%20education_codes)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20age_group%3Dage_group(base%5B%22mem_age%22%5D))%0A%20%20%20%20%20%20%20%20except%20Exception%3A%20%20%23%20an%20exercise%20not%20written%20yet%2C%20or%20one%20that%20still%20raises%3A%20its%20own%20Try%20it%20cell%20says%20which%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20NOT_YET%0A%0A%20%20%20%20frame%20%3D%20_build()%0A%20%20%20%20_key%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22mem_line%22%5D%0A%20%20%20%20if%20frame%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20_shown%20%3D%20waiting_for(%22Exercises%205.1%2C%205.3%20and%205.6%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_shown%20%3D%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(frame)%3A%2C%7D%20rows%20for%20%7Blen(operators)%3A%2C%7D%20operators**%3B%20%7Bframe.duplicated(_key).sum()%7D%20operator%20keys%20repeat%3B%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bframe%5B'sel_final_weight'%5D.isna().sum()%7D%20operators%20have%20no%20weight%3B%20%7Bframe%5B'stratum_id'%5D.nunique()%7D%20strata%2C%20%7Bframe%5B'psu_id'%5D.nunique()%3A%2C%7D%20PSUs.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20frame%5B%5B*_key%2C%20%22province_code%22%2C%20%22sex%22%2C%20%22age_group%22%2C%20%22education%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%5D%5D.head(6)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%20%20%20%20_shown%0A%20%20%20%20return%20(frame%2C)%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%20Unweighted%20tables%3A%20every%20category%2C%20in%20order%0A%0A%20%20%20%20A%20published%20table%20lists%20**every**%20category%20of%20its%20rows%20and%20columns%2C%20in%20the%20order%20the%20categories%20have%2C%20and%20a%20category%20with%20no%20one%20in%20it%20shows%20a%200.%20A%20reader%20then%20sees%20that%20the%20survey%20found%20no%20operator%20with%20a%20missing%20sex%2C%20instead%20of%20wondering%20whether%20the%20row%20was%20forgotten%3B%20and%20two%20tables%20of%20the%20same%20variable%20line%20up%20row%20for%20row.%0A%0A%20%20%20%20%60pd.crosstab(rows%2C%20columns)%60%20counts%20the%20units%20in%20each%20cell.%20Two%20of%20its%20arguments%20make%20the%20table%20a%20published%20one%3A%0A%0A%20%20%20%20%7C%20argument%20%7C%20what%20it%20does%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60dropna%3DFalse%60%20%7C%20keeps%20the%20categories%20no%20row%20has%2C%20as%20rows%20and%20columns%20of%20zeros.%20The%20default%20drops%20them.%20%7C%0A%20%20%20%20%7C%20%60margins%3DTrue%2C%20margins_name%3D%22Total%22%60%20%7C%20adds%20a%20total%20row%20and%20a%20total%20column.%20%7C%0A%0A%20%20%20%20A%20categorical%20column%20brings%20its%20categories%20and%20their%20order%20into%20the%20table.%20A%20text%20column%20is%20sorted%20by%20letter%20and%20lists%20only%20the%20values%20it%20has.%0A%0A%20%20%20%20%23%23%23%20Exercise%205.7.%20An%20unweighted%20table%20with%20every%20category%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20unweighted%20table%20is%20the%20first%20one%20checked%20against%20the%20field%20reports%20(how%20many%20operators%20were%20interviewed%20in%20each%20group)%20and%20the%20one%20module%206's%20estimates%20sit%20beside.%20It%20must%20list%20every%20category%2C%20in%20order%2C%20with%20its%20totals.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20frame%20(one%20row%20per%20unit)%20and%20the%20names%20of%20two%20of%20its%20columns%20%E2%86%92%20the%20number%20of%20rows%20in%20each%20combination%2C%20with%3A%0A%0A%20%20%20%20-%20one%20row%20per%20category%20of%20the%20%60rows%60%20column%20and%20one%20column%20per%20category%20of%20the%20%60columns%60%20column%2C%20**empty%20categories%20included**%2C%20in%20the%20categories'%20order%3B%0A%20%20%20%20-%20a%20last%20row%20and%20a%20last%20column%20named%20%60%22Total%22%60.%0A%0A%20%20%20%20%7C%20age_group%20%7C%20Male%20%7C%20Female%20%7C%20Invalid%20value%20%7C%20No%20answer%20%7C%20Total%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%20Under%2010%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%200%20%7C%200%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20Total%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60pd.crosstab%60%20with%20%60dropna%3DFalse%60%2C%20%60margins%3DTrue%60%20and%20%60margins_name%3D%22Total%22%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20unweighted_table(frame%3A%20pd.DataFrame%2C%20rows%3A%20str%2C%20columns%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Rows%20of%20the%20frame%20counted%20by%20two%20columns%3A%20every%20category%2C%20empty%20ones%20included%2C%20in%20order%2C%20with%20Total%20row%20and%20column.%22%22%22%0A%20%20%20%20%23%20Exercise%205.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20pd.crosstab%20of%20the%20rows%20column%20by%20the%20columns%20column%0A%20%20%20%20%23%20Step%202%3A%20keep%20empty%20categories%20(dropna%3DFalse)%20and%20add%20totals%20named%20%22Total%22%20(margins%3DTrue%2C%20margins_name%3D%22Total%22)%3B%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%205.7%22)%0A%0A%0A%40app.function%0Adef%20as_table(result)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22A%20table%20for%20the%20checks%3A%20its%20row%20and%20column%20labels%20as%20texts%2C%20in%20order%2C%20and%20its%20counts.%22%22%22%0A%20%20%20%20return%20%7B%22rows%22%3A%20%5Bstr(label)%20for%20label%20in%20result.index%5D%2C%20%22columns%22%3A%20%5Bstr(label)%20for%20label%20in%20result.columns%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22counts%22%3A%20result.to_numpy().astype(%22int64%22).tolist()%7D%0A%0A%0A%40app.function%0Adef%20table_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Five%20operators%20with%20an%20ordered%20age%20group%20and%20a%20sex%2C%20used%20by%20the%20check%20of%20Exercise%205.7%3A%20several%20categories%20of%20each%0A%20%20%20%20have%20no%20one%20in%20them%2C%20and%20the%20age%20groups'%20order%20is%20not%20their%20order%20as%20text.%22%22%22%0A%20%20%20%20ages%20%3D%20%5B%22Under%2010%22%2C%20%2210-17%22%2C%20%2218-29%22%2C%20%2230-44%22%2C%20%2245-59%22%2C%20%2260%20and%20over%22%2C%20INVALID%2C%20NO_ANSWER%5D%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22age_group%22%3A%20pd.Categorical(%5B%2260%20and%20over%22%2C%20%2210-17%22%2C%20%22Under%2010%22%2C%20%2210-17%22%2C%20%2260%20and%20over%22%5D%2C%20categories%3Dages%2C%20ordered%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%22sex%22%3A%20pd.Categorical(%5B%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Male%22%2C%20%22Male%22%5D%2C%20categories%3D%5B%22Male%22%2C%20%22Female%22%2C%20INVALID%2C%20NO_ANSWER%5D%2C%20ordered%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2207%22%2C%20%2207%22%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_operators%20%3D%20operators.assign(sex%3Dsex_category(operators%5B%22mem_sex%22%5D))%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%205.7%22%2C%20unweighted_table%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%20operators%3A%20empty%20age%20groups%20and%20sexes%2C%20age%20groups%20whose%20order%20is%20not%20alphabetical%22%2C%20(table_fixture()%2C%20%22age_group%22%2C%20%22sex%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20five%20operators%2C%20sex%20by%20region%22%2C%20(table_fixture()%2C%20%22sex%22%2C%20%22region_code%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20operators%2C%20region%20by%20sex%22%2C%20(_operators%2C%20%22region_code%22%2C%20%22sex%22))%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%3Das_table%2C%0A%20%20%20%20%20%20%20%20hint%3D%22pd.crosstab(frame%5Brows%5D%2C%20frame%5Bcolumns%5D%2C%20dropna%3DFalse%2C%20margins%3DTrue%2C%20margins_name%3D'Total').%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20operators%20by%20age%20group%20and%20sex%2C%20unweighted%0A%20%20%20%20try_it(unweighted_table%2C%20frame%2C%20%22age_group%22%2C%20%22sex%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%205.7%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%20%60Invalid%20value%60%20or%20%60No%20answer%60%20column%3B%20fewer%20rows%20%7C%20Empty%20categories%20dropped%2C%20the%20default.%20%7C%20%60dropna%3DFalse%60%20%7C%0A%20%20%20%20%7C%20No%20%60Total%60%20row%20or%20column%20%7C%20The%20margins%20are%20missing.%20%7C%20%60margins%3DTrue%60%20%7C%0A%20%20%20%20%7C%20A%20row%20and%20column%20named%20%60All%60%20%7C%20pandas'%20default%20name%20for%20the%20margins.%20%7C%20%60margins_name%3D%22Total%22%60%20%7C%0A%20%20%20%20%7C%20Age%20groups%20in%20the%20order%20%6010-17%60%2C%20%6060%20and%20over%60%2C%20%60Under%2010%60%20%7C%20The%20category%20column%20was%20turned%20into%20text%2C%20which%20sorts%20by%20letter.%20%7C%20pass%20the%20categorical%20columns%20as%20they%20are%20%7C%0A%20%20%20%20%7C%20The%20counts%20are%20weights%20%7C%20%60values%3D%60%20and%20%60aggfunc%3D%22sum%22%60%20summed%20a%20weight%3A%20that%20is%20module%206's%20estimate%2C%20not%20a%20count.%20%7C%20count%20rows%20only%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%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20same%20function%20on%20another%20unit%3A%20the%20members%20who%20fished%2C%20by%20role%20(Exercise%205.2)%20and%20sex%0A%20%20%20%20def%20_fishers_by_role()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_roles%20%3D%20pd.Categorical(fishing_role(fishers)%2C%20categories%3D%5B*ROLES%2C%20INVALID%2C%20NO_ANSWER%5D%2C%20ordered%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20unweighted_table(fishers.assign(role%3D_roles%2C%20sex%3Dsex_category(fishers%5B%22mem_sex%22%5D))%2C%20%22role%22%2C%20%22sex%22)%0A%20%20%20%20%20%20%20%20except%20Exception%3A%20%20%23%20an%20exercise%20not%20written%20yet%2C%20or%20one%20that%20still%20raises%3A%20its%20own%20Try%20it%20cell%20says%20which%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20NOT_YET%0A%0A%20%20%20%20_table%20%3D%20_fishers_by_role()%0A%20%20%20%20mo.stop(_table%20is%20NOT_YET%2C%20waiting_for(%22Exercises%205.2%20and%205.7%22))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**The%20members%20who%20fished%2C%20by%20role%20and%20sex.**%20The%20frame%20here%20is%20%60fishers%60%2C%20one%20row%20per%20member%20who%20fished%2C%20so%20the%20table%20counts%20%7Blen(fishers)%3A%2C%7D%20members.%20Your%20%60unweighted_table%60%20is%20the%20same%20function%3A%20the%20unit%20of%20a%20table%20is%20whatever%20one%20row%20of%20its%20frame%20is.%22)%2C%0A%20%20%20%20%20%20%20%20_table%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20From%20here%20to%20module%206%0A%0A%20%20%20%20The%20frame%20is%20ready%3A%20one%20row%20per%20operator%2C%20its%20categories%20as%20ordered%20columns%2C%20its%20design%20columns%20beside%20them.%20Every%20table%20in%20this%20module%20is%20**unweighted**%3A%20it%20counts%20the%20operators%20the%20survey%20interviewed.%20That%20is%20what%20the%20field%20reports%20are%20checked%20against%2C%20and%20it%20is%20the%20%60n%60%20a%20published%20table%20prints%20beside%20each%20estimate%2C%20so%20a%20reader%20knows%20how%20many%20interviews%20an%20estimate%20rests%20on.%0A%0A%20%20%20%20It%20is%20not%20the%20number%20of%20operators%20in%20the%20provinces.%20Summing%20%60sel_final_weight%60%20per%20cell%20would%20give%20an%20estimate%20of%20that%2C%20but%20not%20how%20far%20off%20the%20estimate%20may%20be.%20That%20depends%20on%20how%20the%20sample%20was%20drawn%3A%20how%20many%20areas%20each%20stratum%20sampled%2C%20how%20alike%20the%20households%20of%20one%20area%20are%2C%20how%20many%20areas%20the%20stratum%20has.%20That%20is%20the%20design.%20Module%206%20first%20finds%20what%20would%20stop%20an%20estimate%2C%20such%20as%20the%20copied%20household%20above%2C%20then%20hands%20the%20frame%20to%20svy%20with%20the%20design%20columns%20named%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20import%20svy%0A%20%20%20%20sample%20%3D%20svy.Sample(%0A%20%20%20%20%20%20%20%20data%3Dpl.from_pandas(frame)%2C%20%20%20%20%20%20%20%23%20svy%20reads%20polars%20tables%2C%20not%20pandas%0A%20%20%20%20%20%20%20%20design%3Dsvy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20pop_size%3D%22psus_in_stratum%22%2C%20singleton%3D%22skip%22)%2C%0A%20%20%20%20)%0A%20%20%20%20sample.estimation.total(y%3D...%2C%20by%3D%22sex%22)%20%20%20%23%20totals%20with%20SE%2C%20CV%20and%20a%2095%25%20CI%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20svy%20reads%20polars%2C%20so%20the%20frame%20goes%20through%20%60pl.from_pandas%60.%20The%20cell%20below%20converts%20it%20and%20shows%20the%20types%20it%20arrives%20with%3A%20the%20categorical%20columns%20become%20polars%20%60Categorical%60%2C%20and%20the%20nullable%20integers%20stay%20integers%20with%20nulls.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20frame%20as%20polars%2C%20the%20form%20svy%20reads%3A%20each%20column's%20type%20after%20the%20conversion%0A%20%20%20%20mo.stop(frame%20is%20NOT_YET%2C%20waiting_for(%22Exercises%205.1%2C%205.3%20and%205.6%22))%0A%20%20%20%20_polars%20%3D%20pl.from_pandas(frame)%0A%20%20%20%20pl.DataFrame(%7B%22column%22%3A%20list(_polars.schema.keys())%2C%20%22polars%20type%22%3A%20%5Bstr(t)%20for%20t%20in%20_polars.schema.values()%5D%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20%7C%20use%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20recode%20from%20a%20code%20list%20%7C%20the%20list%20as%20a%20lookup%3A%20%60.map(codes.set_index(%22code%22)%5B%22group%22%5D)%60%20%7C%20read%20the%20list%20with%20%60keep_default_na%3DFalse%60%3B%20split%20No%20answer%20from%20Invalid%20value%20by%20the%20code%20%7C%0A%20%20%20%20%7C%20recode%20by%20rules%20with%20an%20order%20%7C%20%60np.select(conditions%2C%20choices%2C%20default%3D...)%60%20%7C%20conditions%20plain%20True%2FFalse%20(%60.eq(1).fillna(False)%60)%3B%20the%20first%20true%20condition%20wins%20%7C%0A%20%20%20%20%7C%20recode%20ranges%20%7C%20%60pd.cut(...%2C%20bins%3D%2C%20right%3DFalse%2C%20labels%3D)%60%20%7C%20the%20edges'%20side%3B%20values%20outside%20every%20range%3B%20the%20categories'%20order%20%7C%0A%20%20%20%20%7C%20keep%20the%20largest%20few%20%7C%20rank%2C%20a%20written%20tie%20rule%2C%20%60.where(...%2C%20%22Other%22)%60%20%7C%20which%20codes%20may%20rank%3B%20invalid%20codes%20kept%20visible%20%7C%0A%20%20%20%20%7C%20count%20units%20%7C%20%60drop_duplicates(key)%60%2C%20then%20%60size()%60%3B%20polars%20%60pl.struct(key).n_unique()%60%20%7C%20the%20region%20is%20part%20of%20every%20key%3B%20a%20total%20is%20counted%2C%20not%20summed%20%7C%0A%20%20%20%20%7C%20build%20the%20tabulation%20frame%20%7C%20filter%20to%20the%20unit%2C%20%60merge(...%2C%20validate%3D%22many_to_one%22)%60%20to%20the%20design%20columns%20%7C%20one%20row%20per%20unit%3B%20every%20unit%20with%20a%20weight%20%7C%0A%20%20%20%20%7C%20publish%20an%20unweighted%20table%20%7C%20%60pd.crosstab(...%2C%20dropna%3DFalse%2C%20margins%3DTrue%2C%20margins_name%3D%22Total%22)%60%20%7C%20every%20category%2C%20in%20order%20%7C%0A%0A%20%20%20%20Next%2C%20module%206%20(%6050_design_estimates.py%60)%20gives%20this%20frame%20to%20svy%3A%20totals%2C%20means%20and%20proportions%20with%20standard%20errors%2C%20CVs%20and%20confidence%20intervals%2C%20by%20domain%2C%20and%20the%20weighted%20tables%20that%20sit%20beside%20this%20module's%20unweighted%20ones.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%225.3%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
3185e3483be679817d118d52b3b12655