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%22Primer%3A%20from%20R%20to%20Python%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%20io%0A%20%20%20%20import%20warnings%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20import%20svy%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%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20%23%20The%20survey%20as%20module%202%20leaves%20it%3A%20every%20region's%20rows%20in%20one%20table%2C%20removed%20households%20and%20voided%20rows%20left%20out%0A%20%20%20%20households%20%3D%20course.households()%20%20%23%20one%20row%20per%20household%2C%20with%20its%20stratum%2C%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%0A%20%20%20%20catch%20%3D%20course.child(%22catch%22)%0A%0A%0A%40app.function%0Adef%20show(example)%20-%3E%20mo.Html%3A%0A%20%20%20%20%22%22%22Run%20a%20small%20example%20and%20show%20what%20happened%3A%20every%20warning%20it%20raised%20(the%20code%20ran)%2C%20then%20either%20what%20it%20gave%20back%0A%20%20%20%20or%20the%20error%20that%20stopped%20it.%22%22%22%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20result%2C%20error%20%3D%20example()%2C%20None%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20exc%3A%20%20%23%20the%20error%20is%20the%20lesson%3A%20show%20it%20rather%20than%20stop%20the%20notebook%0A%20%20%20%20%20%20%20%20%20%20%20%20result%2C%20error%20%3D%20None%2C%20exc%0A%20%20%20%20parts%20%3D%20%5B%5D%0A%20%20%20%20for%20warning%20in%20caught%3A%0A%20%20%20%20%20%20%20%20first%20%3D%20str(warning.message).strip().splitlines()%5B0%5D%0A%20%20%20%20%20%20%20%20parts.append(mo.callout(mo.md(f%22**Warning%3A%20%7Bwarning.category.__name__%7D.**%20The%20code%20ran%2C%20but%20the%20library%20says%3A%5Cn%5Cn%60%60%60text%5Cn%7Bfirst%7D%5Cn%60%60%60%22)%2C%20kind%3D%22warn%22))%0A%20%20%20%20if%20error%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20lines%20%3D%20%5Bline%20for%20line%20in%20str(error).strip().splitlines()%20if%20line.strip()%5D%0A%20%20%20%20%20%20%20%20parts.append(mo.callout(mo.md(f%22**Error%3A%20%7Btype(error).__name__%7D.**%20The%20code%20stopped%20here%3A%5Cn%5Cn%60%60%60text%5Cn%7Blines%5B0%5D%20if%20lines%20else%20type(error).__name__%7D%5Cn%60%60%60%22)%2C%20kind%3D%22danger%22))%0A%20%20%20%20elif%20isinstance(result%2C%20(pd.DataFrame%2C%20pd.Series%2C%20pl.DataFrame%2C%20pl.Series))%3A%0A%20%20%20%20%20%20%20%20parts.append(result)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20parts.append(mo.md(f%22It%20gave%20back%3A%20%60%7Bresult!r%7D%60%22))%0A%20%20%20%20return%20mo.vstack(parts)%0A%0A%0A%40app.function%0Adef%20side_by_side(**results)%20-%3E%20mo.Html%3A%0A%20%20%20%20%22%22%22The%20same%20task%20done%20in%20two%20or%20more%20tools%2C%20each%20result%20under%20its%20tool's%20name.%22%22%22%0A%20%20%20%20return%20mo.hstack(%5Bmo.vstack(%5Bmo.md(f%22**%7Bname.replace('_'%2C%20'%20')%7D**%22)%2C%20value%5D)%20for%20name%2C%20value%20in%20results.items()%5D%2C%20justify%3D%22start%22%2C%20gap%3D2%2C%20wrap%3DTrue)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Primer%3A%20from%20R%20to%20Python%0A%0A%20%20%20%20You%20know%20how%20to%20process%20a%20survey%20in%20R%3A%20read%20a%20region's%20tables%20from%20a%20database%2C%20match%20keys%2C%20recode%2C%20tabulate%20and%20estimate%20under%20the%20design.%20This%20primer%20is%20the%20lookup%20table%20from%20that%20work%20to%20Python.%20Each%20R%20verb%20you%20use%20has%20a%20Python%20equivalent%2C%20usually%20two%3A%20one%20in%20**pandas**%2C%20the%20table%20library%20of%20Arrays%20%26%20DataFrames%2C%20and%20one%20in%20**polars**%2C%20the%20faster%20library%20this%20course%20also%20uses.%20Databases%20are%20read%20with%20**DuckDB**%2C%20and%20survey%20estimates%20come%20from%20**svy**.%0A%0A%20%20%20%20One%20idea%20runs%20through%20the%20primer%2C%20and%20through%20module%206%3A%20**a%20library%20is%20a%20shortcut%2C%20not%20a%20requirement.**%20If%20you%20understand%20what%20an%20R%20function%20computes%2C%20you%20can%20write%20it%20in%20Python%20with%20general%20tools%2C%20whether%20or%20not%20a%20Python%20library%20already%20does%20it.%20Where%20an%20R%20survey%20function%20has%20an%20svy%20equivalent%2C%20the%20primer%20shows%20the%20svy%20call%20and%2C%20beside%20it%2C%20the%20same%20estimate%20written%20with%20pandas.%20Module%206%20does%20the%20same%20for%20every%20ReGenesees%20call%20the%20course%20uses%2C%20including%20the%20ones%20no%20Python%20library%20offers.%0A%0A%20%20%20%20%7C%20part%20%7C%20what%20it%20covers%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20The%20verbs%2C%20one%20table%20%7C%20every%20R%20verb%20of%20survey%20processing%20beside%20its%20Python%20equivalents%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Rows%2C%20columns%20and%20groups%20%7C%20%60filter%60%2C%20%60select%60%2C%20%60mutate%60%2C%20%60arrange%60%2C%20%60summarise%60%2C%20%60count%60%2C%20%60n_distinct%60%2C%20run%20on%20the%20survey%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Keys%20and%20joins%20%7C%20keys%20pasted%20from%20parts%2C%20%60inner_join%60%2C%20%60anti_join%60%2C%20%60semi_join%60%2C%20%60full_join%60%2C%20%60bind_rows%60%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20Recodes%20and%20reshaping%20%7C%20%60case_when%60%2C%20%60if_else%60%2C%20%60pivot_longer%60%2C%20%60pivot_wider%60%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20Databases%3A%20DBI%20and%20RMySQL%20against%20DuckDB%20%7C%20one%20regional%20database%2C%20a%20lazy%20table%2C%20the%20loop%20over%20regions%20%7C%0A%20%20%20%20%7C%206%20%C2%B7%20Survey%20estimates%3A%20survey%20and%20ReGenesees%20against%20svy%2C%20and%20against%20your%20own%20code%20%7C%20the%20design%2C%20totals%2C%20means%2C%20shares%20and%20ratios%20by%20domain%2C%20and%20the%20sample's%20own%20counts%20%7C%0A%20%20%20%20%7C%207%20%C2%B7%20Files%3A%20RDS%20against%20Parquet%20%7C%20saving%20a%20table%20so%20that%20it%20comes%20back%20exactly%20%7C%0A%20%20%20%20%7C%208%20%C2%B7%20Reports%3A%20Quarto%20against%20marimo%20%7C%20what%20changes%20when%20the%20report%20is%20a%20marimo%20notebook%20%7C%0A%20%20%20%20%7C%209%20%C2%B7%20Habits%20that%20differ%20%7C%20counting%20from%200%2C%20copy-on-write%2C%20method%20chains%20for%20pipes%2C%20missing%20values%2C%20factors%20against%20categoricals%20%7C%0A%0A%20%20%20%20Come%20back%20to%20any%20part%20at%20any%20time.%20The%20three%20exercises%20(P1.1%20to%20P1.3)%20work%20as%20every%20exercise%20of%20the%20course%20does%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20read%20the%20check.%20A%20**warning**%20box%20under%20an%20example%20means%20the%20code%20ran%20but%20the%20library%20has%20something%20to%20say%3B%20an%20**error**%20box%20means%20it%20stopped.%20Read%20either%20from%20its%20first%20line%3A%20it%20names%20what%20happened.%0A%0A%20%20%20%20**The%20tables.**%20The%20setup%20cell%20at%20the%20top%20reads%20the%20Survey%20of%20Fishing%20Households%202025%20as%20module%202%20leaves%20it%3A%20%60households%60%20(one%20row%20per%20household%2C%20with%20its%20stratum%2C%20area%20and%20weights)%2C%20%60members%60%2C%20%60units%60%20(the%20fishing%20units)%20and%20%60catch%60.%20The%20area%20codes%20%60region_code%60%20and%20%60province_code%60%20are%20text%2C%20zero-padded%20(%60%2201%22%60%2C%20%60%22033%22%60)%2C%20as%20module%202%20teaches.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20The%20verbs%2C%20one%20table%0A%0A%20%20%20%20Each%20row%20is%20one%20task%20of%20survey%20processing.%20The%20**R**%20column%20gives%20the%20tidyverse%20way%20first%20and%2C%20where%20it%20differs%2C%20the%20data.table%20way%2C%20or%20the%20DBI%2C%20survey%20or%20ReGenesees%20call%3B%20the%20**Python**%20column%20gives%20pandas%20and%20polars%2C%20or%20DuckDB%2C%20or%20svy%20and%20your%20own%20code.%20The%20last%20column%20says%20which%20part%20of%20this%20primer%20runs%20the%20row%20on%20the%20survey.%20%60hh%60%20stands%20for%20the%20households%20table%2C%20%60m%60%20for%20the%20members%2C%20%60units%60%20for%20the%20fishing%20units%2C%20%60key%60%20for%20the%20list%20of%20a%20table's%20key%20columns.%0A%0A%20%20%20%20%7C%20%23%20%7C%20task%20%7C%20R%20%7C%20Python%20%7C%20runs%20in%20%7C%0A%20%20%20%20%7C--%3A%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%7C%20**Rows%2C%20columns%20and%20groups**%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%201%20%7C%20keep%20rows%20%7C%20dplyr%3A%20%60filter(hh%2C%20region_code%20%3D%3D%20%2201%22)%60%3Cbr%3Edata.table%3A%20%60hh%5Bregion_code%20%3D%3D%20%2201%22%5D%60%20%7C%20pandas%3A%20%60hh%5Bhh%5B%22region_code%22%5D%20%3D%3D%20%2201%22%5D%60%3Cbr%3Epolars%3A%20%60hh.filter(pl.col(%22region_code%22)%20%3D%3D%20%2201%22)%60%20%7C%202%20%7C%0A%20%20%20%20%7C%202%20%7C%20keep%20columns%20%7C%20dplyr%3A%20%60select(hh%2C%20region_code%2C%20sel_final_weight)%60%3Cbr%3Edata.table%3A%20%60hh%5B%2C%20.(region_code%2C%20sel_final_weight)%5D%60%20%7C%20pandas%3A%20%60hh%5B%5B%22region_code%22%2C%20%22sel_final_weight%22%5D%5D%60%3Cbr%3Epolars%3A%20%60hh.select(%22region_code%22%2C%20%22sel_final_weight%22)%60%20%7C%202%20%7C%0A%20%20%20%20%7C%203%20%7C%20a%20new%20column%20%7C%20dplyr%3A%20%60mutate(hh%2C%20w_members%20%3D%20sel_final_weight%20*%20hh_roster_members)%60%3Cbr%3Edata.table%3A%20%60hh%5B%2C%20w_members%20%3A%3D%20sel_final_weight%20*%20hh_roster_members%5D%60%20%7C%20pandas%3A%20%60hh.assign(w_members%3Dhh%5B%22sel_final_weight%22%5D%20*%20hh%5B%22hh_roster_members%22%5D)%60%3Cbr%3Epolars%3A%20%60hh.with_columns(w_members%3Dpl.col(%22sel_final_weight%22)%20*%20pl.col(%22hh_roster_members%22))%60%20%7C%202%20%7C%0A%20%20%20%20%7C%204%20%7C%20rename%20%7C%20dplyr%3A%20%60rename(hh%2C%20weight%20%3D%20sel_final_weight)%60%3Cbr%3Edata.table%3A%20%60setnames(hh%2C%20%22sel_final_weight%22%2C%20%22weight%22)%60%20%7C%20pandas%3A%20%60hh.rename(columns%3D%7B%22sel_final_weight%22%3A%20%22weight%22%7D)%60%3Cbr%3Epolars%3A%20%60hh.rename(%7B%22sel_final_weight%22%3A%20%22weight%22%7D)%60%20%7C%202%20%7C%0A%20%20%20%20%7C%205%20%7C%20sort%20%7C%20dplyr%3A%20%60arrange(hh%2C%20desc(sel_final_weight))%60%3Cbr%3Edata.table%3A%20%60hh%5Border(-sel_final_weight)%5D%60%20%7C%20pandas%3A%20%60hh.sort_values(%22sel_final_weight%22%2C%20ascending%3DFalse)%60%3Cbr%3Epolars%3A%20%60hh.sort(%22sel_final_weight%22%2C%20descending%3DTrue)%60%20%7C%202%20%7C%0A%20%20%20%20%7C%206%20%7C%20one%20row%20per%20group%20%7C%20dplyr%3A%20%60hh%20%7C%3E%20group_by(region_code)%20%7C%3E%20summarise(households%20%3D%20n()%2C%20members%20%3D%20sum(hh_roster_members))%60%3Cbr%3Edata.table%3A%20%60hh%5B%2C%20.(households%20%3D%20.N%2C%20members%20%3D%20sum(hh_roster_members))%2C%20by%20%3D%20region_code%5D%60%20%7C%20pandas%3A%20%60hh.groupby(%22region_code%22).agg(households%3D(%22hh_id%22%2C%20%22size%22)%2C%20members%3D(%22hh_roster_members%22%2C%20%22sum%22))%60%3Cbr%3Epolars%3A%20%60hh.group_by(%22region_code%22).agg(households%3Dpl.len()%2C%20members%3Dpl.col(%22hh_roster_members%22).sum())%60%20%7C%202%20%7C%0A%20%20%20%20%7C%207%20%7C%20count%20rows%20by%20group%20%7C%20dplyr%3A%20%60count(hh%2C%20region_code%2C%20sel_stratum)%60%3Cbr%3Edata.table%3A%20%60hh%5B%2C%20.N%2C%20by%20%3D%20.(region_code%2C%20sel_stratum)%5D%60%20%7C%20pandas%3A%20%60hh.value_counts(%5B%22region_code%22%2C%20%22sel_stratum%22%5D)%60%3Cbr%3Epolars%3A%20%60hh.group_by(%22region_code%22%2C%20%22sel_stratum%22).len()%60%20%7C%202%20%7C%0A%20%20%20%20%7C%208%20%7C%20count%20distinct%20values%20%7C%20dplyr%3A%20%60summarise(areas%20%3D%20n_distinct(sel_ea))%60%3Cbr%3Edata.table%3A%20%60uniqueN(sel_ea)%60%20%7C%20pandas%3A%20%60.agg(areas%3D(%22sel_ea%22%2C%20%22nunique%22))%60%3Cbr%3Epolars%3A%20%60pl.col(%22sel_ea%22).n_unique()%60%20%7C%202%20%7C%0A%20%20%20%20%7C%209%20%7C%20distinct%20rows%20%7C%20dplyr%3A%20%60distinct(hh%2C%20region_code%2C%20province_code)%60%3Cbr%3Edata.table%3A%20%60unique(hh%2C%20by%20%3D%20c(%22region_code%22%2C%20%22province_code%22))%60%20%7C%20pandas%3A%20%60hh%5B%5B%22region_code%22%2C%20%22province_code%22%5D%5D.drop_duplicates()%60%3Cbr%3Epolars%3A%20%60hh.select(%22region_code%22%2C%20%22province_code%22).unique()%60%20%7C%202%20%7C%0A%20%20%20%20%7C%20%7C%20**Keys%20and%20joins**%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%2010%20%7C%20a%20key%20pasted%20from%20parts%20%7C%20%60paste0(region_code%2C%20%22-%22%2C%20hh_id)%60%20%7C%20pandas%3A%20%60hh%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20hh%5B%22hh_id%22%5D.astype(%22string%22)%60%3Cbr%3Epolars%3A%20%60pl.concat_str(%22region_code%22%2C%20pl.col(%22hh_id%22).cast(pl.String)%2C%20separator%3D%22-%22)%60%20%7C%203%20%7C%0A%20%20%20%20%7C%2011%20%7C%20inner%20or%20left%20join%20%7C%20dplyr%3A%20%60inner_join(m%2C%20hh%2C%20by%20%3D%20c(%22region_code%22%2C%20%22hh_id%22))%60%3Cbr%3Edata.table%3A%20%60merge(m%2C%20hh%2C%20by%20%3D%20c(%22region_code%22%2C%20%22hh_id%22))%60%20%7C%20pandas%3A%20%60m.merge(hh%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20how%3D%22inner%22%2C%20validate%3D%22many_to_one%22)%60%3Cbr%3Epolars%3A%20%60m.join(hh%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20how%3D%22inner%22)%60%20%7C%203%20%7C%0A%20%20%20%20%7C%2012%20%7C%20rows%20with%20no%20match%2C%20rows%20with%20a%20match%20%7C%20dplyr%3A%20%60anti_join(units%2C%20catch%2C%20by%20%3D%20key)%60%2C%20%60semi_join(...)%60%3Cbr%3Edata.table%3A%20%60units%5B!catch%2C%20on%20%3D%20key%5D%60%20%7C%20pandas%3A%20%60units.merge(catch%5Bkey%5D.drop_duplicates()%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%60%2C%20then%20keep%20%60%22left_only%22%60%20or%20%60%22both%22%60%3Cbr%3Epolars%3A%20%60units.join(catch%2C%20on%3Dkey%2C%20how%3D%22anti%22)%60%2C%20%60how%3D%22semi%22%60%20%7C%203%20%7C%0A%20%20%20%20%7C%2013%20%7C%20every%20row%20of%20both%2C%20with%20where%20it%20came%20from%20%7C%20dplyr%3A%20%60full_join(units%2C%20catch%2C%20by%20%3D%20key)%60%2C%20after%20a%20marker%20column%20added%20to%20each%20side%3Cbr%3Edata.table%3A%20%60merge(units%2C%20catch%2C%20by%20%3D%20key%2C%20all%20%3D%20TRUE)%60%20%7C%20pandas%3A%20%60units.merge(catch%2C%20on%3Dkey%2C%20how%3D%22outer%22%2C%20indicator%3DTrue)%60%3A%20%60_merge%60%20is%20%60left_only%60%2C%20%60right_only%60%20or%20%60both%60%3Cbr%3Epolars%3A%20%60units.join(catch%2C%20on%3Dkey%2C%20how%3D%22full%22%2C%20coalesce%3DTrue)%60%20%7C%203%20%7C%0A%20%20%20%20%7C%2014%20%7C%20stack%20tables%20%7C%20dplyr%3A%20%60bind_rows(r01%2C%20r04%2C%20r07)%60%3Cbr%3Edata.table%3A%20%60rbindlist(list(r01%2C%20r04%2C%20r07)%2C%20fill%20%3D%20TRUE)%60%20%7C%20pandas%3A%20%60pd.concat(%5Br01%2C%20r04%2C%20r07%5D%2C%20ignore_index%3DTrue)%60%3Cbr%3Epolars%3A%20%60pl.concat(%5Br01%2C%20r04%2C%20r07%5D%2C%20how%3D%22diagonal%22)%60%20%7C%203%20%7C%0A%20%20%20%20%7C%20%7C%20**Recodes%20and%20reshaping**%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%2015%20%7C%20recode%20with%20conditions%20%7C%20dplyr%3A%20%60case_when(is.na(mem_sex)%20~%20%22No%20answer%22%2C%20mem_sex%20%3D%3D%201%20~%20%22Male%22%2C%20mem_sex%20%3D%3D%202%20~%20%22Female%22%2C%20.default%20%3D%20%22Invalid%20value%22)%60%3Cbr%3Edata.table%3A%20%60fcase(...)%60%20%7C%20pandas%3A%20%60np.select(%5Bconditions%5D%2C%20%5Blabels%5D%2C%20default%3D%22Invalid%20value%22)%60%3Cbr%3Epolars%3A%20%60pl.when(...).then(...).when(...).then(...).otherwise(...)%60%20%7C%204%20%7C%0A%20%20%20%20%7C%2016%20%7C%20one%20condition%2C%20two%20values%20%7C%20dplyr%3A%20%60if_else(sel_stratum%20%3D%3D%20%22F%22%2C%20%22listed%22%2C%20%22area%22)%60%3Cbr%3Edata.table%3A%20%60fifelse(...)%60%20%7C%20pandas%3A%20%60np.where(hh%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22%2C%20%22listed%22%2C%20%22area%22)%60%3Cbr%3Epolars%3A%20%60pl.when(pl.col(%22sel_stratum%22)%20%3D%3D%20%22F%22).then(pl.lit(%22listed%22)).otherwise(pl.lit(%22area%22))%60%20%7C%204%20%7C%0A%20%20%20%20%7C%2017%20%7C%20long%20to%20wide%2C%20wide%20to%20long%20%7C%20tidyr%3A%20%60pivot_wider(...)%60%2C%20%60pivot_longer(...)%60%3Cbr%3Edata.table%3A%20%60dcast(...)%60%2C%20%60melt(...)%60%20%7C%20pandas%3A%20%60pivot_table(...)%60%2C%20%60melt(...)%60%3Cbr%3Epolars%3A%20%60pivot(...)%60%2C%20%60unpivot(...)%60%20%7C%204%20%7C%0A%20%20%20%20%7C%20%7C%20**Databases**%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%2018%20%7C%20open%20a%20database%20%7C%20DBI%3A%20%60con%20%3C-%20dbConnect(RMySQL%3A%3AMySQL()%2C%20dbname%20%3D%20%22sfh2025_r01%22%2C%20host%20%3D%20...%2C%20user%20%3D%20...)%60%20%7C%20DuckDB%3A%20%60con%20%3D%20duckdb.connect(%22data%2Fsfh2025_r01.duckdb%22%2C%20read_only%3DTrue)%60%3B%20a%20MySQL%20server%20through%20DuckDB's%20mysql%20extension%20(module%202)%20%7C%205%20%7C%0A%20%20%20%20%7C%2019%20%7C%20a%20query%20into%20a%20table%20%7C%20DBI%3A%20%60dbGetQuery(con%2C%20%22SELECT%20...%22)%60%20%7C%20DuckDB%3A%20%60con.execute(%22SELECT%20...%22).df()%60%20for%20pandas%2C%20%60.pl()%60%20for%20polars%20%7C%205%20%7C%0A%20%20%20%20%7C%2020%20%7C%20a%20lazy%20table%2C%20collected%20at%20the%20end%20%7C%20dbplyr%3A%20%60tbl(con%2C%20%22household%22)%20%7C%3E%20filter(...)%20%7C%3E%20select(...)%20%7C%3E%20collect()%60%20%7C%20DuckDB%3A%20%60con.sql(%22FROM%20household%22).filter(%22...%22).select(%22...%22).df()%60%20%7C%205%20%7C%0A%20%20%20%20%7C%2021%20%7C%20every%20regional%20database%20%7C%20DBI%3A%20a%20%60for%60%20loop%20of%20%60dbConnect%60%2C%20%60dbGetQuery%60%2C%20%60dbDisconnect%60%2C%20then%20%60bind_rows%60%20%7C%20DuckDB%3A%20%60ATTACH%60%20each%20file%2C%20then%20one%20query%20with%20%60UNION%20ALL%20BY%20NAME%60%20%7C%205%20%7C%0A%20%20%20%20%7C%20%7C%20**Survey%20estimates**%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%2022%20%7C%20the%20design%20%7C%20survey%3A%20%60svydesign(ids%20%3D%20~psu_id%2C%20strata%20%3D%20~stratum_id%2C%20weights%20%3D%20~sel_final_weight%2C%20fpc%20%3D%20~fpc%2C%20data%20%3D%20hh)%60%3Cbr%3EReGenesees%3A%20%60e.svydesign(data%20%3D%20hh%2C%20ids%20%3D%20~psu_id%2C%20strata%20%3D%20~stratum_id%2C%20weights%20%3D%20~sel_final_weight%2C%20fpc%20%3D%20~fpc)%60%20%7C%20svy%3A%20%60svy.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)%60%2C%20then%20%60svy.Sample(data%3Dpl.from_pandas(hh)%2C%20design%3Ddesign)%60%3Cbr%3Eyour%20own%3A%20the%20same%20four%20columns%2C%20read%20by%20your%20code%20%7C%206%20%7C%0A%20%20%20%20%7C%2023%20%7C%20a%20total%2C%20a%20mean%20%7C%20survey%3A%20%60svytotal(~y%2C%20des)%60%2C%20%60svymean(~y%2C%20des)%60%3Cbr%3EReGenesees%3A%20%60svystatTM(des%2C%20~y%2C%20estimator%20%3D%20%22Total%22)%60%2C%20%60estimator%20%3D%20%22Mean%22%60%20%7C%20svy%3A%20%60sample.estimation.total(y%3D%22y%22)%60%2C%20%60.mean(y%3D%22y%22)%60%3Cbr%3Eyour%20own%3A%20%60(w%20*%20y).sum()%60%2C%20%60(w%20*%20y).sum()%20%2F%20w.sum()%60%20%7C%206%20%7C%0A%20%20%20%20%7C%2024%20%7C%20by%20domain%20%7C%20survey%3A%20%60svyby(~y%2C%20~region_code%2C%20des%2C%20svytotal)%60%3Cbr%3EReGenesees%3A%20%60svystatTM(des%2C%20~y%2C%20by%20%3D%20~region_code)%60%20%7C%20svy%3A%20%60total(y%3D%22y%22%2C%20by%3D%22region_code%22)%60%3Cbr%3Eyour%20own%3A%20the%20same%20sums%20with%20%60groupby(%22region_code%22)%60%20(exercise%20P1.2)%20%7C%206%20%7C%0A%20%20%20%20%7C%2025%20%7C%20shares%20of%20a%20category%20%7C%20survey%3A%20%60svymean(~factor(sel_stratum)%2C%20des)%60%3Cbr%3EReGenesees%3A%20%60svystatTM(des%2C%20~sel_stratum%2C%20estimator%20%3D%20%22Mean%22)%60%2C%20with%20%60sel_stratum%60%20a%20factor%20%7C%20svy%3A%20%60prop(y%3D%22sel_stratum%22)%60%3Cbr%3Eyour%20own%3A%20%60w.groupby(x).sum()%20%2F%20w.sum()%60%20%7C%206%20%7C%0A%20%20%20%20%7C%2026%20%7C%20a%20ratio%20%7C%20survey%3A%20%60svyratio(~y%2C%20~x%2C%20des)%60%3Cbr%3EReGenesees%3A%20%60svystatR(des%2C%20~y%2C%20~x)%60%20%7C%20svy%3A%20%60ratio(y%3D%22y%22%2C%20x%3D%22x%22)%60%3Cbr%3Eyour%20own%3A%20%60(w%20*%20y).sum()%20%2F%20(w%20*%20x).sum()%60%20%7C%206%20%7C%0A%20%20%20%20%7C%2027%20%7C%20SE%2C%20CV%20and%20confidence%20interval%20%7C%20survey%3A%20%60SE()%60%2C%20%60cv()%60%2C%20%60confint()%60%3Cbr%3EReGenesees%3A%20%60vartype%20%3D%20c(%22se%22%2C%20%22cv%22)%60%2C%20%60conf.int%20%3D%20TRUE%60%20%7C%20svy%3A%20the%20columns%20%60se%60%2C%20%60cv%60%2C%20%60lci%60%2C%20%60uci%60%20of%20%60.to_polars()%60%3Cbr%3Eyour%20own%3A%20the%20variance%20in%20a%20few%20lines%2C%20primer%20%5BP2%5D(%2F%3Ffile%3D96_primer_survey_sampling.py)%20%7C%206%20and%20P2%20%7C%0A%20%20%20%20%7C%2028%20%7C%20the%20sample's%20own%20counts%20and%20sums%20%7C%20ReGenesees%3A%20a%20design%20whose%20every%20weight%20is%201%2C%20%60e.svydesign(data%20%3D%20hh%2C%20ids%20%3D%20~w_one%2C%20weights%20%3D%20~w_one)%60%2C%20then%20%60svystatTM%60%20%7C%20pandas%3A%20%60groupby(...).agg(households%3D(%22hh_id%22%2C%20%22size%22)%2C%20members%3D(%22hh_roster_members%22%2C%20%22sum%22))%60%3Cbr%3Eyour%20own%3A%20exercise%20P1.2%20with%20the%20weight%20%60w_one%60%20%7C%206%20%7C%0A%20%20%20%20%7C%2029%20%7C%20a%20subpopulation%20%7C%20survey%3A%20%60svytotal(~y%2C%20subset(des%2C%20region_code%20%3D%3D%20%2201%22))%60%20%7C%20svy%3A%20%60total(y%3D%22y%22%2C%20where%3Dpl.col(%22region_code%22)%20%3D%3D%20%2201%22)%60%20%7C%206%20%7C%0A%20%20%20%20%7C%2030%20%7C%20strata%20with%20one%20PSU%20%7C%20survey%3A%20%60options(survey.lonely.psu%20%3D%20%22remove%22)%60%3Cbr%3EReGenesees%3A%20%60options(RG.lonely.psu%20%3D%20%22remove%22)%60%2C%20or%20%60collapse.strata(des)%60%20%7C%20svy%3A%20%60svy.Design(...%2C%20singleton%3D%22skip%22)%60%2C%20or%20%60singleton%3Dsvy.Singleton(%22collapse%22)%60%20%7C%20P2%20%7C%0A%20%20%20%20%7C%2031%20%7C%20logit%20calibration%20with%20bounds%3B%20weights%20equal%20within%20a%20cluster%20%7C%20ReGenesees%3A%20%60e.calibrate(...%2C%20calfun%20%3D%20%22logit%22%2C%20bounds%20%3D%20...%2C%20aggregate.stage%20%3D%20...)%60%20%7C%20no%20Python%20library%20found%20(November%202026)%3B%20module%206%20writes%20each%20as%20a%20short%20function%20of%20its%20own%20(%5Bsection%204%5D(%2F%3Ffile%3D50_design_estimates.py))%20%7C%20module%206%20%7C%0A%20%20%20%20%7C%20%7C%20**Files%20and%20reports**%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%2032%20%7C%20save%20a%20table%2C%20read%20it%20back%20%7C%20%60saveRDS(hh%2C%20%22hh.rds%22)%60%2C%20%60readRDS(%22hh.rds%22)%60%20%7C%20pandas%3A%20%60hh.to_parquet(%22hh.parquet%22)%60%2C%20%60pd.read_parquet(%22hh.parquet%22)%60%3Cbr%3Epolars%3A%20%60pl.read_parquet(...)%60%3B%20DuckDB%3A%20%60FROM%20'hh.parquet'%60%20%7C%207%20%7C%0A%20%20%20%20%7C%2033%20%7C%20tables%20to%20a%20workbook%20%7C%20writexl%3A%20%60write_xlsx(list(by_region%20%3D%20t1%2C%20by_province%20%3D%20t2)%2C%20%22tables.xlsx%22)%60%20%7C%20pandas%3A%20%60pd.ExcelWriter(%22tables.xlsx%22)%60%20with%20one%20%60to_excel(...%2C%20sheet_name%3D...)%60%20per%20table%3Cbr%3Epolars%3A%20%60t1.write_excel(...)%60%20%7C%207%2C%20module%207%20%7C%0A%20%20%20%20%7C%2034%20%7C%20a%20report%20that%20runs%20its%20own%20code%20%7C%20Quarto%3A%20a%20%60.qmd%60%20file%2C%20rendered%20%7C%20marimo%3A%20a%20%60.py%60%20notebook%2C%20exported%20%7C%208%20%7C%0A%0A%20%20%20%20The%20pipe%20%60%7C%3E%60%20appears%20in%20the%20table%20as%20written%20in%20R.%20In%20Python%20the%20same%20chain%20is%20written%20with%20dots%2C%20one%20method%20after%20another%20(part%209).%20The%20R%20calls%20were%20read%20against%20the%20survey%20package's%20reference%20manual%20(version%204.4-2)%20and%20ReGenesees'%20help%20pages%20(version%202.5)%20in%20November%202026.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20Rows%2C%20columns%20and%20groups%0A%0A%20%20%20%20**The%20problem.**%20The%20most%20common%20R%20lines%20in%20a%20processing%20script%20are%20one-verb%20steps%3A%20keep%20these%20rows%2C%20these%20columns%2C%20add%20a%20column%2C%20sort%2C%20summarise%20by%20group.%20You%20need%20their%20Python%20forms%20at%20your%20fingertips%2C%20in%20both%20libraries.%0A%0A%20%20%20%20**The%20tool.**%20In%20pandas%2C%20rows%20are%20chosen%20with%20a%20**mask**%20(a%20column%20of%20%60True%60%20and%20%60False%60)%20in%20square%20brackets%2C%20columns%20with%20a%20list%20of%20names%20in%20square%20brackets%2C%20and%20a%20new%20column%20with%20%60assign%60.%20In%20polars%2C%20every%20step%20is%20a%20method%20that%20takes%20**expressions**%2C%20%60pl.col(%22name%22)%60%20standing%20for%20a%20column%2C%20as%20dplyr's%20verbs%20take%20bare%20column%20names.%20Neither%20library%20changes%20the%20table%20you%20start%20from%3A%20each%20step%20gives%20back%20a%20new%20one%2C%20as%20dplyr%20does.%0A%0A%20%20%20%20Rows%201%20to%205%20of%20the%20table%2C%20as%20one%20chain%3A%20region%2001's%20households%20outside%20stratum%20F%20(the%20registered%20large%20units%2C%20every%20one%20of%20them%20taken)%2C%20four%20columns%2C%20a%20new%20column%2C%20the%20largest%20weights%20first.%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%20hh_pl%20%3D%20pl.from_pandas(households)%20%20%23%20the%20same%20households%20as%20a%20polars%20table%0A%0A%20%20%20%20by_pandas%20%3D%20(%0A%20%20%20%20%20%20%20%20households%5B(households%5B%22region_code%22%5D%20%3D%3D%20%2201%22)%20%26%20(households%5B%22sel_stratum%22%5D%20!%3D%20%22F%22)%5D%20%20%23%20filter%0A%20%20%20%20%20%20%20%20%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22sel_final_weight%22%2C%20%22hh_roster_members%22%5D%5D%20%20%23%20select%0A%20%20%20%20%20%20%20%20.assign(w_members%3Dlambda%20t%3A%20t%5B%22sel_final_weight%22%5D%20*%20t%5B%22hh_roster_members%22%5D)%20%20%23%20mutate%0A%20%20%20%20%20%20%20%20.rename(columns%3D%7B%22sel_final_weight%22%3A%20%22weight%22%7D)%20%20%23%20rename%0A%20%20%20%20%20%20%20%20.sort_values(%22weight%22%2C%20ascending%3DFalse)%20%20%23%20arrange(desc(...))%0A%20%20%20%20%20%20%20%20.head(5)%0A%20%20%20%20)%0A%20%20%20%20by_polars%20%3D%20(%0A%20%20%20%20%20%20%20%20hh_pl.filter((pl.col(%22region_code%22)%20%3D%3D%20%2201%22)%20%26%20(pl.col(%22sel_stratum%22)%20!%3D%20%22F%22))%0A%20%20%20%20%20%20%20%20.select(%22region_code%22%2C%20%22hh_id%22%2C%20%22sel_final_weight%22%2C%20%22hh_roster_members%22)%0A%20%20%20%20%20%20%20%20.with_columns(w_members%3Dpl.col(%22sel_final_weight%22)%20*%20pl.col(%22hh_roster_members%22))%0A%20%20%20%20%20%20%20%20.rename(%7B%22sel_final_weight%22%3A%20%22weight%22%7D)%0A%20%20%20%20%20%20%20%20.sort(%22weight%22%2C%20descending%3DTrue)%0A%20%20%20%20%20%20%20%20.head(5)%0A%20%20%20%20)%0A%20%20%20%20side_by_side(pandas%3Dby_pandas%2C%20polars%3Dby_polars)%0A%20%20%20%20return%20(hh_pl%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%20Two%20things%20differ%20from%20R%20in%20that%20cell.%20The%20two%20conditions%20are%20joined%20with%20%60%26%60%20and%20**each%20is%20in%20brackets**%3A%20part%209%20shows%20what%20happens%20without%20them.%20And%20pandas'%20%60assign%60%20takes%20a%20%60lambda%20t%3A%60%20so%20that%20the%20new%20column%20is%20computed%20from%20the%20table%20at%20that%20point%20of%20the%20chain%2C%20the%20role%20dplyr's%20bare%20names%20play%20in%20%60mutate%60.%0A%0A%20%20%20%20Rows%206%20to%208%3A%20one%20row%20per%20group.%20R's%20%60n()%60%20is%20the%20number%20of%20rows%3B%20pandas%20calls%20it%20%60%22size%22%60%2C%20polars%20%60pl.len()%60.%20R's%20%60n_distinct%60%20is%20pandas'%20%60%22nunique%22%60%20and%20polars'%20%60n_unique()%60.%20pandas%20puts%20the%20group%20keys%20in%20the%20**index**%2C%20the%20row%20labels%20to%20the%20left%20of%20the%20table%3B%20%60as_index%3DFalse%60%20keeps%20them%20as%20ordinary%20columns%2C%20as%20a%20tibble%20has%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hh_pl)%3A%0A%20%20%20%20summary_pandas%20%3D%20households.groupby(%5B%22region_code%22%2C%20%22sel_stratum%22%5D%2C%20as_index%3DFalse).agg(%0A%20%20%20%20%20%20%20%20households%3D(%22hh_id%22%2C%20%22size%22)%2C%20%20%23%20n()%0A%20%20%20%20%20%20%20%20areas%3D(%22sel_ea%22%2C%20%22nunique%22)%2C%20%20%23%20n_distinct(sel_ea)%2C%20blanks%20left%20out%0A%20%20%20%20%20%20%20%20members%3D(%22hh_roster_members%22%2C%20%22sum%22)%2C%20%20%23%20sum(...)%0A%20%20%20%20)%0A%20%20%20%20summary_polars%20%3D%20hh_pl.group_by(%22region_code%22%2C%20%22sel_stratum%22).agg(%0A%20%20%20%20%20%20%20%20households%3Dpl.len()%2C%0A%20%20%20%20%20%20%20%20areas%3Dpl.col(%22sel_ea%22).drop_nulls().n_unique()%2C%20%20%23%20polars%20counts%20a%20null%20as%20a%20value%20unless%20it%20is%20dropped%0A%20%20%20%20%20%20%20%20members%3Dpl.col(%22hh_roster_members%22).sum()%2C%0A%20%20%20%20).sort(%22region_code%22%2C%20%22sel_stratum%22)%20%20%23%20polars%20keeps%20no%20order%20for%20groups%20unless%20asked%0A%20%20%20%20side_by_side(pandas%3Dsummary_pandas%2C%20polars%3Dsummary_polars)%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%20shows%200%20areas%3A%20its%20households%20were%20taken%20from%20the%20register%20of%20large%20units%2C%20not%20drawn%20by%20area%2C%20so%20their%20%60sel_ea%60%20is%20blank%20and%20each%20household%20is%20its%20own%20primary%20sampling%20unit%20(primer%20%5BP2%5D(%2F%3Ffile%3D96_primer_survey_sampling.py)%20explains%20why).%20pandas'%20%60nunique%60%20leaves%20blanks%20out%2C%20as%20%60n_distinct(x%2C%20na.rm%20%3D%20TRUE)%60%20does%3B%20polars'%20%60n_unique%60%20counts%20a%20null%20as%20one%20more%20value%20unless%20you%20drop%20it%20first.%20A%20count%20of%20areas%20is%20a%20count%20over%20the%20area%20strata%20A%2C%20B%20and%20C%2C%20which%20is%20the%20first%20step%20of%20the%20exercise.%0A%0A%20%20%20%20%23%23%23%20Exercise%20P1.1.%20Areas%20and%20households%20by%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20fieldwork%20report%20starts%20with%20how%20many%20areas%20were%20visited%20and%20how%20many%20households%20were%20interviewed%20in%20each%20province.%20In%20R%20you%20write%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20households%20%7C%3E%0A%20%20%20%20%20%20filter(sel_stratum%20!%3D%20%22F%22)%20%7C%3E%0A%20%20%20%20%20%20group_by(region_code%2C%20province_code)%20%7C%3E%0A%20%20%20%20%20%20summarise(households%20%3D%20n()%2C%20areas%20%3D%20n_distinct(sel_ea)%2C%20members%20%3D%20sum(hh_roster_members)%2C%20.groups%20%3D%20%22drop%22)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20and%20this%20exercise%20is%20its%20Python%20version.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20households%20table%20%E2%86%92%20one%20row%20per%20province%20of%20the%20area%20strata%20A%2C%20B%20and%20C%20(a%20province%20is%20named%20by%20its%20region%20and%20province%20codes%20together%2C%20as%20module%201%20sets%20out)%2C%20with%20five%20columns%3A%0A%0A%20%20%20%20%7C%20region_code%20%7C%20province_code%20%7C%20households%20%7C%20areas%20%7C%20members%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%20033%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%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-%20%60households%60%3A%20how%20many%20households%3B%0A%20%20%20%20-%20%60areas%60%3A%20how%20many%20distinct%20areas%20(%60sel_ea%60)%3B%0A%20%20%20%20-%20%60members%60%3A%20the%20sum%20of%20%60hh_roster_members%60.%0A%0A%20%20%20%20Key%20columns%20or%20index%2C%20either%20passes%3A%20the%20check%20reads%20both.%0A%0A%20%20%20%20**Tools%3A**%20a%20mask%20with%20%60!%3D%60%3B%20%60groupby(%5B...%5D%2C%20as_index%3DFalse)%60%20and%20%60.agg()%60%20with%20named%20results%2C%20%60%22size%22%60%2C%20%60%22nunique%22%60%20and%20%60%22sum%22%60%20(above).%20In%20polars%2C%20the%20same%20with%20%60group_by%60%20and%20%60pl.len()%60%2C%20%60n_unique()%60%2C%20%60sum()%60%2C%20and%20%60.to_pandas()%60%20at%20the%20end.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20areas_by_province(households%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20province%20of%20the%20area%20strata%20A%2C%20B%20and%20C%3A%20households%2C%20distinct%20areas%20(sel_ea)%20and%20members.%22%22%22%0A%20%20%20%20%23%20Exercise%20P1.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20households%20outside%20stratum%20F%20(sel_stratum%20is%20not%20%22F%22)%0A%20%20%20%20%23%20Step%202%3A%20group%20them%20by%20region_code%20and%20province_code%2C%20as%20columns%20(as_index%3DFalse)%0A%20%20%20%20%23%20Step%203%3A%20.agg()%20three%20named%20results%3A%20households%20(size)%2C%20areas%20(nunique%20of%20sel_ea)%2C%20members%20(sum)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20P1.1%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%20its%20keys%20as%20columns%2C%20rows%20sorted%20by%20them%2C%20columns%20in%20name%20order%2C%0A%20%20%20%20numbers%20rounded.%20So%20a%20correct%20answer%20passes%20whether%20its%20keys%20came%20back%20as%20the%20index%20or%20as%20columns%2C%20from%20pandas%20or%0A%20%20%20%20from%20polars.%22%22%22%0A%20%20%20%20table%20%3D%20result.to_pandas()%20if%20isinstance(result%2C%20pl.DataFrame)%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%20two_provinces()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Ten%20households%20in%20two%20provinces%20of%20two%20regions%2C%20used%20by%20the%20checks%3A%20households%20of%20stratum%20F%20with%20a%20blank%20area%2C%0A%20%20%20%20an%20area%20with%20three%20households%2C%20and%20area%20numbers%20that%20repeat%20in%20both%20provinces.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20region_code%2C%20province_code%2C%20hh_id%2C%20sel_stratum%2C%20sel_ea%2C%20hh_roster_members%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%205001%2C%20%22F%22%2C%20None%2C%206)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%205002%2C%20%22F%22%2C%20None%2C%203)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%205003%2C%20%22A%22%2C%201002%2C%204)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%205004%2C%20%22A%22%2C%201002%2C%205)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%205005%2C%20%22A%22%2C%201002%2C%202)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%205006%2C%20%22C%22%2C%203017%2C%207)%2C%0A%20%20%20%20%20%20%20%20(%2207%22%2C%20%22022%22%2C%205001%2C%20%22F%22%2C%20None%2C%209)%2C%0A%20%20%20%20%20%20%20%20(%2207%22%2C%20%22022%22%2C%205002%2C%20%22A%22%2C%201002%2C%203)%2C%0A%20%20%20%20%20%20%20%20(%2207%22%2C%20%22022%22%2C%205003%2C%20%22C%22%2C%203017%2C%204)%2C%0A%20%20%20%20%20%20%20%20(%2207%22%2C%20%22022%22%2C%205004%2C%20%22C%22%2C%203044%2C%205)%2C%0A%20%20%20%20%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22region_code%22%2C%20%22province_code%22%2C%20%22hh_id%22%2C%20%22sel_stratum%22%2C%20%22sel_ea%22%2C%20%22hh_roster_members%22%5D)%0A%20%20%20%20return%20table.astype(%7B%22sel_ea%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%20P1.1%22%2C%20areas_by_province%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20households%22%2C%20(households%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table%3A%20two%20provinces%2C%20households%20of%20stratum%20F%20with%20no%20area%2C%20an%20area%20of%20three%20households%2C%20area%20numbers%20in%20both%20provinces%22%2C%20(two_provinces()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20households%22%2C%20(households%5Bhouseholds%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20households%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%5B%22region_code%22%2C%20%22province_code%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Leave%20stratum%20F%20out%2C%20group%20by%20region_code%20and%20province_code%2C%20and%20count%20areas%20with%20nunique.%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%20table%20for%20the%20whole%20survey%2C%20one%20row%20per%20province.%0A%20%20%20%20try_it(areas_by_province%2C%20households)%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%20P1.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%60households%60%20and%20%60members%60%20too%20large%2C%20%60areas%60%20right%20(6%20households%2C%20not%204%2C%20for%20province%20033%20in%20the%20small%20table)%20%7C%20Stratum%20F%20is%20still%20in.%20Its%20households%20have%20no%20area%2C%20so%20%60nunique%60%20skips%20them%2C%20but%20they%20are%20counted%20as%20households%20and%20members.%20%7C%20step%201%3A%20%60households%5B%22sel_stratum%22%5D%20!%3D%20%22F%22%60%20%7C%0A%20%20%20%20%7C%20%60areas%60%20equals%20%60households%60%20%7C%20The%20areas%20are%20counted%20with%20%60%22size%22%60%20or%20%60%22count%22%60%2C%20one%20per%20row.%20%7C%20step%203%3A%20%60(%22sel_ea%22%2C%20%22nunique%22)%60%20%7C%0A%20%20%20%20%7C%20No%20%60region_code%60%20column%2C%20or%20a%20*KeyError*%20about%20it%20%7C%20The%20groups%20use%20%60province_code%60%20alone.%20%7C%20step%202%3A%20%60groupby(%5B%22region_code%22%2C%20%22province_code%22%5D)%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20The%20truth%20value%20of%20a%20Series%20is%20ambiguous*%2C%20or%20*TypeError%3A%20operation%20'rand_'%20not%20supported*%20%7C%20Two%20conditions%20joined%20with%20%60and%60%20(the%20first%20error)%2C%20or%20with%20%60%26%60%20but%20without%20brackets%20(the%20second).%20%7C%20one%20condition%20is%20enough%20here%3B%20for%20two%2C%20%60(a)%20%26%20(b)%60%20(part%209)%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20'SeriesGroupBy'%20object%20has%20no%20attribute%20'n'*%20%7C%20R's%20names%20used%20as%20pandas'%20function%20names.%20%7C%20%60%22size%22%60%2C%20%60%22nunique%22%60%2C%20%60%22sum%22%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20Keys%20and%20joins%0A%0A%20%20%20%20**The%20problem.**%20The%20survey's%20tables%20join%20on%20keys%20of%20several%20parts%2C%20and%20a%20household%20id%20repeats%20across%20regions%3A%20each%20regional%20database%20numbers%20its%20households%20from%20its%20own%20start%2C%20so%20household%205420%20of%20region%2001%20and%20household%205420%20of%20region%2007%20are%20two%20different%20households.%20R%20scripts%20paste%20the%20parts%20into%20one%20key%2C%20or%20join%20on%20all%20of%20them%20at%20once.%0A%0A%20%20%20%20**The%20tool.**%20pandas'%20%60merge%60%20and%20polars'%20%60join%60%20take%20a%20list%20of%20key%20columns%2C%20which%20is%20R's%20%60by%20%3D%20c(...)%60.%20A%20key%20pasted%20from%20parts%20is%20text%20joined%20with%20%60%2B%60%20in%20pandas%20(every%20part%20must%20be%20text%20first)%20and%20%60pl.concat_str%60%20in%20polars.%20Joining%20on%20the%20list%20is%20the%20better%20habit%20in%20both%20languages%3A%20a%20pasted%20key%20loses%20its%20parts'%20types%20and%20needs%20a%20separator%20that%20no%20part%20contains.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hh_pl)%3A%0A%20%20%20%20hh_key_pandas%20%3D%20households%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20households%5B%22hh_id%22%5D.astype(%22string%22)%0A%20%20%20%20hh_key_polars%20%3D%20hh_pl.select(hh_key%3Dpl.concat_str(%22region_code%22%2C%20pl.col(%22hh_id%22).cast(pl.String)%2C%20separator%3D%22-%22))%0A%20%20%20%20_shared%20%3D%20len(set(households.loc%5Bhouseholds%5B%22region_code%22%5D%20%3D%3D%20%2201%22%2C%20%22hh_id%22%5D)%20%26%20set(households.loc%5Bhouseholds%5B%22region_code%22%5D%20%3D%3D%20%2207%22%2C%20%22hh_id%22%5D))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20side_by_side(pandas%3Dhh_key_pandas.head(3).to_frame(%22hh_key%22)%2C%20polars%3Dhh_key_polars.head(3))%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22Household%20ids%20that%20occur%20in%20both%20region%2001%20and%20region%2007%3A%20**%7B_shared%3A%2C%7D**.%20A%20join%20on%20%60hh_id%60%20alone%20would%20pair%20each%20of%20them%20with%20a%20household%20of%20the%20other%20region.%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%20Rows%2011%20to%2013.%20%60validate%3D%22many_to_one%22%60%20is%20dplyr's%20%60relationship%20%3D%20%22many-to-one%22%60%3A%20the%20join%20stops%2C%20with%20a%20%60MergeError%60%2C%20if%20the%20right%20table's%20key%20repeats%2C%20rather%20than%20multiply%20rows.%20%60indicator%3DTrue%60%20adds%20a%20column%20%60_merge%60%20saying%20where%20each%20row%20came%20from%3A%20%60left_only%60%2C%20%60right_only%60%20or%20%60both%60.%20After%20a%20%60full_join%60%20in%20R%20you%20find%20the%20same%20by%20adding%20a%20marker%20column%20to%20each%20table%20first%20and%20seeing%20which%20marker%20is%20missing.%20Module%203%20uses%20%60_merge%60%20to%20find%20and%20diagnose%20mismatched%20keys.%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%20key%20%3D%20course.UNIT_KEY%20%20%23%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no%0A%20%20%20%20members_with_design%20%3D%20members.merge(households%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22sel_stratum%22%2C%20%22sel_final_weight%22%5D%5D%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20how%3D%22inner%22%2C%20validate%3D%22many_to_one%22)%0A%0A%20%20%20%20both%20%3D%20units%5Bkey%5D.merge(catch%5Bkey%5D.drop_duplicates()%2C%20on%3Dkey%2C%20how%3D%22outer%22%2C%20indicator%3DTrue)%20%20%23%20one%20row%20per%20key%2C%20with%20where%20it%20was%20found%0A%0A%20%20%20%20anti%20%3D%20pl.from_pandas(units).join(pl.from_pandas(catch)%2C%20on%3Dkey%2C%20how%3D%22anti%22)%20%20%23%20anti_join%0A%20%20%20%20semi%20%3D%20pl.from_pandas(units).join(pl.from_pandas(catch)%2C%20on%3Dkey%2C%20how%3D%22semi%22)%20%20%23%20semi_join%0A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22-%20inner%20join%20of%20members%20and%20households%3A%20**%7Blen(members_with_design)%3A%2C%7D**%20of%20**%7Blen(members)%3A%2C%7D**%20members%20found%20their%20household%5Cn%22%0A%20%20%20%20%20%20%20%20f%22-%20full%20join%20of%20the%20units%20and%20the%20catch%20by%20the%20unit's%20key%2C%20keys%20by%20%60_merge%60%3A%20%7Bboth%5B'_merge'%5D.value_counts().to_dict()%7D%5Cn%22%0A%20%20%20%20%20%20%20%20f%22-%20polars%3A%20**%7Banti.height%3A%2C%7D**%20units%20with%20no%20catch%20row%20(%60how%3D%5C%22anti%5C%22%60)%2C%20**%7Bsemi.height%3A%2C%7D**%20with%20at%20least%20one%20(%60how%3D%5C%22semi%5C%22%60)%2C%20out%20of%20**%7Blen(units)%3A%2C%7D**%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Row%2014%2C%20%60bind_rows%60%3A%20%60pd.concat%60%20stacks%20tables%20and%20lines%20up%20their%20columns%20by%20name.%20Columns%20one%20table%20lacks%20are%20filled%20with%20missing%20values%2C%20as%20%60bind_rows%60%20does%3B%20polars%20wants%20%60how%3D%22diagonal%22%60%20to%20do%20the%20same%2C%20and%20refuses%20by%20default.%20A%20column%20with%20different%20types%20in%20different%20tables%20(region%2004's%20area%20codes%20are%20text%2C%20the%20other%20regions'%20numbers)%20is%20where%20they%20differ%3A%20pandas%20makes%20it%20a%20column%20of%20mixed%20values%2C%20polars%20refuses%2C%20R's%20%60bind_rows%60%20refuses.%20Module%202%20deals%20with%20it%20before%20stacking.%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%20first_rows%20%3D%20%5Bhouseholds%5Bhouseholds%5B%22region_code%22%5D%20%3D%3D%20reg%5D.head(2)%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22sel_final_weight%22%5D%5D%20for%20reg%20in%20course.REGIONS%5D%0A%20%20%20%20first_rows%5B2%5D%20%3D%20first_rows%5B2%5D.assign(note%3D%22only%20region%2007's%20table%20has%20this%20column%22)%0A%20%20%20%20side_by_side(%0A%20%20%20%20%20%20%20%20pandas%3Dpd.concat(first_rows%2C%20ignore_index%3DTrue)%2C%0A%20%20%20%20%20%20%20%20polars%3Dpl.concat(%5Bpl.from_pandas(part)%20for%20part%20in%20first_rows%5D%2C%20how%3D%22diagonal%22)%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204%20%C2%B7%20Recodes%20and%20reshaping%0A%0A%20%20%20%20**The%20problem.**%20A%20tabulation%20needs%20labels%2C%20not%20codes%2C%20and%20a%20category%20for%20every%20value%20that%20is%20not%20a%20valid%20code.%20A%20recode%20in%20R%20writes%20%60case_when%60%20with%20a%20category%20for%20a%20missing%20answer%20and%20one%20for%20a%20value%20outside%20the%20code%20list%3B%20the%20course%20calls%20them%20%22No%20answer%22%20and%20%22Invalid%20value%22.%20The%20survey's%20members%20table%20has%20sex%20codes%20that%20are%20neither%201%20nor%202%3A%20module%204%20finds%20them.%0A%0A%20%20%20%20**The%20tool.**%20%60np.select%60%20takes%20a%20list%20of%20conditions%20and%20a%20list%20of%20labels%2C%20and%20the%20first%20condition%20that%20holds%20gives%20a%20row%20its%20label%2C%20exactly%20as%20%60case_when%60%20reads%20its%20lines%20from%20the%20top.%20%60default%3D%60%20is%20R's%20%60.default%60%20(or%20%60TRUE%20~%60%20in%20older%20scripts).%20polars%20writes%20the%20same%20as%20a%20chain%20of%20%60when%60%20and%20%60then%60.%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%20sex%20%3D%20members%5B%22mem_sex%22%5D%0A%20%20%20%20sex_label%20%3D%20np.select(%0A%20%20%20%20%20%20%20%20%5Bsex.isna()%2C%20sex%20%3D%3D%201%2C%20sex%20%3D%3D%202%5D%2C%20%20%23%20tried%20in%20this%20order%2C%20as%20case_when's%20lines%0A%20%20%20%20%20%20%20%20%5B%22No%20answer%22%2C%20%22Male%22%2C%20%22Female%22%5D%2C%0A%20%20%20%20%20%20%20%20default%3D%22Invalid%20value%22%2C%0A%20%20%20%20)%0A%20%20%20%20sex_label_polars%20%3D%20pl.from_pandas(members).select(%0A%20%20%20%20%20%20%20%20pl.when(pl.col(%22mem_sex%22).is_null()).then(pl.lit(%22No%20answer%22))%0A%20%20%20%20%20%20%20%20.when(pl.col(%22mem_sex%22)%20%3D%3D%201).then(pl.lit(%22Male%22))%0A%20%20%20%20%20%20%20%20.when(pl.col(%22mem_sex%22)%20%3D%3D%202).then(pl.lit(%22Female%22))%0A%20%20%20%20%20%20%20%20.otherwise(pl.lit(%22Invalid%20value%22))%0A%20%20%20%20%20%20%20%20.alias(%22sex%22)%0A%20%20%20%20)%0A%20%20%20%20side_by_side(%0A%20%20%20%20%20%20%20%20pandas%3Dpd.Series(sex_label%2C%20name%3D%22sex%22).value_counts().to_frame()%2C%0A%20%20%20%20%20%20%20%20polars%3Dsex_label_polars%5B%22sex%22%5D.value_counts(sort%3DTrue)%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Rows%2016%20and%2017.%20%60np.where(condition%2C%20a%2C%20b)%60%20is%20%60if_else%60.%20For%20reshaping%2C%20pandas'%20%60pivot_table%60%20and%20polars'%20%60pivot%60%20take%20R's%20%60names_from%60%20as%20%60columns%3D%60%20or%20%60on%3D%60%2C%20and%20%60values_from%60%20as%20%60values%3D%60.%20pandas'%20%60melt%60%20and%20polars'%20%60unpivot%60%20go%20the%20other%20way%2C%20as%20%60pivot_longer%60.%20Below%3A%20members%20by%20sex%20code%20in%20each%20region%2C%20wide%3B%20and%20the%20three%20weights%20of%20the%20first%20two%20households%2C%20long.%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%20by_sex%20%3D%20members.assign(sex_code%3Dmembers%5B%22mem_sex%22%5D.astype(%22string%22))%20%20%23%20column%20names%20should%20be%20text%3A%20the%20codes%20become%20names%0A%20%20%20%20wide%20%3D%20by_sex.pivot_table(index%3D%22region_code%22%2C%20columns%3D%22sex_code%22%2C%20values%3D%22mem_line%22%2C%20aggfunc%3D%22size%22%2C%20fill_value%3D0)%0A%20%20%20%20weights%20%3D%20%5B%22sel_design_weight%22%2C%20%22sel_nr_weight%22%2C%20%22sel_final_weight%22%5D%0A%20%20%20%20long%20%3D%20households.head(2).melt(id_vars%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20value_vars%3Dweights%2C%20var_name%3D%22weight%22%2C%20value_name%3D%22value%22)%0A%20%20%20%20wide_polars%20%3D%20pl.from_pandas(by_sex).pivot(on%3D%22sex_code%22%2C%20index%3D%22region_code%22%2C%20values%3D%22mem_line%22%2C%20aggregate_function%3D%22len%22).fill_null(0)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20side_by_side(pandas_pivot_table%3Dwide%2C%20polars_pivot%3Dwide_polars)%2C%0A%20%20%20%20%20%20%20%20side_by_side(pandas_melt%3Dlong%2C%20polars_unpivot%3Dpl.from_pandas(households.head(2)).unpivot(on%3Dweights%2C%20index%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20variable_name%3D%22weight%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%205%20%C2%B7%20Databases%3A%20DBI%20and%20RMySQL%20against%20DuckDB%0A%0A%20%20%20%20**The%20problem.**%20A%20survey%20kept%20region%20by%20region%20often%20sits%20in%20one%20MySQL%20database%20per%20region%2C%20and%20an%20R%20script%20loops%20over%20them%3A%20connect%2C%20query%2C%20disconnect%2C%20then%20%60bind_rows%60.%20The%20course%20keeps%20each%20region%20in%20its%20own%20**DuckDB**%20file%2C%20the%20same%20shape%20without%20a%20server%20(module%202%20explains%20why%2C%20and%20reads%20a%20real%20MySQL%20server%20through%20DuckDB's%20%60mysql%60%20extension).%0A%0A%20%20%20%20**The%20tool.**%20%60duckdb.connect(path)%60%20is%20%60dbConnect%60.%20%60con.execute(sql)%60%20runs%20a%20query%2C%20and%20%60.df()%60%20hands%20the%20result%20over%20as%20a%20pandas%20table%2C%20%60.pl()%60%20as%20a%20polars%20one%3A%20together%20they%20are%20%60dbGetQuery%60.%20%60con.sql(...)%60%20makes%20a%20**relation**%2C%20DuckDB's%20lazy%20table%3A%20like%20dbplyr's%20%60tbl()%60%2C%20it%20runs%20nothing%20until%20you%20ask%20for%20the%20rows%2C%20so%20%60filter%60%20and%20%60select%60%20become%20part%20of%20one%20query.%20Python's%20standard%20library%20also%20reads%20SQLite%20files%20(%60sqlite3%60%2C%20with%20%60pd.read_sql%60)%2C%20the%20fallback%20where%20DuckDB%20cannot%20be%20installed.%0A%0A%20%20%20%20A%20household%20deleted%20in%20the%20capture%20system%20keeps%20its%20row%2C%20with%20the%20day%20of%20the%20deletion%20in%20%60hh_deleted_on%60%3A%20every%20count%20below%20leaves%20out%20the%20rows%20where%20it%20is%20set.%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%20dbConnect%2C%20dbGetQuery%2C%20dbDisconnect%3A%20one%20region's%20database%0A%20%20%20%20with%20duckdb.connect(str(course.PATHS.region(%2201%22))%2C%20read_only%3DTrue)%20as%20_db%3A%0A%20%20%20%20%20%20%20%20households_01%20%3D%20_db.execute(%22SELECT%20count(*)%20FROM%20household%20WHERE%20hh_deleted_on%20IS%20NULL%22).fetchone()%5B0%5D%0A%20%20%20%20%20%20%20%20by_interviewer%20%3D%20_db.execute(%22SELECT%20hh_interviewer%2C%20count(*)%20AS%20households%20FROM%20household%20WHERE%20hh_deleted_on%20IS%20NULL%20GROUP%20BY%201%20ORDER%20BY%201%22).df()%0A%0A%20%20%20%20%23%20tbl()%20%7C%3E%20filter()%20%7C%3E%20select()%20%7C%3E%20collect()%3A%20a%20lazy%20relation%2C%20run%20once%20at%20.pl()%0A%20%20%20%20_attached%20%3D%20course.connect()%20%20%23%20every%20region's%20file%20attached%2C%20read-only%2C%20as%20sfh2025_r01%2C%20sfh2025_r04%2C%20sfh2025_r07%0A%20%20%20%20_lazy%20%3D%20_attached.sql(%22FROM%20sfh2025_r07.household%22).filter(%22hh_deleted_on%20IS%20NULL%20AND%20hh_interview_minutes%20%3C%2015%22).select(%22hh_id%2C%20hh_interviewer%2C%20hh_interview_minutes%22)%0A%20%20%20%20short_interviews%20%3D%20_lazy.pl()%0A%20%20%20%20_attached.close()%0A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22Region%2001%20has%20**%7Bhouseholds_01%3A%2C%7D**%20households%20not%20deleted%20in%20the%20capture%20system.%20By%20interviewer%2C%20with%20%60.df()%60%3A%22)%2C%0A%20%20%20%20%20%20%20%20by_interviewer%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22Region%2007's%20interviews%20shorter%20than%2015%20minutes%2C%20with%20%60.pl()%60%3A%20**%7Bshort_interviews.height%7D**%2C%20from%20**%7Bshort_interviews%5B'hh_interviewer'%5D.n_unique()%7D**%20interviewer%20(module%208%20asks%20why).%22)%2C%0A%20%20%20%20%20%20%20%20short_interviews.head(5)%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%20The%20loop%20over%20regions%2C%20two%20ways.%20The%20first%20is%20an%20R%20script's%20loop%20written%20in%20Python%3A%20a%20%60for%60%20over%20the%20regions%2C%20one%20connection%20each%2C%20the%20results%20stacked%20at%20the%20end.%20The%20second%20is%20DuckDB's%20own%3A%20attach%20every%20file%20to%20one%20connection%2C%20then%20one%20query%20reads%20them%20all%2C%20%60UNION%20ALL%20BY%20NAME%60%20lining%20up%20the%20columns%20by%20name%20as%20%60bind_rows%60%20does.%20Module%202%20teaches%20the%20second.%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%20An%20R%20script's%20loop%2C%20in%20Python%0A%20%20%20%20parts%20%3D%20%5B%5D%0A%20%20%20%20for%20reg%20in%20course.REGIONS%3A%0A%20%20%20%20%20%20%20%20with%20duckdb.connect(str(course.PATHS.region(reg))%2C%20read_only%3DTrue)%20as%20_con%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20part%20%3D%20_con.execute(%22SELECT%20count(*)%20AS%20households%20FROM%20household%20WHERE%20hh_deleted_on%20IS%20NULL%22).df()%0A%20%20%20%20%20%20%20%20parts.append(part.assign(region%3Dreg))%0A%20%20%20%20looped%20%3D%20pd.concat(parts%2C%20ignore_index%3DTrue)%20%20%23%20bind_rows%0A%0A%20%20%20%20%23%20DuckDB's%20way%3A%20every%20file%20attached%2C%20one%20query%0A%20%20%20%20_con_all%20%3D%20course.connect()%0A%20%20%20%20one_query%20%3D%20_con_all.execute(%22%22%22%0A%20%20%20%20%20%20%20%20SELECT%20'01'%20AS%20region%2C%20count(*)%20AS%20households%20FROM%20sfh2025_r01.household%20WHERE%20hh_deleted_on%20IS%20NULL%0A%20%20%20%20%20%20%20%20UNION%20ALL%20BY%20NAME%0A%20%20%20%20%20%20%20%20SELECT%20'04'%20AS%20region%2C%20count(*)%20AS%20households%20FROM%20sfh2025_r04.household%20WHERE%20hh_deleted_on%20IS%20NULL%0A%20%20%20%20%20%20%20%20UNION%20ALL%20BY%20NAME%0A%20%20%20%20%20%20%20%20SELECT%20'07'%20AS%20region%2C%20count(*)%20AS%20households%20FROM%20sfh2025_r07.household%20WHERE%20hh_deleted_on%20IS%20NULL%0A%20%20%20%20%22%22%22).df()%0A%20%20%20%20_con_all.close()%0A%20%20%20%20side_by_side(a_for_loop%3Dlooped%2C%20one_query%3Done_query)%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%206%20%C2%B7%20Survey%20estimates%3A%20survey%20and%20ReGenesees%20against%20svy%2C%20and%20against%20your%20own%20code%0A%0A%20%20%20%20**The%20problem.**%20A%20table%20of%20the%20survey%20reports%20estimates%20for%20the%20population%2C%20not%20counts%20of%20the%20sample%2C%20each%20with%20its%20standard%20error.%20In%20R%20that%20is%20the%20%60survey%60%20package%20or%20ReGenesees%3B%20in%20Python%20it%20is%20**svy**%20(version%200.36.0%2C%20pinned%20by%20the%20course%2C%20since%20its%20interface%20still%20changes%20from%20one%20release%20to%20the%20next).%0A%0A%20%20%20%20**The%20tool.**%20svy%20works%20in%20two%20steps%2C%20as%20R%20does.%20%60svy.Design%60%20says%20which%20column%20is%20which%3A%20the%20strata%2C%20the%20primary%20sampling%20units%20(PSUs)%2C%20the%20weight%20and%20the%20population%20count%20of%20PSUs%20for%20the%20finite%20population%20correction.%20%60svy.Sample%60%20binds%20the%20design%20to%20the%20data.%20Estimates%20are%20methods%20of%20%60sample.estimation%60%2C%20and%20%60.to_polars()%60%20gives%20each%20as%20a%20table%3A%20%60est%60%2C%20%60se%60%2C%20%60lci%60%20and%20%60uci%60%20(the%2095%25%20interval)%2C%20%60cv%60%2C%20%60df%60%20(the%20design's%20degrees%20of%20freedom)%20and%20%60n%60.%20svy%20reads%20polars%20tables%2C%20so%20a%20pandas%20table%20goes%20in%20through%20%60pl.from_pandas%60.%0A%0A%20%20%20%20Two%20differences%20from%20R%20to%20keep%20in%20mind%3A%0A%0A%20%20%20%20-%20**The%20fpc%20is%20a%20count.**%20R's%20%60svydesign%60%20and%20ReGenesees'%20%60e.svydesign%60%20accept%20the%20finite%20population%20correction%20as%20population%20sizes%20or%20as%20sampling%20fractions%3B%20svy%20accepts%20counts%20only%20and%20refuses%20a%20fraction%20with%20an%20error.%20Primer%20%5BP2%5D(%2F%3Ffile%3D96_primer_survey_sampling.py)%20shows%20the%20conversion.%0A%20%20%20%20-%20**The%20cv%20is%20a%20fraction.**%20svy's%20%60cv%60%20of%200.032%20is%20a%20CV%20of%203.2%25%2C%20what%20ReGenesees%20gives%20with%20%60vartype%20%3D%20%22cvpct%22%60.%0A%0A%20%20%20%20The%20frame%3A%20%60course.design_frame()%60%20gives%20the%20households%20with%20%60stratum_id%60%20(region%2C%20province%20and%20stratum%20letter)%2C%20%60psu_id%60%20(the%20area%2C%20or%20the%20household%20itself%20in%20stratum%20F)%20and%20%60psus_in_stratum%60.%20The%20cell%20below%20also%20leaves%20out%20the%20interviews%20that%20are%20copies%20of%20others%2C%20which%20module%204%20finds%3A%20svy%20refuses%20the%20design%20with%20them%20in%2C%20for%20a%20reason%20P2%20explains.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20estimation_frame()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20households%20ready%20for%20svy%20(course.design_frame())%2C%20without%20the%20copied%20interviews%20module%204%20finds.%20The%20course's%0A%20%20%20%20list%20of%20planted%20defects%20names%20them%20here%3B%20module%204%20finds%20them%20from%20the%20answers%20themselves.%22%22%22%0A%20%20%20%20frame%20%3D%20course.design_frame()%0A%20%20%20%20copies%20%3D%20course.planted_defects().query(%22kind%20%3D%3D%20'household_copied'%22)%5B%5B%22region_code%22%2C%20%22hh_id%22%5D%5D%0A%20%20%20%20found%20%3D%20frame.merge(copies%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%5B%22_merge%22%5D.eq(%22both%22).to_numpy()%0A%20%20%20%20return%20frame%5B~found%5D.reset_index(drop%3DTrue)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20frame%20%3D%20estimation_frame()%0A%20%20%20%20design%20%3D%20svy.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)%0A%20%20%20%20columns%20%3D%20%5B%22region_code%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%2C%20%22sel_stratum%22%2C%20%22hh_roster_members%22%5D%0A%20%20%20%20sample%20%3D%20svy.Sample(data%3Dpl.from_pandas(frame%5Bcolumns%5D).with_columns(one%3Dpl.lit(1))%2C%20design%3Ddesign)%0A%0A%20%20%20%20members_total%20%3D%20sample.estimation.total(y%3D%22hh_roster_members%22%2C%20by%3D%22region_code%22).to_polars()%20%20%23%20svyby(~y%2C%20~region_code%2C%20des%2C%20svytotal)%0A%20%20%20%20members_mean%20%3D%20sample.estimation.mean(y%3D%22hh_roster_members%22%2C%20by%3D%22region_code%22).to_polars()%20%20%23%20svyby(...%2C%20svymean)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Household%20members%20by%20region%3A%20the%20total**%20(%60svystatTM(des%2C%20~hh_roster_members%2C%20by%20%3D%20~region_code)%60)%22)%2C%0A%20%20%20%20%20%20%20%20members_total%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**The%20mean%20per%20household**%20(%60estimator%20%3D%20%5C%22Mean%5C%22%60)%22)%2C%0A%20%20%20%20%20%20%20%20members_mean%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20frame%2C%20members_mean%2C%20members_total%2C%20sample%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**The%20same%20estimates%2C%20without%20svy.**%20An%20estimated%20total%20is%20the%20sum%20of%20every%20household's%20value%20times%20its%20weight%3A%20each%20sampled%20household%20stands%20for%20%60sel_final_weight%60%20households%20of%20the%20population.%20An%20estimated%20mean%20is%20that%20total%20divided%20by%20the%20estimated%20number%20of%20households%2C%20the%20sum%20of%20the%20weights.%20Neither%20needs%20a%20survey%20library%2C%20only%20a%20sum%3A%20this%20is%20what%20%60svytotal%60%20and%20%60svymean%60%20compute%20before%20they%20compute%20the%20standard%20error.%20The%20standard%20error%20needs%20the%20design%20(strata%2C%20PSUs%2C%20the%20fpc)%20and%20takes%20a%20few%20lines%20more%3A%20primer%20P2%20writes%20them%20in%20numpy%20and%20checks%20them%20against%20svy.%0A%0A%20%20%20%20Rows%2025%20and%2026%20below%3A%20shares%20of%20a%20category%20are%20weighted%20counts%20divided%20by%20the%20sum%20of%20the%20weights%2C%20and%20a%20ratio%20is%20one%20weighted%20total%20divided%20by%20another.%20A%20mean%20is%20a%20ratio%20whose%20second%20variable%20is%201%20for%20every%20household%2C%20which%20the%20%60ratio%60%20line%20shows.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20w%20%3D%20frame%5B%22sel_final_weight%22%5D%0A%20%20%20%20y%20%3D%20frame%5B%22hh_roster_members%22%5D%0A%20%20%20%20shares_svy%20%3D%20sample.estimation.prop(y%3D%22sel_stratum%22).to_polars().select(%22sel_stratum%22%2C%20%22est%22%2C%20%22se%22)%0A%20%20%20%20shares_own%20%3D%20(w.groupby(frame%5B%22sel_stratum%22%5D).sum()%20%2F%20w.sum()).rename(%22est%22).reset_index()%0A%20%20%20%20ratio_svy%20%3D%20sample.estimation.ratio(y%3D%22hh_roster_members%22%2C%20x%3D%22one%22).to_polars().select(%22est%22%2C%20%22se%22)%0A%20%20%20%20ratio_own%20%3D%20(w%20*%20y).sum()%20%2F%20(w%20*%201).sum()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20side_by_side(svy_prop%3Dshares_svy%2C%20your_own%3Dshares_own)%2C%0A%20%20%20%20%20%20%20%20side_by_side(svy_ratio%3Dratio_svy%2C%20your_own%3Dmo.md(f%22%60(w%20*%20y).sum()%20%2F%20(w%20*%201).sum()%60%20%3D%20**%7Bratio_own%3A.6f%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%20Exercise%20P1.2.%20Weighted%20totals%20and%20means%20by%20domain%2C%20without%20svy%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20estimate%20in%20the%20course's%20tables%20starts%20as%20a%20weighted%20sum.%20Writing%20it%20yourself%20is%20how%20you%20know%20what%20svy's%20%60est%60%20column%20is%2C%20and%20how%20you%20check%20a%20number%20when%20R%20and%20Python%20disagree.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20households%2C%20the%20name%20of%20a%20variable%20%60y%60%2C%20the%20name%20of%20a%20column%20%60by%60%20that%20defines%20the%20domains%2C%20and%20the%20name%20of%20the%20weight%20column%20(%60%22sel_final_weight%22%60%20unless%20another%20is%20given)%20%E2%86%92%20one%20row%20per%20domain%2C%20with%20four%20columns%3A%0A%0A%20%20%20%20%7C%20%60by%60%20column%20%7C%20households%20%7C%20total%20%7C%20mean%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%20%E2%80%A6%20%7C%20%CE%A3%20w%C2%B7y%20over%20the%20domain%20%7C%20total%20%C3%B7%20%CE%A3%20w%20over%20the%20domain%20%7C%0A%0A%20%20%20%20For%20two%20households%20in%20one%20domain%2C%20with%20weights%2010%20and%2030%20and%20values%202%20and%206%3A%20%60total%60%20is%2010%C2%B72%20%2B%2030%C2%B76%20%3D%20200%2C%20and%20%60mean%60%20is%20200%20%2F%2040%20%3D%205%20(not%20the%20plain%20mean%204%2C%20nor%20the%20mean%20of%20w%C2%B7y%2C%20100).%0A%0A%20%20%20%20**Tools%3A**%20a%20column%20made%20with%20%60assign%60%20(w%C2%B7y)%3B%20%60groupby(by%2C%20as_index%3DFalse).agg(...)%60%20with%20%60%22size%22%60%20and%20%60%22sum%22%60%3B%20a%20division%20of%20two%20columns.%20No%20missing%20values%20in%20%60y%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20weighted_by_domain(frame%3A%20pd.DataFrame%2C%20y%3A%20str%2C%20by%3A%20str%2C%20weight%3A%20str%20%3D%20%22sel_final_weight%22)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20domain%3A%20the%20number%20of%20households%2C%20the%20weighted%20total%20of%20y%20and%20its%20weighted%20mean.%22%22%22%0A%20%20%20%20%23%20Exercise%20P1.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20column%20of%20weight%20times%20y%2C%20and%20keep%20the%20weight%20beside%20it%0A%20%20%20%20%23%20Step%202%3A%20group%20by%20the%20domain%20column%2C%20as%20columns%3B%20sum%20both%2C%20and%20count%20the%20rows%0A%20%20%20%20%23%20Step%203%3A%20mean%20%3D%20the%20sum%20of%20weight%20times%20y%20divided%20by%20the%20sum%20of%20the%20weights%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20domain%20column%2C%20households%2C%20total%20and%20mean%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20P1.2%22)%0A%0A%0A%40app.function%0Adef%20three_domains()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Seven%20households%20in%20three%20domains%20with%20unequal%20weights%2C%20used%20by%20the%20checks%3A%20in%20each%20domain%20the%20weighted%20mean%2C%0A%20%20%20%20the%20plain%20mean%20and%20the%20mean%20of%20w%C2%B7y%20differ.%20w_one%20is%20a%20weight%20of%201%20on%20every%20row.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22domain%22%3A%20%5B%22A%22%2C%20%22A%22%2C%20%22B%22%2C%20%22B%22%2C%20%22B%22%2C%20%22C%22%2C%20%22C%22%5D%2C%0A%20%20%20%20%20%20%20%20%22sel_final_weight%22%3A%20%5B10.0%2C%2030.0%2C%202.0%2C%202.0%2C%2020.0%2C%201.0%2C%204.0%5D%2C%0A%20%20%20%20%20%20%20%20%22members%22%3A%20%5B2%2C%206%2C%203%2C%205%2C%204%2C%208%2C%203%5D%2C%0A%20%20%20%20%20%20%20%20%22w_one%22%3A%20%5B1.0%2C%201.0%2C%201.0%2C%201.0%2C%201.0%2C%201.0%2C%201.0%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20domain_rows(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20table%20of%20domains%20in%20one%20shape%20for%20the%20checks%2C%20whether%20the%20domain%20came%20back%20as%20a%20column%20or%20as%20the%20index.%22%22%22%0A%20%20%20%20table%20%3D%20table.reset_index()%20if%20table.index.name%20is%20not%20None%20else%20table%0A%20%20%20%20return%20as_rows(table%2C%20table.columns%5B0%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20P1.2%22%2C%20weighted_by_domain%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22household%20members%20by%20region%2C%20weighted%20by%20sel_final_weight%22%2C%20(frame%2C%20%22hh_roster_members%22%2C%20%22region_code%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table%3A%20three%20domains%20with%20unequal%20weights%22%2C%20(three_domains()%2C%20%22members%22%2C%20%22domain%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20small%20table%20with%20every%20weight%201%20(w_one)%22%2C%20(three_domains()%2C%20%22members%22%2C%20%22domain%22%2C%20%22w_one%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22household%20members%20by%20stratum%22%2C%20(frame%2C%20%22hh_roster_members%22%2C%20%22sel_stratum%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%3Ddomain_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22total%20%3D%20the%20sum%20of%20weight%20%C3%97%20y%3B%20mean%20%3D%20that%20total%20divided%20by%20the%20sum%20of%20the%20weights%2C%20both%20by%20domain.%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%20members%20by%20region.%20Compare%20total%20and%20mean%20with%20svy's%20est%20above.%0A%20%20%20%20try_it(weighted_by_domain%2C%20frame%2C%20%22hh_roster_members%22%2C%20%22region_code%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%20P1.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%60mean%60%20is%204.0%20for%20domain%20A%20of%20the%20small%20table%2C%20not%205.0%20%7C%20The%20plain%20mean%20of%20%60y%60%2C%20without%20the%20weights.%20%7C%20step%203%3A%20%CE%A3%20w%C2%B7y%20%C3%B7%20%CE%A3%20w%20%7C%0A%20%20%20%20%7C%20%60mean%60%20is%20100.0%20for%20domain%20A%20%7C%20The%20mean%20of%20w%C2%B7y%3A%20the%20total%20divided%20by%20the%20number%20of%20households%2C%20not%20by%20the%20sum%20of%20their%20weights.%20%7C%20step%203%3A%20divide%20by%20the%20sum%20of%20the%20weights%20%7C%0A%20%20%20%20%7C%20%60total%60%20equals%20the%20sum%20of%20%60y%60%20%7C%20The%20weights%20are%20not%20used.%20%7C%20step%201%3A%20%60frame%5Bweight%5D%20*%20frame%5By%5D%60%20%7C%0A%20%20%20%20%7C%20The%20third%20case%20is%20red%20and%20the%20others%20green%20%7C%20The%20weight's%20name%20is%20typed%20in%20as%20%60%22sel_final_weight%22%60%20instead%20of%20taken%20from%20%60weight%60.%20%7C%20%60frame%5Bweight%5D%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'domain'*%20or%20a%20missing%20domain%20column%20%7C%20The%20column%20is%20named%20%60%22by%22%60%2C%20or%20%60as_index%3DFalse%60%20is%20missing%20and%20the%20domain%20is%20in%20the%20index.%20%7C%20%60groupby(by%2C%20as_index%3DFalse)%60%3B%20return%20%60%5Bby%2C%20%22households%22%2C%20%22total%22%2C%20%22mean%22%5D%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20members_mean%2C%20members_total)%3A%0A%20%20%20%20%23%20Your%20function%20beside%20svy%3A%20the%20same%20estimates%2C%20to%20the%20last%20digit%20shown%0A%20%20%20%20_mine%20%3D%20attempt(weighted_by_domain%2C%20frame%2C%20%22hh_roster_members%22%2C%20%22region_code%22)%0A%20%20%20%20mo.stop(_mine%20is%20NOT_YET%2C%20waiting_for(%22Exercise%20P1.2%22))%0A%20%20%20%20_svy%20%3D%20members_total.select(%22region_code%22%2C%20svy_total%3D%22est%22).join(members_mean.select(%22region_code%22%2C%20svy_mean%3D%22est%22)%2C%20on%3D%22region_code%22)%0A%20%20%20%20_both%20%3D%20_svy.to_pandas().merge(_mine%2C%20on%3D%22region_code%22).assign(%0A%20%20%20%20%20%20%20%20total_differs_by%3Dlambda%20t%3A%20(t%5B%22total%22%5D%20-%20t%5B%22svy_total%22%5D).abs()%2C%0A%20%20%20%20%20%20%20%20mean_differs_by%3Dlambda%20t%3A%20(t%5B%22mean%22%5D%20-%20t%5B%22svy_mean%22%5D).abs()%2C%0A%20%20%20%20)%0A%20%20%20%20_same%20%3D%20bool((_both%5B%22total_differs_by%22%5D%20%3C%201e-6).all()%20and%20(_both%5B%22mean_differs_by%22%5D%20%3C%201e-9).all())%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_both%2C%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(%22**The%20same%20as%20svy's%20%60est%60.**%20The%20point%20estimates%20need%20no%20survey%20library%3B%20the%20standard%20error%20is%20primer%20P2's%20subject.%22%20if%20_same%20else%20%22**Not%20the%20same%20as%20svy's%20%60est%60%20yet.**%20Compare%20your%20function%20with%20the%20check%20above.%22)%2C%20kind%3D%22success%22%20if%20_same%20else%20%22warn%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**The%20sample's%20own%20counts%2C%20beside%20the%20estimates.**%20A%20published%20table%20puts%20beside%20each%20weighted%20estimate%20the%20number%20of%20interviews%20it%20rests%20on%2C%20and%20sometimes%20the%20sample's%20own%20sums.%20The%20course%20computes%20them%20with%20pandas%3A%20one%20%60groupby%60%20with%20%60%22size%22%60%20and%20%60%22sum%22%60%2C%20no%20weights.%20In%20R%20you%20may%20meet%20another%20way%2C%20a%20second%20design%20whose%20every%20weight%20is%201%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20hh%24w_one%20%3C-%201%0A%20%20%20%20options(RG.lonely.psu%20%3D%20%22remove%22)%0A%20%20%20%20des_one%20%3C-%20e.svydesign(data%20%3D%20hh%2C%20ids%20%3D%20~w_one%2C%20weights%20%3D%20~w_one)%0A%20%20%20%20svystatTM(des_one%2C%20~hh_roster_members%2C%20by%20%3D%20~region_code)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20With%20every%20weight%201%2C%20each%20total%20is%20the%20sample's%20own%20sum%2C%20%CE%A3%201%C2%B7y%20%3D%20%CE%A3%20y%2C%20and%20a%20total%20of%20a%20factor%20is%20a%20count%20of%20households.%20%60ids%20%3D%20~w_one%60%20puts%20every%20household%20in%20one%20cluster%2C%20so%20the%20design%20has%20a%20single%20PSU%3A%20under%20ReGenesees'%20default%2C%20%60RG.lonely.psu%20%3D%20%22fail%22%60%2C%20%60svystatTM%60%20stops%20with%20*Stratum%20(1)%20has%20only%20one%20sampling%20unit%20at%20stage%201*%2C%20and%20under%20the%20other%20three%20settings%20it%20returns%20the%20sums%20with%20a%20standard%20error%20of%200.%20R's%20%60svydesign%60%20refuses%20the%20same%20declaration%20with%20*Design%20has%20only%20one%20primary%20sampling%20unit*.%20(Run%20with%20ReGenesees%202.5%20and%20survey%204.4-2%20on%20R%204.5.0%2C%20November%202026.)%0A%0A%20%20%20%20The%20Python%20equivalent%20is%20the%20counting%20itself%2C%20shown%20below%20beside%20exercise%20P1.2%20run%20with%20the%20weight%20%60w_one%60%3A%20the%20same%20sums%2C%20with%20no%20design%20to%20declare.%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%20own_counts%20%3D%20frame.groupby(%22region_code%22%2C%20as_index%3DFalse).agg(households%3D(%22hh_id%22%2C%20%22size%22)%2C%20members%3D(%22hh_roster_members%22%2C%20%22sum%22))%0A%20%20%20%20_mine%20%3D%20attempt(weighted_by_domain%2C%20frame.assign(w_one%3D1.0)%2C%20%22hh_roster_members%22%2C%20%22region_code%22%2C%20%22w_one%22)%0A%20%20%20%20mo.stop(_mine%20is%20NOT_YET%2C%20mo.vstack(%5Bmo.md(%22**pandas**%2C%20one%20%60groupby%60%22)%2C%20own_counts%2C%20waiting_for(%22Exercise%20P1.2%22)%5D))%0A%20%20%20%20side_by_side(pandas_groupby%3Down_counts%2C%20exercise_P1_2_with_w_one%3D_mine)%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**Calls%20no%20Python%20library%20offers.**%20Two%20ReGenesees%20features%20have%20no%20equivalent%20in%20any%20maintained%20Python%20package%20the%20course%20found%20(November%202026)%3A%20calibration%20with%20the%20**logit**%20distance%20and%20bounds%2C%20and%20calibrated%20weights%20kept%20**equal%20within%20a%20cluster**%20(%60aggregate.stage%60).%20Module%206%20writes%20each%20as%20a%20short%20function%20of%20its%20own%2C%20from%20the%20formula%2C%20and%20tests%20it%20(%5Bsection%204%5D(%2F%3Ffile%3D50_design_estimates.py)).%20That%20is%20the%20same%20move%20as%20exercise%20P1.2%2C%20one%20step%20further%3A%20understand%20what%20the%20R%20function%20computes%2C%20then%20write%20it.%0A%0A%20%20%20%20%23%23%207%20%C2%B7%20Files%3A%20RDS%20against%20Parquet%0A%0A%20%20%20%20**The%20problem.**%20A%20processing%20step%20saves%20its%20table%20for%20the%20next%20step%2C%20and%20the%20table%20must%20come%20back%20exactly%3A%20the%20same%20column%20types%2C%20codes%20still%20text%20with%20their%20leading%20zeros%2C%20missing%20values%20still%20missing.%20R%20scripts%20save%20with%20%60saveRDS%60%2C%20which%20only%20R%20can%20read.%0A%0A%20%20%20%20**The%20tool.**%20**Parquet**%20is%20the%20file%20format%20that%20does%20for%20every%20language%20what%20RDS%20does%20for%20R.%20A%20Parquet%20file%20keeps%20each%20column's%20type%2C%20compresses%20well%20and%20is%20read%20by%20pandas%2C%20polars%2C%20DuckDB%2C%20R%20(the%20%60arrow%60%20package's%20%60read_parquet%60)%20and%20most%20data%20tools.%20A%20CSV%20file%20keeps%20none%20of%20the%20types%3A%20a%20code%20that%20looks%20like%20a%20number%20comes%20back%20as%20one%2C%20without%20its%20leading%20zeros%2C%20as%20module%202%20shows.%20To%20bring%20an%20RDS%20file%20across%2C%20write%20it%20once%20from%20R%20as%20Parquet%3A%20%60arrow%3A%3Awrite_parquet(readRDS(%22hh.rds%22)%2C%20%22hh.parquet%22)%60.%0A%0A%20%20%20%20The%20cell%20below%20saves%20the%20households%20to%20Parquet%20in%20memory%20(a%20buffer%20stands%20in%20for%20a%20file%2C%20so%20nothing%20is%20written%20to%20your%20folder)%20and%20reads%20them%20back%20with%20pandas%20and%20with%20polars.%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%20saved%20%3D%20households%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22hh_id%22%2C%20%22hh_interviewer%22%2C%20%22hh_interview_date%22%2C%20%22sel_stratum%22%2C%20%22sel_final_weight%22%2C%20%22sel_excluded%22%5D%5D%0A%20%20%20%20buffer%20%3D%20io.BytesIO()%0A%20%20%20%20saved.to_parquet(buffer)%20%20%23%20saveRDS(hh%2C%20%22hh.rds%22)%20-%3E%20hh.to_parquet(%22hh.parquet%22)%0A%20%20%20%20back%20%3D%20pd.read_parquet(io.BytesIO(buffer.getvalue()))%20%20%23%20readRDS%20-%3E%20pd.read_parquet%0A%20%20%20%20back_polars%20%3D%20pl.read_parquet(io.BytesIO(buffer.getvalue()))%0A%20%20%20%20types%20%3D%20pd.DataFrame(%7B%22saved%22%3A%20saved.dtypes.astype(str)%2C%20%22read%20back%20by%20pandas%22%3A%20back.dtypes.astype(str)%2C%20%22read%20back%20by%20polars%22%3A%20%5Bstr(t)%20for%20t%20in%20back_polars.dtypes%5D%7D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(buffer.getvalue())%20%2F%201024%3A%2C.0f%7D%20KB**%20for%20%7Blen(saved)%3A%2C%7D%20rows.%20Same%20values%20after%20the%20round%20trip%3A%20**%7Bsaved.reset_index(drop%3DTrue).equals(back)%7D**.%20The%20column%20types%3A%22)%2C%0A%20%20%20%20%20%20%20%20types%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%60writexl%3A%3Awrite_xlsx(list(...))%60%2C%20row%2033%20of%20the%20table%2C%20is%20pandas'%20%60ExcelWriter%60%20with%20one%20%60to_excel%60%20per%20sheet.%20Module%207%20adds%20number%20formats%20and%20a%20notes%20sheet%3B%20the%20cell%20below%20writes%20two%20sheets%20to%20a%20buffer%20and%20lists%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(members_mean%2C%20members_total)%3A%0A%20%20%20%20workbook%20%3D%20io.BytesIO()%0A%20%20%20%20with%20pd.ExcelWriter(workbook%2C%20engine%3D%22xlsxwriter%22)%20as%20xl%3A%20%20%23%20write_xlsx(list(total%20%3D%20...%2C%20mean%20%3D%20...)%2C%20%22tables.xlsx%22)%0A%20%20%20%20%20%20%20%20members_total.to_pandas().to_excel(xl%2C%20sheet_name%3D%22total%22%2C%20index%3DFalse)%0A%20%20%20%20%20%20%20%20members_mean.to_pandas().to_excel(xl%2C%20sheet_name%3D%22mean%22%2C%20index%3DFalse)%0A%20%20%20%20pd.ExcelFile(io.BytesIO(workbook.getvalue())).sheet_names%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%208%20%C2%B7%20Reports%3A%20Quarto%20against%20marimo%0A%0A%20%20%20%20**The%20problem.**%20A%20tabulation%20report%20should%20be%20made%20by%20its%20own%20code%2C%20so%20that%20it%20can%20be%20run%20again%20on%20the%20next%20round's%20data.%20In%20R%20that%20is%20often%20a%20Quarto%20(or%20R%20Markdown)%20document.%20This%20course's%20notebooks%20are%20**marimo**%20notebooks%2C%20and%20the%20stretch%20S2%20makes%20one%20the%20report%20(%5Bits%20part%204%5D(%2F%3Ffile%3D61_stretch_tabulation.py)).%0A%0A%20%20%20%20%7C%20%7C%20Quarto%20(%60.qmd%60)%20%7C%20marimo%20(%60.py%60)%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20the%20file%20%7C%20text%20and%20code%20chunks%20in%20one%20Markdown%20file%20%7C%20a%20Python%20file%3B%20each%20cell%20is%20a%20function%20marimo%20writes%20for%20you%20%7C%0A%20%20%20%20%7C%20how%20it%20runs%20%7C%20top%20to%20bottom%2C%20every%20chunk%2C%20at%20each%20render%20%7C%20each%20cell%20when%20what%20it%20uses%20changes%3B%20opening%20the%20notebook%20runs%20it%20all%20%7C%0A%20%20%20%20%7C%20a%20changed%20value%20%7C%20render%20again%20%7C%20the%20cells%20that%20use%20it%20run%20again%20by%20themselves%20%7C%0A%20%20%20%20%7C%20stale%20results%20%7C%20possible%20in%20an%20editor%20that%20runs%20chunks%20out%20of%20order%20%7C%20not%20possible%3A%20a%20result%20always%20matches%20the%20code%20above%20it%20%7C%0A%20%20%20%20%7C%20parameters%20%7C%20%60params%3A%60%20in%20the%20header%2C%20set%20at%20render%20%7C%20controls%20(%60mo.ui.dropdown%60%2C%20a%20slider)%20in%20the%20notebook%3B%20%60mo.cli_args()%60%20from%20the%20command%20line%20%7C%0A%20%20%20%20%7C%20output%20%7C%20HTML%2C%20Word%2C%20PDF%2C%20slides%20%7C%20HTML%20(%60marimo%20export%20html%60%2C%20with%20%60--no-include-code%60%20for%20readers)%2C%20PDF%2C%20Markdown%2C%20a%20Jupyter%20notebook%3B%20an%20app%20(%60marimo%20run%60)%20%7C%0A%20%20%20%20%7C%20as%20a%20script%20%7C%20%60quarto%20render%20report.qmd%60%20%7C%20%60python%20report.py%60%2C%20or%20%60marimo%20export%20html%20report.py%20-o%20report.html%60%20%7C%0A%20%20%20%20%7C%20code%20in%20version%20control%20%7C%20readable%20%7C%20readable%3A%20plain%20Python%2C%20one%20cell%20after%20another%20%7C%0A%0A%20%20%20%20Quarto%20can%20also%20run%20Python%2C%20through%20Jupyter%3B%20the%20course%20uses%20marimo%20for%20the%20reason%20in%20the%20fourth%20row.%20A%20report%20whose%20numbers%20always%20match%20its%20code%20is%20a%20report%20you%20can%20hand%20on.%20Where%20a%20Word%20document%20is%20the%20deliverable%2C%20Quarto%20stays%20the%20tool%2C%20and%20it%20can%20render%20a%20Python%20document%20as%20well%20as%20an%20R%20one.%0A%0A%20%20%20%20%23%23%209%20%C2%B7%20Habits%20that%20differ%0A%0A%20%20%20%20**The%20problem.**%20Most%20R%20code%20translates%20line%20by%20line.%20A%20few%20R%20habits%20carry%20over%20and%20give%20a%20wrong%20answer%20without%20an%20error%2C%20or%20an%20error%20whose%20cause%20is%20not%20obvious.%20These%20are%20the%20ones%20a%20statistician%20coming%20from%20R%20meets%20in%20this%20course.%0A%0A%20%20%20%20%7C%20R%20habit%20%7C%20in%20Python%20%7C%20why%20it%20matters%20here%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20positions%20count%20from%201%3B%20%60substr(x%2C%203%2C%205)%60%20includes%20both%20ends%20%7C%20positions%20count%20from%20**0**%3B%20%60x%5B2%3A5%5D%60%20includes%20the%20start%20and%20**excludes**%20the%20end%20%7C%20splitting%20a%20household%20code%20into%20its%20area%20codes%20(exercise%20P1.3)%20%7C%0A%20%20%20%20%7C%20%60y%20%3C-%20x%3B%20y%24a%20%3C-%200%60%20leaves%20%60x%60%20as%20it%20was%20%7C%20the%20same%20in%20pandas%203%20(**copy-on-write**)%20and%20polars%3B%20data.table's%20%60%3A%3D%60%20was%20the%20exception%2C%20and%20Python%20has%20no%20such%20step%20%7C%20a%20selection%20can%20be%20changed%20freely%3B%20to%20change%20a%20table%2C%20assign%20to%20the%20table%20%7C%0A%20%20%20%20%7C%20%60x%20%7C%3E%20f()%20%7C%3E%20g()%60%20%7C%20%60x.f().g()%60%2C%20one%20method%20after%20another%2C%20the%20chain%20in%20round%20brackets%20to%20span%20lines%3B%20%60.pipe(h)%60%20for%20a%20function%20of%20your%20own%20%7C%20every%20chain%20in%20the%20course%20%7C%0A%20%20%20%20%7C%20%60a%20%3D%3D%201%20%26%20b%20%3D%3D%202%60%20%7C%20%60(a%20%3D%3D%201)%20%26%20(b%20%3D%3D%202)%60%3A%20**brackets**%20around%20each%20comparison%20%7C%20without%20them%20Python%20reads%20%601%20%26%20b%60%20first%20%7C%0A%20%20%20%20%7C%20%60x%20%25in%25%20c(1%2C%202)%60%20%7C%20%60x.isin(%5B1%2C%202%5D)%60%2C%20polars%20%60pl.col(%22x%22).is_in(%5B1%2C%202%5D)%60%20%7C%20the%20valid%20codes%20of%20a%20variable%2C%20in%20one%20test%20%7C%0A%20%20%20%20%7C%20%60sum(x)%60%20is%20%60NA%60%20if%20any%20%60x%60%20is%20%7C%20pandas%20and%20polars%20**skip**%20missing%20values%3A%20%60sum%60%2C%20%60mean%60%2C%20%60count%60%20%7C%20a%20total%20over%20incomplete%20data%20looks%20complete%20%7C%0A%20%20%20%20%7C%20%60NA%60%20%7C%20pandas%3A%20%60NaN%60%20in%20decimal%20columns%2C%20%60%3CNA%3E%60%20in%20nullable%20ones%20(%60Int64%60%2C%20%60string%60)%3B%20polars%3A%20%60null%60%20(and%20%60NaN%60%20is%20a%20number%2C%20not%20missing)%20%7C%20test%20with%20%60.isna()%60%20or%20%60.is_null()%60%3A%20%60%3D%3D%20None%60%20does%20not%20find%20them%20%7C%0A%20%20%20%20%7C%20%60factor(x%2C%20levels%2C%20labels)%60%20%7C%20pandas%20%60Categorical%60%2C%20polars%20%60Enum%60%20%7C%20values%20outside%20the%20levels%20%7C%0A%0A%20%20%20%20**Counting%20from%200.**%20A%20Python%20position%20is%20the%20number%20of%20items%20**before**%20it%2C%20so%20the%20first%20is%200.%20A%20slice%20%60start%3Aend%60%20takes%20the%20items%20from%20%60start%60%20up%20to%2C%20not%20including%2C%20%60end%60%3A%20%60%5B2%3A5%5D%60%20is%20three%20items%2C%20the%20third%2C%20fourth%20and%20fifth.%20R's%20%60substr(hh_code%2C%203%2C%205)%60%20is%20therefore%20%60hh_code%5B2%3A5%5D%60%3A%20subtract%201%20from%20the%20start%2C%20keep%20the%20end.%20The%20same%20holds%20for%20%60.iloc%60%20on%20a%20table%20and%20for%20%60.str%5B...%5D%60%20on%20a%20column%20of%20text.%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%20hh_code%20%3D%20households%5B%22hh_code%22%5D.iloc%5B0%5D%20%20%23%20the%20first%20row%3A%20position%200%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22%60hh_code%20%3D%20households%5B%5C%22hh_code%5C%22%5D.iloc%5B0%5D%60%20is%20%60%5C%22%7Bhh_code%7D%5C%22%60.%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20f%22-%20%60hh_code%5B0%3A2%5D%60%20is%20%60%5C%22%7Bhh_code%5B0%3A2%5D%7D%5C%22%60%3A%20the%20region%2C%20R's%20%60substr(hh_code%2C%201%2C%202)%60%5Cn%22%0A%20%20%20%20%20%20%20%20f%22-%20%60hh_code%5B2%3A5%5D%60%20is%20%60%5C%22%7Bhh_code%5B2%3A5%5D%7D%5C%22%60%3A%20the%20province%2C%20R's%20%60substr(hh_code%2C%203%2C%205)%60%5Cn%22%0A%20%20%20%20%20%20%20%20f%22-%20%60hh_code%5B-3%3A%5D%60%20is%20%60%5C%22%7Bhh_code%5B-3%3A%5D%7D%5C%22%60%3A%20the%20last%20three%20characters%2C%20counted%20from%20the%20end%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Copy-on-write.**%20R%20copies%20a%20table%20when%20you%20change%20it%2C%20so%20a%20change%20to%20%60y%60%20never%20reaches%20%60x%60.%20pandas%203%20behaves%20the%20same%3A%20a%20selection%20is%20your%20own%20to%20change%2C%20and%20the%20table%20it%20came%20from%20stays%20as%20it%20was.%20(pandas%202%20sometimes%20changed%20both%2C%20which%20is%20why%20older%20code%20is%20full%20of%20%60.copy()%60.%20The%20course%20Arrays%20%26%20DataFrames%20has%20a%20primer%20on%20what%20changed%20from%20pandas%202%20to%203.)%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%20def%20_change_a_selection()%3A%0A%20%20%20%20%20%20%20%20listed%20%3D%20households%5Bhouseholds%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22%5D%0A%20%20%20%20%20%20%20%20listed%5B%22sel_final_weight%22%5D%20%3D%200.0%20%20%23%20changes%20the%20selection%20only%0A%20%20%20%20%20%20%20%20return%20f%22stratum%20F's%20weights%20now%20sum%20to%20%7Blisted%5B'sel_final_weight'%5D.sum()%7D%3B%20in%20households%20they%20still%20sum%20to%20%7Bhouseholds.loc%5Bhouseholds%5B'sel_stratum'%5D%20%3D%3D%20'F'%2C%20'sel_final_weight'%5D.sum()%3A%2C.2f%7D%22%0A%0A%20%20%20%20show(_change_a_selection)%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**Brackets%20around%20each%20comparison.**%20In%20R%2C%20%60%26%60%20binds%20more%20loosely%20than%20%60%3D%3D%60%2C%20so%20%60a%20%3D%3D%201%20%26%20b%20%3D%3D%202%60%20means%20what%20it%20says.%20In%20Python%2C%20%60%26%60%20binds%20**more%20tightly**%2C%20so%20the%20same%20line%20is%20read%20as%20%60a%20%3D%3D%20(1%20%26%20b)%20%3D%3D%202%60%2C%20and%20pandas%20stops%20with%20an%20error%20whose%20last%20line%20does%20not%20mention%20brackets%20at%20all.%20Put%20each%20comparison%20in%20brackets.%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%20show(lambda%3A%20households%5Bhouseholds%5B%22region_code%22%5D%20%3D%3D%20%2201%22%20%26%20households%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20What%20to%20write%0A%20%20%20%20show(lambda%3A%20households%5B(households%5B%22region_code%22%5D%20%3D%3D%20%2201%22)%20%26%20(households%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22)%5D.shape)%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**Missing%20values.**%20R's%20default%20is%20the%20careful%20one%3A%20%60sum(x)%60%20and%20%60mean(x)%60%20give%20%60NA%60%20when%20any%20%60x%60%20is%20missing%2C%20and%20you%20say%20%60na.rm%20%3D%20TRUE%60%20to%20leave%20the%20missing%20ones%20out.%20pandas%20and%20polars%20do%20the%20opposite%20by%20default%3A%20they%20leave%20missing%20values%20out%20without%20a%20word.%20The%20survey%20asks%20whether%20a%20member%20took%20part%20in%20fishing%20in%202025%20(%60mem_fished%60)%20of%20members%2010%20years%20old%20and%20over%2C%20so%20the%20younger%20ones%20have%20no%20answer%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%20fished%20%3D%20members%5B%22mem_fished%22%5D%0A%20%20%20%20fished_pl%20%3D%20pl.from_pandas(members)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22%7C%20%7C%20R%20%7C%20pandas%20%7C%20polars%20%7C%5Cn%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%5Cn%22%0A%20%20%20%20%20%20%20%20f%22%7C%20members%20%7C%20%60length(mem_fished)%60%20%3D%20%7Blen(fished)%3A%2C%7D%20%7C%20%60fished.size%60%20%3D%20%7Bfished.size%3A%2C%7D%20%7C%20%60pl.len()%60%20%3D%20%7Bfished_pl.height%3A%2C%7D%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20f%22%7C%20answers%20%7C%20%60sum(!is.na(mem_fished))%60%20%7C%20%60fished.count()%60%20%3D%20%7Bfished.count()%3A%2C%7D%20%7C%20%60pl.col('mem_fished').count()%60%20%3D%20%7Bfished_pl%5B'mem_fished'%5D.count()%3A%2C%7D%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20f%22%7C%20share%20who%20fished%20%7C%20%60mean(mem_fished%20%3D%3D%201)%60%20is%20%60NA%60%3B%20with%20%60na.rm%20%3D%20TRUE%60%20%7C%20%60(fished%20%3D%3D%201).mean()%60%20%3D%20%7B(fished%20%3D%3D%201).mean()%3A.4f%7D%20%7C%20%60(pl.col('mem_fished')%20%3D%3D%201).mean()%60%20%3D%20%7Bfished_pl.select((pl.col('mem_fished')%20%3D%3D%201).mean()).item()%3A.4f%7D%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20f%22%7C%20the%20same%2C%20missing%20kept%20%7C%20%60mean(mem_fished%20%3D%3D%201)%60%20%3D%20%60NA%60%20%7C%20%60(fished%20%3D%3D%201).mean(skipna%3DFalse)%60%20%3D%20%60%7B(fished%20%3D%3D%201).mean(skipna%3DFalse)%7D%60%20%7C%20no%20option%3A%20test%20%60null_count()%60%20first%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20f%22%7C%20keep%20rows%20where%20it%20is%201%20%7C%20%60filter(m%2C%20mem_fished%20%3D%3D%201)%60%3A%20missing%20rows%20dropped%20%7C%20%60members%5Bfished%20%3D%3D%201%5D%60%3A%20%7Blen(members%5Bfished%20%3D%3D%201%5D)%3A%2C%7D%20rows%2C%20missing%20dropped%20%7C%20%60filter(pl.col('mem_fished')%20%3D%3D%201)%60%3A%20missing%20dropped%20%7C%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20share%20is%20right%20here%2C%20since%20the%20question%20is%20not%20asked%20of%20the%20young%3A%20leaving%20them%20out%20is%20what%20the%20survey%20means.%20Where%20a%20missing%20value%20is%20a%20missing%20answer%2C%20the%20same%20default%20hides%20it.%20Count%20the%20missing%20values%20first%20(%60.isna().sum()%60)%2C%20as%20module%202%20does%20on%20every%20table%20it%20reads.%0A%0A%20%20%20%20**Factors%20against%20categoricals.**%20R's%20%60factor(mem_sex%2C%20levels%20%3D%20c(1%2C%202)%2C%20labels%20%3D%20c(%22Male%22%2C%20%22Female%22))%60%20turns%20any%20value%20outside%20the%20levels%20into%20%60NA%60%20without%20a%20word%3A%20the%20survey's%20invalid%20sex%20codes%20(module%204)%20would%20vanish%20into%20the%20missing%20ones.%20pandas'%20%60Categorical%60%20does%20the%20same%2C%20and%20since%20pandas%203.1%20warns%20that%20it%20will%20refuse%20in%20a%20future%20version.%20polars'%20%60Enum%60%20refuses%20now.%20Two%20more%20differences%3A%20a%20category's%20code%20counts%20from%200%20(%60.cat.codes%60)%2C%20where%20%60as.integer(factor)%60%20counts%20from%201%2C%20and%20a%20missing%20value's%20code%20is%20-1.%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%20invalid_sex%20%3D%20members.loc%5B~members%5B%22mem_sex%22%5D.isin(%5B1%2C%202%5D)%2C%20%22mem_sex%22%5D.head(2).tolist()%0A%20%20%20%20sample_codes%20%3D%20pd.Series(%5B1%2C%202%5D%20%2B%20invalid_sex)%0A%20%20%20%20labels%20%3D%20sample_codes.astype(str).replace(%7B%221%22%3A%20%22Male%22%2C%20%222%22%3A%20%22Female%22%7D)%20%20%23%20valid%20codes%20as%20labels%2C%20the%20rest%20as%20they%20are%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22Sex%20codes%20from%20the%20members%20table%2C%20%7Blen(invalid_sex)%7D%20of%20them%20invalid%3A%20%60%7Bsample_codes.tolist()%7D%60.%20As%20a%20pandas%20category%20with%20the%20levels%20Male%20and%20Female%3A%22)%2C%0A%20%20%20%20%20%20%20%20show(lambda%3A%20pd.Series(pd.Categorical(labels%2C%20categories%3D%5B%22Male%22%2C%20%22Female%22%5D)))%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20codes%20behind%20it%2C%20counted%20from%200%20(-1%20for%20missing)%3A%22)%2C%0A%20%20%20%20%20%20%20%20show(lambda%3A%20pd.Categorical(labels.where(labels.isin(%5B%22Male%22%2C%20%22Female%22%5D))%2C%20categories%3D%5B%22Male%22%2C%20%22Female%22%5D).codes.tolist())%2C%0A%20%20%20%20%20%20%20%20mo.md(%22As%20a%20polars%20%60Enum%60%3A%22)%2C%0A%20%20%20%20%20%20%20%20show(lambda%3A%20pl.Series(labels.tolist()).cast(pl.Enum(%5B%22Male%22%2C%20%22Female%22%5D)))%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20two_codes()%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Three%20household%20codes%20with%20the%20row%20labels%207%2C%208%20and%209%2C%20the%20second%20missing%2C%20used%20by%20the%20exercise's%20example%20and%20its%0A%20%20%20%20check.%22%22%22%0A%20%20%20%20return%20pd.Series(%5B%220103310008001%22%2C%20None%2C%20%220701244017013%22%5D%2C%20index%3D%5B7%2C%208%2C%209%5D%2C%20dtype%3D%22str%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%20What%20to%20write%3A%20count%20the%20codes%20against%20the%20valid%20list%20**before**%20you%20make%20the%20category%20(module%204's%20frequency%20tables)%2C%20then%20map%20the%20valid%20codes%20to%20labels%20and%20give%20the%20rest%20the%20%22Invalid%20value%22%20label%20of%20part%204%2C%20as%20a%20category%20of%20its%20own.%0A%0A%20%20%20%20%23%23%23%20Exercise%20P1.3.%20A%20household%20code%20into%20its%20area%20codes%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20survey's%20%60hh_code%60%20is%20the%20area%20codes%20and%20a%20serial%20number%20written%20one%20after%20another%3A%20two%20digits%20of%20region%2C%20three%20of%20province%2C%20two%20of%20city%20or%20municipality%2C%20three%20of%20barangay%2C%20then%20three%20for%20the%20household's%20number%20in%20its%20barangay.%20An%20R%20script%20splits%20it%20with%20%60substr%60%2C%20counting%20from%201%20and%20including%20both%20ends.%20Translated%20one%20for%20one%2C%20every%20part%20comes%20out%20shifted%20by%20one%20character%2C%20with%20no%20error.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20column%20of%20household%20codes%20(text)%20%E2%86%92%20a%20table%20with%20the%20same%20row%20labels%20and%20five%20text%20columns%2C%20%60region_code%60%2C%20%60province_code%60%2C%20%60citymun_code%60%2C%20%60barangay_code%60%20and%20%60serial%60%2C%20the%20zeros%20kept.%20A%20missing%20household%20code%20gives%20five%20missing%20parts.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_codes%20%3D%20two_codes()%0A%20%20%20%20_rows%20%3D%20%22%5Cn%22.join(%0A%20%20%20%20%20%20%20%20f%22%7C%20%7Blabel%7D%20%7C%20%60%5C%22%7Bcode%7D%5C%22%60%20%7C%20%60%5C%22%7Bcode%5B0%3A2%5D%7D%5C%22%60%20%7C%20%60%5C%22%7Bcode%5B2%3A5%5D%7D%5C%22%60%20%7C%20%60%5C%22%7Bcode%5B5%3A7%5D%7D%5C%22%60%20%7C%20%60%5C%22%7Bcode%5B7%3A10%5D%7D%5C%22%60%20%7C%20%60%5C%22%7Bcode%5B10%3A13%5D%7D%5C%22%60%20%7C%22%20if%20isinstance(code%2C%20str)%0A%20%20%20%20%20%20%20%20else%20f%22%7C%20%7Blabel%7D%20%7C%20missing%20%7C%20missing%20%7C%20missing%20%7C%20missing%20%7C%20missing%20%7C%20missing%20%7C%22%0A%20%20%20%20%20%20%20%20for%20label%2C%20code%20in%20_codes.items()%0A%20%20%20%20)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%7C%20row%20label%20%7C%20hh_code%20%7C%20region_code%20%7C%20province_code%20%7C%20citymun_code%20%7C%20barangay_code%20%7C%20serial%20%7C%5Cn%7C--%3A%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%5Cn%22%20%2B%20_rows%20%2B%20%22%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C%20part%20%7C%20R%20%7C%20positions%20in%20Python%20%7C%5Cn%7C%3A--%7C%3A--%7C%3A--%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C%20region%20%7C%20%60substr(hh_code%2C%201%2C%202)%60%20%7C%20%3F%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C%20province%20%7C%20%60substr(hh_code%2C%203%2C%205)%60%20%7C%20%60%5B2%3A5%5D%60%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C%20city%20or%20municipality%20%7C%20%60substr(hh_code%2C%206%2C%207)%60%20%7C%20%3F%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C%20barangay%20%7C%20%60substr(hh_code%2C%208%2C%2010)%60%20%7C%20%3F%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C%20serial%20%7C%20%60substr(hh_code%2C%2011%2C%2013)%60%20%7C%20%3F%20%7C%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%22**Tools%3A**%20%60.str%5Bstart%3Aend%5D%60%20on%20a%20column%20of%20text%20(part%209%20above)%3B%20%60pd.DataFrame(%7B...%7D)%60%20from%20five%20columns%2C%20which%20keeps%20their%20row%20labels.%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20hh_code_parts(hh_codes%3A%20pd.Series)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20area%20codes%20and%20serial%20number%20in%20each%20household%20code%2C%20as%20text%3A%20region_code%2C%20province_code%2C%20citymun_code%2C%0A%20%20%20%20barangay_code%2C%20serial.%22%22%22%0A%20%20%20%20%23%20Exercise%20P1.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20for%20each%20part%2C%20its%20positions%20in%20Python%3A%20R's%20substr(x%2C%20a%2C%20b)%20is%20%5Ba%20-%201%3Ab%5D%0A%20%20%20%20%23%20Step%202%3A%20cut%20each%20part%20out%20of%20the%20column%20with%20.str%5Bstart%3Aend%5D%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20five%20parts%20as%20a%20table%3A%20pd.DataFrame(%7B%22region_code%22%3A%20...%2C%20...%7D)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20P1.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20P1.3%22%2C%20hh_code_parts%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20first%20300%20household%20codes%22%2C%20(households%5B%22hh_code%22%5D.head(300)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20household%20codes%2C%20one%20of%20them%20missing%2C%20with%20row%20labels%207%2C%208%20and%209%22%2C%20(two_codes()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20household%20codes%22%2C%20(households.loc%5Bhouseholds%5B%22region_code%22%5D%20%3D%3D%20%2207%22%2C%20%22hh_code%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%22columns%20of%20household%20codes%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22R's%20substr(x%2C%20a%2C%20b)%20is%20.str%5Ba%20-%201%3Ab%5D%20in%20Python%3A%20the%20region%20is%20.str%5B0%3A2%5D%2C%20the%20province%20.str%5B2%3A5%5D.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20first%20five%20household%20codes.%20Compare%20each%20part%20with%20the%20area%20code%20columns%20of%20households.%0A%20%20%20%20try_it(hh_code_parts%2C%20households%5B%22hh_code%22%5D.head(5))%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%20P1.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%20%60%221%22%60%20for%20the%20region%20and%20%60%2233%22%60%20for%20the%20province%20%7C%20R's%20starts%20kept%3A%20%60%5B1%3A2%5D%60%2C%20%60%5B3%3A5%5D%60.%20%7C%20step%201%3A%20subtract%201%20from%20each%20start%20only%20%7C%0A%20%20%20%20%7C%20%60%2201%22%60%2C%20then%20%60%220331%22%60%20for%20the%20province%20%7C%20R's%20ends%20moved%20down%20too%2C%20or%20each%20end%20raised%20by%20one.%20%7C%20step%201%3A%20the%20end%20stays%20as%20in%20R%20%7C%0A%20%20%20%20%7C%20%601%60%20and%20%6033%60%2C%20numbers%20%7C%20The%20parts%20turned%20into%20numbers.%20%7C%20keep%20them%20as%20text%3A%20no%20%60astype(int)%60%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20Can%20only%20use%20.str%20accessor%20with%20string%20values*%20%7C%20A%20single%20household%20code%20was%20passed%2C%20or%20a%20column%20of%20numbers.%20%7C%20the%20function%20takes%20the%20whole%20column%2C%20as%20text%20%7C%0A%20%20%20%20%7C%20Row%20labels%200%2C%201%2C%202%20for%20the%20second%20case%20%7C%20The%20parts%20were%20made%20into%20lists%20(%60.tolist()%60)%20or%20the%20index%20reset.%20%7C%20keep%20each%20%60.str%5B...%5D%60%20result%20as%20a%20column%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Your%20function%20against%20the%20area%20code%20columns%20the%20households%20table%20already%20has%0A%20%20%20%20_parts%20%3D%20attempt(hh_code_parts%2C%20households%5B%22hh_code%22%5D)%0A%20%20%20%20mo.stop(_parts%20is%20NOT_YET%2C%20waiting_for(%22Exercise%20P1.3%22))%0A%20%20%20%20_agree%20%3D%20%7Bcode%3A%20int((_parts%5Bcode%5D%20%3D%3D%20households%5Bcode%5D).sum())%20for%20code%20in%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22citymun_code%22%2C%20%22barangay_code%22%5D%7D%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22Of%20**%7Blen(households)%3A%2C%7D**%20households%2C%20the%20parts%20agree%20with%20the%20area%20code%20columns%20in%20this%20many%20rows%3A%20%22%0A%20%20%20%20%20%20%20%20%2B%20%22%2C%20%22.join(f%22%60%7Bcode%7D%60%20%7Bcount%3A%2C%7D%22%20for%20code%2C%20count%20in%20_agree.items())%0A%20%20%20%20%20%20%20%20%2B%20%22.%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20from%20R%20%7C%20you%20can%20now%20write%2C%20in%20Python%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60filter%60%2C%20%60select%60%2C%20%60mutate%60%2C%20%60arrange%60%2C%20%60summarise%60%2C%20%60n_distinct%60%20%7C%20the%20same%20steps%20in%20pandas%20and%20in%20polars%2C%20as%20a%20method%20chain%20%7C%0A%20%20%20%20%7C%20%60paste0%60%20keys%2C%20%60inner_join%60%2C%20%60anti_join%60%2C%20%60full_join%60%20%7C%20joins%20on%20keys%20of%20several%20parts%2C%20with%20%60validate%3D%60%20and%20%60indicator%3DTrue%60%20%7C%0A%20%20%20%20%7C%20%60case_when%60%2C%20%60pivot_wider%60%2C%20%60pivot_longer%60%20%7C%20%60np.select%60%20and%20%60pl.when%60%3B%20%60pivot_table%60%2C%20%60melt%60%2C%20%60pivot%60%2C%20%60unpivot%60%20%7C%0A%20%20%20%20%7C%20DBI%20and%20RMySQL%20in%20a%20loop%20%7C%20one%20DuckDB%20file%2C%20or%20every%20region%20attached%20and%20read%20in%20one%20query%20%7C%0A%20%20%20%20%7C%20%60svydesign%60%2C%20%60svytotal%60%2C%20%60svymean%60%2C%20%60svyby%60%3B%20a%20design%20whose%20every%20weight%20is%201%20%7C%20the%20svy%20calls%2C%20the%20same%20estimates%20in%20pandas%20without%20svy%2C%20and%20the%20sample's%20own%20counts%20with%20one%20%60groupby%60%20%7C%0A%20%20%20%20%7C%20%60saveRDS%60%2C%20%60writexl%60%2C%20Quarto%20%7C%20Parquet%2C%20%60ExcelWriter%60%2C%20a%20marimo%20notebook%20%7C%0A%20%20%20%20%7C%20%60substr%60%2C%20%60NA%60%2C%20%60factor%60%20%7C%200-based%20slices%2C%20missing%20values%20counted%20before%20they%20are%20skipped%2C%20categories%20that%20keep%20invalid%20codes%20in%20sight%20%7C%0A%0A%20%20%20%20**Next%3A**%20primer%20%5BP2%2C%20survey%20sampling%20in%20one%20page%5D(%2F%3Ffile%3D96_primer_survey_sampling.py)%20writes%20the%20standard%20error%20of%20a%20stratified%20total%20in%20a%20few%20lines%20of%20numpy%2C%20beside%20svy's.%20Module%201%20starts%20the%20course%3A%20%5BStart%20here%5D(%2F%3Ffile%3D00_start.py).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
6d0d220b86d7ac5612453ae98b25f31a