import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Start%20here%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20every%20other%20cell%20uses.%0A%20%20%20%20import%20altair%20as%20alt%20%20%23%20charts%20described%20by%20encodings%2C%20drawn%20by%20your%20browser%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%20%20%23%20charts%20drawn%20on%20a%20figure%20and%20its%20axes%0A%20%20%20%20import%20pandas%20as%20pd%20%20%23%20tables%3B%20module%203%20teaches%20it%0A%20%20%20%20import%20seaborn%20as%20sns%20%20%23%20charts%20from%20a%20table%20and%20its%20column%20names%2C%20drawn%20with%20matplotlib%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20where%20the%20files%20are%2C%20the%20readiness%20check%2C%20the%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Start%20here%0A%0A%20%20%20%20Welcome%20to%20**Arrays%20%26%20DataFrames**.%20In%20Python%20Essentials%20you%20worked%20with%20Python's%20own%20values%3A%20numbers%2C%20texts%2C%20lists%20and%20dictionaries%2C%20one%20at%20a%20time%20or%20in%20a%20loop.%20This%20course%20works%20with%20**a%20whole%20dataset%20in%20memory**%2C%20as%20an%20analyst%20does%3A%20you%20read%20it%20with%20the%20right%20types%2C%20clean%20it%2C%20reshape%20and%20combine%20it%2C%20summarize%20it%20and%20chart%20it.%20It%20uses%20the%20libraries%20made%20for%20that%20work%3A%0A%0A%20%20%20%20%7C%20library%20%7C%20what%20it%20is%20for%20%7C%20where%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20NumPy%20%7C%20arrays%3A%20many%20numbers%20of%20one%20type%2C%20computed%20all%20at%20once%20%7C%20module%202%20%7C%0A%20%20%20%20%7C%20pandas%203%20%7C%20tables%20(DataFrames)%20with%20named%20columns%20%7C%20modules%203%20to%205%20%7C%0A%20%20%20%20%7C%20polars%20%7C%20the%20same%20table%20work%2C%20written%20as%20expressions%20%7C%20module%206%20%7C%0A%20%20%20%20%7C%20matplotlib%2C%20seaborn%2C%20Altair%20%7C%20charts%20%7C%20this%20module%2C%20then%20throughout%3B%20module%207%20chooses%20between%20them%20%7C%0A%0A%20%20%20%20Every%20notebook%20is%20a%20**marimo**%20notebook%2C%20like%20this%20one.%0A%0A%20%20%20%20**One%20running%20case.**%20A%20farm%20survey%20runs%20through%20the%20whole%20course.%20From%20October%20to%20December%202025%2C%20170%20farm%20households%20were%20interviewed%20in%20each%20of%20seven%20provinces%3A%20Ilocos%20Norte%2C%20Isabela%2C%20Nueva%20Ecija%2C%20Camarines%20Sur%2C%20Iloilo%2C%20Bukidnon%20and%20Cotabato.%20They%20were%20asked%20about%20their%20household%20and%20about%20every%20plot%20they%20farm%3A%20its%20crop%2C%20its%20area%2C%20its%20harvest%20and%20the%20fertilizer%20it%20got.%20The%20area%20codes%20and%20place%20names%20are%20**real**%2C%20from%20the%20Philippine%20Statistics%20Authority's%20(PSA)%20Philippine%20Standard%20Geographic%20Code%20(PSGC)%3B%20the%20households%2C%20plots%20and%20weights%20are%20**invented**%20for%20teaching.%20Monthly%20**farmgate%20prices**%20of%20palay%2C%20yellow%20corn%2C%20tomato%20and%20eggplant%20in%20the%20same%20provinces%2C%202022%20to%202025%2C%20carry%20the%20introduction%20to%20time%20series%20in%20module%205.%0A%0A%20%20%20%20**How%20the%20notebooks%20work.**%20Work%20from%20top%20to%20bottom.%20Each%20notebook%20mixes%20explanation%20and%20exercises%3A%0A%0A%20%20%20%20%7C%20part%20%7C%20what%20it%20is%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20explainers%20%7C%20controls%20such%20as%20a%20dropdown%20or%20a%20button%3A%20what%20is%20under%20them%20changes%20as%20you%20use%20them%20%7C%0A%20%20%20%20%7C%20an%20exercise%20%7C%20a%20function%20whose%20name%2C%20inputs%20and%20steps%20are%20written%20for%20you%2C%20ending%20in%20a%20%60raise%20NotImplementedError(...)%60%20line%3A%20write%20your%20code%2C%20delete%20that%20line%20and%20run%20the%20cell%20%7C%0A%20%20%20%20%7C%20its%20check%20%7C%20the%20box%20under%20the%20function.%20It%20runs%20your%20function%20on%20several%20inputs%20and%20compares%20each%20result%20with%20a%20correct%20one%3A%20**green**%20when%20all%20match%3B%20**red**%20with%20the%20first%20input%20that%20does%20not%2C%20and%20what%20your%20function%20gave%3B%20**grey**%20while%20it%20is%20not%20written%20%7C%0A%20%20%20%20%7C%20Try%20it%20%7C%20a%20cell%20that%20calls%20your%20function%20on%20the%20real%20data%20and%20shows%20what%20it%20gives%20back%2C%20or%20what%20its%20error%20means%20%7C%0A%20%20%20%20%7C%20common%20causes%20%7C%20a%20folded%20table%20under%20each%20check%3A%20what%20you%20see%2C%20the%20usual%20cause%20and%20where%20to%20look%20%7C%0A%20%20%20%20%7C%20the%20companion%20%7C%20each%20exercise's%20solution%20and%20why%20it%20works%2C%20on%20the%20Learn%20Python%20site.%20The%20check%20links%20to%20it%20when%20it%20is%20green%2C%20and%20when%20you%20are%20stuck%20%7C%0A%0A%20%20%20%20**New%20to%20marimo%3F**%20If%20you%20did%20not%20take%20Python%20Essentials%2C%20or%20have%20not%20met%20marimo%20in%20its%20module%207%2C%20read%20**primer%20P1**%20first%3A%20%5B90_primer_marimo.py%5D(%2F%3Ffile%3D90_primer_marimo.py).%20It%20shows%20how%20a%20marimo%20notebook%20runs%2C%20how%20the%20exercises%20work%2C%20the%20Python%20this%20course%20assumes%20and%20how%20to%20read%20an%20error.%0A%0A%20%20%20%20This%20module%20has%20three%20sections%3A%20get%20your%20laptop%20and%20the%20course%20folder%20ready%3B%20meet%20the%20farm%20survey%20through%20its%20codebook%3B%20and%20draw%20a%20first%20chart%20three%20ways.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%221.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Your%20laptop%20and%20the%20course%20folder%20ready%0A%0A%20%20%20%20Nothing%20in%20the%20course%20works%20until%20two%20things%20are%20true%3A%20the%20Python%20that%20uv%20installed%20for%20the%20course%20has%20every%20package%20the%20notebooks%20import%2C%20and%20the%20course%20folder%20is%20complete%20and%20somewhere%20you%20can%20save%20files.%20The%20check%20below%20tests%20both%2C%20one%20line%20each.%20If%20a%20line%20says%20**no**%2C%20its%20last%20column%20says%20what%20to%20do%3A%20do%20it%2C%20then%20press%20**Check%20again**.%20If%20a%20line%20still%20says%20no%2C%20raise%20your%20hand%3B%20on%20your%20own%2C%20go%20through%20%60SETUP.md%60%20in%20the%20course%20folder%20again.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20recheck%20%3D%20mo.ui.run_button(label%3D%22Check%20again%22)%0A%20%20%20%20recheck%0A%20%20%20%20return%20(recheck%2C)%0A%0A%0A%40app.cell%0Adef%20_(recheck)%3A%0A%20%20%20%20%23%20The%20readiness%20check.%20Pressing%20%22Check%20again%22%20runs%20this%20cell%20again%2C%20because%20it%20uses%20%60recheck%60.%0A%20%20%20%20recheck%0A%20%20%20%20course.readiness()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20What%20each%20line%20checks%3A%0A%0A%20%20%20%20%7C%20line%20%7C%20what%20it%20checks%20%7C%20why%20the%20course%20needs%20it%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Python%203.12%20or%20newer%20%7C%20the%20Python%20that%20runs%20the%20notebooks.%20uv%20installs%20it%20for%20the%20course%20(3.14)%2C%20apart%20from%20any%20Python%20already%20on%20your%20laptop%20%7C%20the%20course's%20packages%20need%20a%20recent%20Python%20%7C%0A%20%20%20%20%7C%20the%20marimo%20package%20%7C%20marimo%20itself%20%7C%20the%20notebooks%20%7C%0A%20%20%20%20%7C%20numpy%2C%20pandas%2C%20polars%20%7C%20the%20three%20libraries%20for%20arrays%20and%20tables%20%7C%20modules%202%20to%206%20%7C%0A%20%20%20%20%7C%20pyarrow%20%7C%20Apache%20Arrow%2C%20a%20way%20of%20storing%20columns%20in%20memory%20%7C%20pandas%203%20stores%20text%20columns%20with%20it%3B%20Parquet%20files%20are%20read%20and%20written%20with%20it%20%7C%0A%20%20%20%20%7C%20openpyxl%20%7C%20reading%20and%20writing%20Excel%20workbooks%20%7C%20the%20survey%20also%20comes%20as%20a%20workbook%20%7C%0A%20%20%20%20%7C%20matplotlib%2C%20seaborn%2C%20altair%20%7C%20the%20three%20chart%20libraries%20%7C%20section%203%2C%20and%20charts%20throughout%20%7C%0A%20%20%20%20%7C%20vegafusion%2C%20vl_convert%20%7C%20Altair's%20helpers%3A%20vegafusion%20computes%20the%20data%20behind%20an%20Altair%20chart%20in%20Python%3B%20vl_convert%20saves%20an%20Altair%20chart%20as%20an%20image%20%7C%20the%20checks%20read%20Altair%20charts%3B%20module%207%20saves%20charts%20%7C%0A%20%20%20%20%7C%20the%20course%20folder%20%7C%20that%20%60modules.toml%60%2C%20%60lesson.py%60%20and%20%60checks.json%60%20are%20there%20%7C%20the%20agenda%20reads%20the%20first%3B%20the%20exercise%20checks%20read%20the%20other%20two%20%7C%0A%20%20%20%20%7C%20the%20course%20can%20save%20files%20here%20%7C%20writes%20a%20small%20file%20into%20the%20course%20folder%20and%20deletes%20it%20%7C%20module%203%20writes%20CSV%2C%20Excel%20and%20Parquet%20files%20%7C%0A%0A%20%20%20%20%23%23%23%20What%20the%20course%20folder%20holds%0A%0A%20%20%20%20%60course.PATHS%60%20knows%20where%20the%20course's%20files%20are%20on%20**your**%20laptop%2C%20wherever%20you%20put%20the%20folder%3A%20%60PATHS.data%60%20is%20the%20data%20folder%2C%20%60PATHS.survey%60%20the%20farm%20survey's%20folder%20inside%20it%2C%20%60PATHS.prices%60%20the%20price%20files'%20folder%2C%20and%20%60PATHS.code_list%60%20the%20PSGC%20file.%20Every%20notebook%20finds%20its%20files%20through%20it.%20The%20data%20folder%20holds%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20folder_table()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20files%20in%20the%20course's%20data%20folder%2C%20as%20a%20Markdown%20table%3A%20one%20row%20each%2C%20the%20price%20files%20in%20one%20row.%22%22%22%0A%20%20%20%20about%20%3D%20%7B%0A%20%20%20%20%20%20%20%20PATHS.survey%20%2F%20%22households.csv%22%3A%20%22one%20row%20per%20household%20interviewed%22%2C%0A%20%20%20%20%20%20%20%20PATHS.survey%20%2F%20%22plots.csv%22%3A%20%22one%20row%20per%20plot%20those%20households%20farm%22%2C%0A%20%20%20%20%20%20%20%20PATHS.survey%20%2F%20%22codebook.csv%22%3A%20%22what%20every%20column%20of%20the%20two%20tables%20means%3A%20its%20codes%20and%20its%20unit%20(section%202)%22%2C%0A%20%20%20%20%20%20%20%20PATHS.survey%20%2F%20%22farm_survey_2025.xlsx%22%3A%20%22the%20same%20survey%20as%20an%20Excel%20workbook%2C%20a%20sheet%20per%20table%20(module%203%20reads%20it)%22%2C%0A%20%20%20%20%20%20%20%20PATHS.code_list%3A%20%22the%20seven%20provinces'%20cities%20and%20municipalities%20with%20their%20PSGC%20codes%2C%20as%20PSA%20publishes%20them%22%2C%0A%20%20%20%20%20%20%20%20PATHS.data%20%2F%20%22README.md%22%3A%20%22where%20the%20data%20comes%20from%2C%20and%20which%20of%20it%20is%20real%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20path%20in%20sorted(PATHS.data.rglob(%22*%22))%3A%0A%20%20%20%20%20%20%20%20if%20path.is_file()%20and%20path.parent%20!%3D%20PATHS.prices%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(f%22%7C%20%60%7Bpath.relative_to(PATHS.root).as_posix()%7D%60%20%7C%20%7Bpath.stat().st_size%20%2F%201024%3A%2C.0f%7D%20KB%20%7C%20%7Babout.get(path%2C%20'')%7D%20%7C%22)%0A%20%20%20%20prices%20%3D%20sorted(PATHS.prices.glob(%22farmgate_*.csv%22))%0A%20%20%20%20if%20prices%3A%0A%20%20%20%20%20%20%20%20size%20%3D%20sum(p.stat().st_size%20for%20p%20in%20prices)%20%2F%201024%0A%20%20%20%20%20%20%20%20rows.append(f%22%7C%20%60%7BPATHS.prices.relative_to(PATHS.root).as_posix()%7D%2F%60%20%7C%20%7Bsize%3A%2C.0f%7D%20KB%20in%20all%20%7C%20%7Blen(prices)%7D%20files%20of%20monthly%20farmgate%20prices%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%60%7Bprices%5B0%5D.name%7D%60%20to%20%60%7Bprices%5B-1%5D.name%7D%60%20(module%205%20asks%20whether%20that%20is%20every%20month)%20%7C%22)%0A%20%20%20%20return%20%22%7C%20file%20%7C%20size%20%7C%20what%20it%20holds%20%7C%5Cn%7C%3A--%7C--%3A%7C%3A--%7C%5Cn%22%20%2B%20%22%5Cn%22.join(rows)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_notebooks%20%3D%20sorted(p.name%20for%20p%20in%20PATHS.root.glob(%22%5B0-9%5D*.py%22))%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20folder_table()%0A%20%20%20%20%20%20%20%20%2B%20%22%5Cn%5CnBeside%20the%20data%20folder%20are%20the%20notebooks%2C%20numbered%20in%20the%20order%20you%20use%20them%20(%22%0A%20%20%20%20%20%20%20%20%2B%20%22%2C%20%22.join(f%22%60%7Bn%7D%60%22%20for%20n%20in%20_notebooks)%0A%20%20%20%20%20%20%20%20%2B%20%22)%3B%20%60checks.json%60%2C%20what%20the%20exercise%20checks%20compare%20your%20results%20with%2C%20stored%20so%20that%20the%20answers%20cannot%20be%20read%20from%20it%3B%20and%20%60course.py%60%20and%20%60lesson.py%60%2C%20the%20helpers%20the%20notebooks%20share%2C%20which%20you%20do%20not%20need%20to%20open.%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20PATHS.survey%20%20%23%20the%20farm%20survey's%20folder%2C%20on%20this%20laptop%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20The%20farm%20survey%20and%20its%20codebook%0A%0A%20%20%20%20Before%20you%20compute%20anything%20from%20a%20dataset%2C%20you%20need%20five%20answers.%20What%20is%20one%20row%3F%20What%20does%20each%20column%20hold%3F%20Which%20numbers%20are%20**codes**%20rather%20than%20amounts%3F%20In%20what%20**unit**%20is%20each%20measure%3F%20Does%20every%20row%20count%20the%20same%3F%20Get%20one%20of%20them%20wrong%20and%20every%20result%20after%20it%20is%20wrong%2C%20and%20no%20error%20message%20tells%20you.%20The%20answers%20are%20in%20the%20dataset's%20**codebook**%3A%20one%20row%20per%20variable%2C%20written%20by%20the%20people%20who%20collected%20the%20data.%20Here%20is%20the%20farm%20survey's%2C%20read%20from%20%60codebook.csv%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20codebook%20%3D%20pd.read_csv(PATHS.survey%20%2F%20%22codebook.csv%22)%20%20%23%20read%20the%20CSV%20file%20into%20a%20table%3B%20module%203%20teaches%20read_csv%0A%20%20%20%20codebook%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%20Each%20row%20describes%20one%20**variable**%20(one%20column%20of%20a%20data%20file)%3A%0A%0A%20%20%20%20%7C%20codebook%20column%20%7C%20what%20it%20says%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60file%60%20%7C%20which%20data%20file%20the%20variable%20is%20in%20%7C%0A%20%20%20%20%7C%20%60variable%60%20%7C%20the%20column's%20name%20in%20that%20file%2C%20exactly%20as%20you%20type%20it%20in%20code%20%7C%0A%20%20%20%20%7C%20%60label%60%20%7C%20what%20the%20variable%20means%2C%20in%20words%20%7C%0A%20%20%20%20%7C%20%60type%60%20%7C%20what%20kind%20of%20value%20it%20holds%3A%20text%2C%20a%20date%2C%20a%20whole%20number%2C%20a%20decimal%20or%20a%20**code**%20%7C%0A%20%20%20%20%7C%20%60values%60%20%7C%20for%20a%20code%2C%20what%20each%20value%20stands%20for%3B%20for%20some%20other%20variables%2C%20how%20the%20values%20are%20written%20%7C%0A%20%20%20%20%7C%20%60unit%60%20%7C%20the%20unit%20of%20a%20measure%3A%20persons%2C%20years%2C%20kilograms%20%7C%0A%0A%20%20%20%20A%20cell%20with%20nothing%20in%20it%20(a%20variable%20that%20has%20no%20codes%2C%20or%20no%20unit)%20shows%20as%20missing.%20pandas%20marks%20a%20missing%20value%20as%20%60NaN%60%3B%20module%205%20is%20about%20missing%20values.%0A%0A%20%20%20%20%23%23%23%20One%20row%20is%20one%20what%3F%20The%20unit%20of%20observation%0A%0A%20%20%20%20The%20**unit%20of%20observation**%20is%20what%20one%20row%20describes.%20In%20%60households.csv%60%20it%20is%20one%20**household**%20interviewed%3B%20in%20%60plots.csv%60%20it%20is%20one%20**plot**%20of%20land%20a%20household%20farms.%20A%20household%20with%20three%20plots%20is%20one%20row%20of%20households%20and%20three%20rows%20of%20plots%2C%20and%20%60hh_id%60%20(the%20household%20ID)%20links%20them.%20Here%20are%20the%20first%20rows%20of%20each.%20%60course.households()%60%20and%20%60course.plots()%60%20read%20the%20two%20files%20for%20you%2C%20with%20the%20right%20types%3B%20module%203%20shows%20how%20to%20read%20them%20yourself%2C%20and%20what%20goes%20wrong%20when%20the%20types%20are%20not%20given.%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%20households%20%3D%20course.households(clean%3DFalse)%20%20%23%20households.csv%2C%20every%20row%20as%20it%20is%20in%20the%20file%0A%20%20%20%20plots%20%3D%20course.plots(clean%3DFalse)%20%20%23%20plots.csv%2C%20the%20same%20way%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**households**%3A%20%7Blen(households)%3A%2C%7D%20rows%2C%20%7Bhouseholds.shape%5B1%5D%7D%20columns.%20The%20first%20five%3A%22)%2C%20households.head()%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**plots**%3A%20%7Blen(plots)%3A%2C%7D%20rows%2C%20%7Bplots.shape%5B1%5D%7D%20columns.%20The%20first%20five%3A%22)%2C%20plots.head()%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20households%2C%20plots%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Pick%20a%20household%20to%20see%20its%20one%20row%20in%20households%20and%20its%20rows%20in%20plots%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(households)%3A%0A%20%20%20%20pick%20%3D%20mo.ui.dropdown(options%3Dhouseholds%5B%22hh_id%22%5D.unique().tolist()%2C%20value%3D%22HH-0001%22%2C%20searchable%3DTrue%2C%20label%3D%22Household%3A%22)%0A%20%20%20%20pick%0A%20%20%20%20return%20(pick%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(households%2C%20pick%2C%20plots)%3A%0A%20%20%20%20_household%20%3D%20households%5Bhouseholds%5B%22hh_id%22%5D%20%3D%3D%20pick.value%5D%0A%20%20%20%20_plots%20%3D%20plots%5Bplots%5B%22hh_id%22%5D%20%3D%3D%20pick.value%5D%0A%20%20%20%20_rows%20%3D%20%22one%20row%22%20if%20len(_household)%20%3D%3D%201%20else%20f%22**%7Blen(_household)%7D%20rows**%3A%20it%20was%20entered%20twice%2C%20one%20of%20the%20traps%20module%205%20finds%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Bpick.value%7D**%20is%20%7B_rows%7D%20in%20households%3A%22)%2C%20_household%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22and%20**%7Blen(_plots)%7D%20row%7B's'%20if%20len(_plots)%20!%3D%201%20else%20''%7D**%20in%20plots%2C%20one%20per%20plot%20it%20farms%3A%22)%2C%20_plots%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(households)%3A%0A%20%20%20%20_with_code%20%3D%20households%5B%22head_age%22%5D.mean()%0A%20%20%20%20_without%20%3D%20households.loc%5Bhouseholds%5B%22head_age%22%5D%20!%3D%20999%2C%20%22head_age%22%5D.mean()%0A%20%20%20%20_not_stated%20%3D%20int((households%5B%22head_age%22%5D%20%3D%3D%20999).sum())%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20Codes%3A%20numbers%20that%20are%20not%20amounts%0A%0A%20%20%20%20A%20**code**%20is%20a%20value%20that%20stands%20for%20something%20else.%20%60head_sex%60%20holds%20%601%60%2C%20%602%60%20or%20%609%60%2C%20and%20the%20codebook%20says%20what%20each%20means%3A%201%20%3D%20Male%2C%202%20%3D%20Female%2C%209%20%3D%20Not%20stated.%20Nobody%20would%20add%20those%20up.%20The%20trap%20is%20a%20code%20hidden%20among%20real%20amounts%3A%20%60head_age%60%20is%20an%20age%20in%20years%2C%20except%20that%20**999%20means%20not%20stated**.%20Taken%20as%20an%20age%2C%20the%20%7B_not_stated%7D%20heads%20whose%20age%20is%20not%20stated%20pull%20the%20mean%20age%20of%20household%20heads%20from%20**%7B_without%3A.1f%7D**%20years%20to%20**%7B_with_code%3A.1f%7D**%2C%20and%20no%20error%20message%20warns%20you.%20Module%205%20turns%20such%20codes%20into%20missing%20values%3B%20for%20now%2C%20the%20lesson%20is%20to%20read%20the%20%60values%60%20column%20before%20computing.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%201.1.%20The%20codes%20of%20a%20variable%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20codebook%20writes%20its%20codes%20as%20text%2C%20such%20as%20%601%20%3D%20Male%3B%202%20%3D%20Female%3B%209%20%3D%20Not%20stated%60.%20Turned%20into%20a%20dictionary%2C%20they%20can%20label%20a%20table%20or%20a%20chart%2C%20and%20tell%20you%20which%20values%20to%20treat%20as%20missing.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60values%60%20text%20of%20one%20codebook%20row%20%E2%86%92%20a%20dictionary%20of%20each%20code%20and%20what%20it%20means.%0A%0A%20%20%20%20%7C%20%60values%60%20%7C%20returns%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%221%20%3D%20Male%3B%202%20%3D%20Female%3B%209%20%3D%20Not%20stated%22%60%20%7C%20%60%7B%221%22%3A%20%22Male%22%2C%20%222%22%3A%20%22Female%22%2C%20%229%22%3A%20%22Not%20stated%22%7D%60%20%7C%0A%20%20%20%20%7C%20%60%22999%20%3D%20Not%20stated%22%60%20%7C%20%60%7B%22999%22%3A%20%22Not%20stated%22%7D%60%20%7C%0A%20%20%20%20%7C%20%60%22Rice%3B%20Corn%3B%20Vegetables%3B%20Banana%3B%20blank%20%3D%20not%20recorded%22%60%20%7C%20%60%7B%22blank%22%3A%20%22not%20recorded%22%7D%60%20%7C%0A%20%20%20%20%7C%20%60''%60%20(empty%3A%20a%20variable%20with%20no%20codes)%20%7C%20%60%7B%7D%60%20%7C%0A%0A%20%20%20%20The%20parts%20are%20separated%20by%20%60%22%3B%20%22%60%20(a%20semicolon%20and%20a%20space).%20A%20part%20written%20%60code%20%3D%20meaning%60%20becomes%20one%20entry%3B%20a%20part%20without%20%60%22%20%3D%20%22%60%2C%20such%20as%20%60Rice%60%2C%20is%20a%20value%20that%20means%20itself%2C%20and%20is%20left%20out.%20The%20codes%20stay%20text%2C%20as%20written%3A%20%60%221%22%60%2C%20not%20%601%60.%0A%0A%20%20%20%20**Tools**%20(Python%20Essentials%2C%20modules%203%20to%206)%3A%20%60text.split(%22%3B%20%22)%60%20gives%20the%20list%20of%20parts%3B%20%60%22%20%3D%20%22%20in%20part%60%20is%20%60True%60%20when%20the%20part%20holds%20%60%20%3D%20%60%3B%20%60part.split(%22%20%3D%20%22%2C%201)%60%20splits%20at%20the%20**first**%20%60%20%3D%20%60%20only%2C%20into%20two%20pieces%2C%20and%20%60code%2C%20meaning%20%3D%20...%60%20names%20them%3B%20%60meanings%5Bcode%5D%20%3D%20meaning%60%20stores%20an%20entry%20in%20a%20dictionary.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20code_meanings(values%3A%20str)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22The%20codes%20written%20in%20a%20codebook's%20%60values%60%20text%2C%20each%20with%20what%20it%20means.%22%22%22%0A%20%20%20%20%23%20Exercise%201.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20start%20an%20empty%20dictionary%0A%20%20%20%20%23%20Step%202%3A%20go%20through%20the%20parts%20of%20%60values%60%2C%20split%20at%20each%20%22%3B%20%22%0A%20%20%20%20%23%20Step%203%3A%20for%20a%20part%20that%20holds%20%22%20%3D%20%22%2C%20split%20it%20once%20at%20%22%20%3D%20%22%20into%20the%20code%20and%20its%20meaning%2C%20and%20store%20them%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20dictionary%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.1%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%201.1%22%2C%20code_meanings%2C%0A%20%20%20%20%20%20%20%20%5B(f%22code_meanings(%7Bv!r%7D)%22%2C%20(v%2C))%20for%20v%20in%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%221%20%3D%20Male%3B%202%20%3D%20Female%3B%209%20%3D%20Not%20stated%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22999%20%3D%20Not%20stated%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22ha%20%3D%20hectares%3B%20sqm%20%3D%20square%20meters%20(1%20ha%20%3D%2010%2C000%20sqm)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Rice%3B%20Corn%3B%20Vegetables%3B%20Banana%3B%20blank%20%3D%20not%20recorded%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Yes%3B%20No%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%2C%0A%20%20%20%20%20%20%20%20%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22codebook%20texts%22%2C%20hint%3D'Split%20into%20parts%20at%20%22%3B%20%22%2C%20keep%20the%20parts%20that%20hold%20%22%20%3D%20%22%2C%20and%20split%20each%20of%20those%20at%20its%20first%20%22%20%3D%20%22%20only.'%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%20codes%20of%20the%20head%20of%20household's%20sex.%20Change%20the%20text%20and%20run%20the%20cell.%0A%20%20%20%20try_it(code_meanings%2C%20%221%20%3D%20Male%3B%202%20%3D%20Female%3B%209%20%3D%20Not%20stated%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%201.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*ValueError%3A%20too%20many%20values%20to%20unpack%20(expected%202%2C%20got%203)*%20%7C%20The%20part%20%60sqm%20%3D%20square%20meters%20(1%20ha%20%3D%2010%2C000%20sqm)%60%20holds%20two%20%60%20%3D%20%60%3B%20splitting%20at%20both%20gives%20three%20pieces%20for%20two%20names.%20%7C%20%60part.split(%22%20%3D%20%22%2C%201)%60%3A%20split%20at%20the%20first%20only%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20not%20enough%20values%20to%20unpack%20(expected%202%2C%20got%201)*%20%7C%20A%20part%20without%20%60%20%3D%20%60%2C%20such%20as%20%60Rice%60%2C%20was%20split%20too.%20%7C%20%60if%20%22%20%3D%20%22%20in%20part%3A%60%20before%20splitting%20%7C%0A%20%20%20%20%7C%20Codes%20such%20as%20%60%22%202%22%60%2C%20with%20a%20space%20in%20front%20%7C%20The%20text%20was%20split%20at%20%60%22%3B%22%60%20without%20its%20space.%20%7C%20%60values.split(%22%3B%20%22)%60%20%7C%0A%20%20%20%20%7C%20Only%20the%20first%20code%20comes%20back%20%7C%20%60return%60%20is%20inside%20the%20loop%2C%20so%20the%20function%20ends%20after%20the%20first%20part.%20%7C%20move%20%60return%20meanings%60%20out%20of%20the%20loop%2C%20to%20the%20function's%20own%20indentation%20%7C%0A%20%20%20%20%7C%20%60None%60%20%7C%20The%20function%20has%20no%20%60return%60%2C%20or%20prints%20the%20dictionary%20instead.%20%7C%20end%20with%20%60return%20meanings%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.function%0Adef%20codebook_rows()%20-%3E%20list%5Bdict%5D%3A%0A%20%20%20%20%22%22%22The%20codebook%20as%20a%20list%20of%20dictionaries%2C%20one%20per%20variable%2C%20with%20empty%20cells%20as%20empty%20text.%22%22%22%0A%20%20%20%20return%20pd.read_csv(PATHS.survey%20%2F%20%22codebook.csv%22%2C%20keep_default_na%3DFalse).to_dict(%22records%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_found%20%3D%20%7Brow%5B%22variable%22%5D%3A%20attempt(code_meanings%2C%20row%5B%22values%22%5D)%20for%20row%20in%20codebook_rows()%7D%0A%20%20%20%20mo.stop(any(v%20is%20NOT_YET%20for%20v%20in%20_found.values())%2C%20waiting_for(%22Exercise%201.1%22))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Your%20function%20on%20every%20variable%20of%20the%20codebook.**%20The%20variables%20with%20codes%2C%20and%20what%20each%20code%20means%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22%7C%20variable%20%7C%20codes%20%7C%5Cn%7C%3A--%7C%3A--%7C%5Cn%22%20%2B%20%22%5Cn%22.join(f%22%7C%20%60%7Bname%7D%60%20%7C%20%60%7Bcodes%7D%60%20%7C%22%20for%20name%2C%20codes%20in%20_found.items()%20if%20codes))%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(households)%3A%0A%20%20%20%20_code%20%3D%20households.loc%5B0%2C%20%22psgc%22%5D%0A%20%20%20%20_place%20%3D%20course.code_list().set_index(%22code%22).loc%5B_code%5D%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20Codes%20that%20must%20stay%20text%3A%20the%20PSGC%0A%0A%20%20%20%20%60psgc%60%20is%20the%20city%20or%20municipality%20where%20a%20household%20lives%2C%20as%20its%2010-digit%20PSGC%20code.%20The%20digits%20are%20parts%3A%20the%20first%20household's%20code%2C%20%60%7B_code%7D%60%2C%20is%0A%0A%20%20%20%20%7C%20digits%20%7C%20part%20%7C%20here%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20and%202%20%7C%20region%20%7C%20%60%7B_code%5B%3A2%5D%7D%60%3A%20%7B_place%5B%22region%22%5D%7D%20%7C%0A%20%20%20%20%7C%203%20to%205%20%7C%20province%20%7C%20%60%7B_code%5B2%3A5%5D%7D%60%3A%20%7B_place%5B%22province%22%5D%7D%20%7C%0A%20%20%20%20%7C%206%20and%207%20%7C%20city%20or%20municipality%20%7C%20%60%7B_code%5B5%3A7%5D%7D%60%3A%20%7B_place%5B%22name%22%5D%7D%20%7C%0A%20%20%20%20%7C%208%20to%2010%20%7C%20barangay%20%7C%20%60%7B_code%5B7%3A%5D%7D%60%3A%20the%20whole%20municipality%2C%20no%20single%20barangay%20%7C%0A%0A%20%20%20%20The%20codebook%20says%20*10%20digits%3B%20keep%20the%20leading%20zero*%2C%20because%20a%20code%20is%20a%20label%2C%20not%20an%20amount.%20Read%20as%20a%20number%2C%20it%20loses%20its%20first%20zero%2C%20and%20its%20parts%20shift.%20Run%20the%20cell%20below.%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%20psgc_text%20%3D%20%220102820000%22%20%20%23%20San%20Nicolas%2C%20Ilocos%20Norte%2C%20as%20households.csv%20writes%20it%0A%20%20%20%20psgc_number%20%3D%20int(psgc_text)%20%20%23%20what%20a%20program%20that%20reads%20the%20column%20as%20numbers%20keeps%0A%20%20%20%20psgc_text%5B%3A2%5D%2C%20psgc_number%2C%20str(psgc_number)%5B%3A2%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%20As%20text%2C%20the%20region%20is%20%6001%60%2C%20the%20Ilocos%20Region.%20As%20a%20number%20the%20code%20is%20%60102820000%60%2C%20nine%20digits%2C%20and%20its%20first%20two%20digits%20are%20%6010%60%3A%20Region%20X%2C%20Northern%20Mindanao%2C%20where%20Bukidnon%20is.%20Five%20of%20the%20seven%20provinces%20have%20a%20region%20code%20that%20starts%20with%200%2C%20so%20most%20of%20the%20survey's%20codes%20would%20be%20damaged.%20Module%203%20reads%20the%20file%20so%20that%20this%20cannot%20happen.%0A%0A%20%20%20%20%23%23%23%20Exercise%201.2.%20Is%20it%20a%20PSGC%20code%3F%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20code%20that%20went%20through%20a%20number%20column%20or%20was%20typed%20by%20hand%20comes%20back%20damaged%20in%20ways%20that%20are%20easy%20to%20miss%3A%20its%20leading%20zero%20lost%2C%20%60.0%60%20added%20when%20a%20number%20was%20turned%20back%20into%20text%2C%20a%20space%20at%20the%20end%2C%20the%20letter%20O%20typed%20for%20a%20zero.%20A%20test%20that%20a%20code%20is%20still%2010%20digits%20catches%20all%20of%20them%2C%20before%20the%20code%20is%20used%20to%20join%20or%20group.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20code%2C%20as%20text%20%E2%86%92%20%60True%60%20when%20it%20is%20exactly%2010%20characters%2C%20every%20one%20a%20digit%3B%20%60False%60%20otherwise.%0A%0A%20%20%20%20%7C%20%60code%60%20%7C%20returns%20%7C%20why%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%220102820000%22%60%20%7C%20%60True%60%20%7C%2010%20digits%20%7C%0A%20%20%20%20%7C%20%60%22102820000%22%60%20%7C%20%60False%60%20%7C%209%20digits%3A%20the%20leading%20zero%20was%20lost%20%7C%0A%20%20%20%20%7C%20%60%22O102820000%22%60%20%7C%20%60False%60%20%7C%2010%20characters%2C%20but%20the%20first%20is%20the%20letter%20O%2C%20not%20a%20zero%20%7C%0A%0A%20%20%20%20**Tools**%20(Python%20Essentials%2C%20modules%202%20and%206)%3A%20%60len(code)%60%20is%20the%20number%20of%20characters%3B%20%60code.isdigit()%60%20is%20%60True%60%20when%20every%20character%20is%20a%20digit%3B%20%60and%60%20is%20%60True%60%20only%20when%20both%20sides%20are.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20is_psgc_code(code%3A%20str)%20-%3E%20bool%3A%0A%20%20%20%20%22%22%22Whether%20a%20code%20is%20exactly%2010%20characters%2C%20all%20digits%2C%20as%20a%20PSGC%20code%20is.%22%22%22%0A%20%20%20%20%23%20Exercise%201.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20is%20the%20code%2010%20characters%20long%3F%0A%20%20%20%20%23%20Step%202%3A%20is%20every%20character%20a%20digit%3F%0A%20%20%20%20%23%20Step%203%3A%20return%20True%20only%20when%20both%20are%20true%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.2%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%201.2%22%2C%20is_psgc_code%2C%0A%20%20%20%20%20%20%20%20%5B(f%22is_psgc_code(%7Bc!r%7D)%22%2C%20(c%2C))%20for%20c%20in%20%5B%220102820000%22%2C%20%221204701000%22%2C%20%22102820000%22%2C%20%22O102820000%22%2C%20%22102820000.0%22%2C%20%220102820000%20%22%2C%20%2201028-2000%22%2C%20%22%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22codes%22%2C%20hint%3D%22Two%20tests%2C%20joined%20with%20and%3A%20exactly%2010%20characters%2C%20and%20every%20character%20a%20digit.%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%20a%20code%20that%20lost%20its%20leading%20zero.%20Try%20others%3A%20%220102820000%22%2C%20%22O102820000%22.%0A%20%20%20%20try_it(is_psgc_code%2C%20%22102820000%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%201.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60True%60%20for%20%60'O102820000'%60%20%7C%20Only%20the%20length%20is%20tested.%20%7C%20add%20%60code.isdigit()%60%20%7C%0A%20%20%20%20%7C%20%60True%60%20for%20%60'102820000'%60%20%7C%20Only%20the%20digits%20are%20tested.%20%7C%20add%20%60len(code)%20%3D%3D%2010%60%20%7C%0A%20%20%20%20%7C%20%60True%60%20for%20%60'0102820000%20'%60%20%7C%20The%20space%20was%20removed%20before%20testing.%20The%20task%20asks%20about%20the%20code%20as%20it%20is.%20%7C%20test%20%60code%60%20itself%2C%20without%20%60.strip()%60%20%7C%0A%20%20%20%20%7C%20%60False%60%20for%20%60'1204701000'%60%20%7C%20The%20code%20is%20tested%20for%20a%20leading%20%600%60.%20Codes%20from%20Region%20X%20onward%20start%20with%20%601%60.%20%7C%20test%20the%20length%20and%20the%20digits%2C%20not%20the%20first%20character%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20object%20of%20type%20'int'%20has%20no%20len()*%20%7C%20Your%20own%20test%20gave%20the%20function%20a%20number%3B%20codes%20are%20text%2C%20and%20the%20check%20gives%20text%20only.%20%7C%20call%20it%20with%20text%3A%20%60is_psgc_code(%220102820000%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_(households)%3A%0A%20%20%20%20_as_text%20%3D%20households%5B%22psgc%22%5D.tolist()%0A%20%20%20%20_as_numbers%20%3D%20%5Bstr(code)%20for%20code%20in%20pd.read_csv(PATHS.survey%20%2F%20%22households.csv%22)%5B%22psgc%22%5D%5D%20%20%23%20read%20as%20numbers%2C%20then%20made%20text%0A%20%20%20%20mo.stop(attempt(is_psgc_code%2C%20_as_text%5B0%5D)%20is%20NOT_YET%2C%20waiting_for(%22Exercise%201.2%22))%0A%20%20%20%20_ok_text%20%3D%20sum(bool(is_psgc_code(c))%20for%20c%20in%20_as_text)%0A%20%20%20%20_ok_numbers%20%3D%20sum(bool(is_psgc_code(c))%20for%20c%20in%20_as_numbers)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Your%20function%20on%20the%20survey's%20codes**%2C%20read%20two%20ways%3A%0A%0A%20%20%20%20%7C%20%60psgc%60%2C%20read%20%7C%20example%20%7C%20codes%20your%20function%20accepts%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20as%20text%2C%20as%20the%20codebook%20says%20%7C%20%60%7B_as_text%5B0%5D%7D%60%20%7C%20%7B_ok_text%3A%2C%7D%20of%20%7Blen(_as_text)%3A%2C%7D%20%7C%0A%20%20%20%20%7C%20as%20numbers%2C%20then%20turned%20back%20into%20text%20%7C%20%60%7B_as_numbers%5B0%5D%7D%60%20%7C%20%7B_ok_numbers%3A%2C%7D%20of%20%7Blen(_as_numbers)%3A%2C%7D%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(plots)%3A%0A%20%20%20%20_sqm%20%3D%20int((plots%5B%22area_unit%22%5D%20%3D%3D%20%22sqm%22).sum())%0A%20%20%20%20_as_is%20%3D%20plots%5B%22area%22%5D.sum()%0A%20%20%20%20_in_ha%20%3D%20plots%5B%22area%22%5D.where(plots%5B%22area_unit%22%5D%20%3D%3D%20%22ha%22%2C%20plots%5B%22area%22%5D%20%2F%2010_000).sum()%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20Units%3A%20the%20same%20measure%2C%20written%20two%20ways%0A%0A%20%20%20%20The%20%60unit%60%20column%20says%20what%20each%20measure%20is%20counted%20in%3A%20%60hh_size%60%20in%20persons%2C%20%60head_age%60%20in%20years%2C%20%60production_kg%60%20and%20%60fertilizer_kg%60%20in%20kilograms.%20%60area%60%20says%20*see%20area_unit*%3A%20each%20plot's%20area%20comes%20with%20its%20own%20unit%2C%20%60ha%60%20(hectares)%20or%20%60sqm%60%20(square%20meters)%2C%20and%201%20ha%20%3D%2010%2C000%20sqm.%20The%20first%20household's%20second%20plot%20is%20%601940%60%20sqm%2C%20which%20is%200.194%20ha.%0A%0A%20%20%20%20Added%20as%20they%20are%2C%20the%20plots'%20areas%20come%20to%20**%7B_as_is%3A%2C.0f%7D**%3A%20the%20%7B_sqm%7D%20plots%20measured%20in%20square%20meters%20count%2010%2C000%20times%20too%20much.%20With%20every%20area%20in%20hectares%2C%20the%20total%20is%20**%7B_in_ha%3A%2C.0f%7D%20ha**.%20A%20yield%20(kilograms%20per%20hectare)%20computed%20from%20the%20mixed%20column%20would%20be%20wrong%20for%20every%20one%20of%20those%20plots.%20Module%205%20puts%20every%20measure%20in%20one%20unit.%0A%0A%20%20%20%20%23%23%23%20Weights%3A%20how%20many%20households%20one%20interview%20stands%20for%0A%0A%20%20%20%20The%20survey%20did%20not%20visit%20every%20farm%20household%3A%20it%20interviewed%20a%20**sample**%2C%20170%20households%20in%20each%20province.%20Each%20household's%20%60weight%60%20says%20how%20many%20farm%20households%20of%20its%20province%20it%20**stands%20for**.%20The%20first%20household's%20weight%2C%20279.77%2C%20means%20it%20stands%20for%20about%20280%20farm%20households%20of%20Ilocos%20Norte%3B%20a%20household%20with%20a%20weight%20of%201%2C000%20stands%20for%20about%201%2C000.%0A%0A%20%20%20%20So%20rows%20do%20**not**%20count%20the%20same.%20Adding%20the%20weights%20estimates%20how%20many%20farm%20households%20there%20are%3B%20a%20mean%20that%20counts%20each%20household%20as%20many%20times%20as%20it%20stands%20for%20is%20a%20**weighted%20mean**%20(module%204).%20Pick%20a%20province%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20weight_province%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%5B%22Ilocos%20Norte%22%2C%20%22Isabela%22%2C%20%22Nueva%20Ecija%22%2C%20%22Camarines%20Sur%22%2C%20%22Iloilo%22%2C%20%22Bukidnon%22%2C%20%22Cotabato%22%5D%2C%20value%3D%22Nueva%20Ecija%22%2C%20label%3D%22Province%3A%22)%0A%20%20%20%20weight_province%0A%20%20%20%20return%20(weight_province%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(weight_province)%3A%0A%20%20%20%20_survey%20%3D%20course.households()%20%20%23%20clean%20(the%20default)%3A%20each%20household%20once%3B%20module%205%20removes%20the%20copies%20itself%0A%20%20%20%20_here%20%3D%20_survey%5B_survey%5B%22province%22%5D%20%3D%3D%20weight_province.value%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20%7Bweight_province.value%7D%20%7C%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20households%20interviewed%20%7C%20%7Blen(_here)%3A%2C%7D%20%7C%0A%20%20%20%20%7C%20the%20sum%20of%20their%20weights%3A%20farm%20households%20represented%20%7C%20%7B_here%5B%22weight%22%5D.sum()%3A%2C.0f%7D%20%7C%0A%20%20%20%20%7C%20on%20average%2C%20one%20interview%20stands%20for%20%7C%20%7B_here%5B%22weight%22%5D.mean()%3A%2C.0f%7D%20farm%20households%20%7C%0A%0A%20%20%20%20The%20same%20number%20of%20interviews%20in%20every%20province%20stands%20for%20very%20different%20numbers%20of%20farm%20households.%20That%20is%20section%203's%20chart.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%201.3.%20Households%20represented%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22how%20many%20farm%20households%20are%20there%20in%20this%20province%3F%22%20is%20a%20sum%20of%20weights%2C%20not%20a%20count%20of%20interviews.%20Counting%20the%20rows%20answers%20a%20different%20question%3A%20how%20many%20were%20interviewed.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20list%20of%20households%2C%20each%20a%20dictionary%20with%20at%20least%20%60%22province%22%60%20and%20%60%22weight%22%60%2C%20and%20a%20province's%20name%20%E2%86%92%20the%20sum%20of%20the%20weights%20of%20that%20province's%20households%2C%20**rounded%20to%20a%20whole%20number**%3B%20%600%60%20when%20none%20is%20in%20that%20province.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20records%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22A%22%2C%20%22province%22%3A%20%22Iloilo%22%2C%20%22weight%22%3A%20250.4%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22B%22%2C%20%22province%22%3A%20%22Iloilo%22%2C%20%22weight%22%3A%20310.2%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22C%22%2C%20%22province%22%3A%20%22Bukidnon%22%2C%20%22weight%22%3A%20512.75%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22D%22%2C%20%22province%22%3A%20%22Iloilo%22%2C%20%22weight%22%3A%2099.1%7D%2C%0A%20%20%20%20%5D%0A%20%20%20%20households_represented(records%2C%20%22Iloilo%22)%20%20%20%20%23%20660%3A%20250.4%20%2B%20310.2%20%2B%2099.1%20is%20659.7%2C%20rounded%0A%20%20%20%20households_represented(records%2C%20%22Cotabato%22)%20%20%23%200%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20**Tools**%20(Python%20Essentials%2C%20modules%203%20to%205)%3A%20a%20total%20that%20starts%20at%20%600%60%3B%20%60for%20record%20in%20records%3A%60%3B%20%60if%20record%5B%22province%22%5D%20%3D%3D%20province%3A%60%3B%20%60round(value)%60%2C%20which%20rounds%20to%20the%20nearest%20whole%20number.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20households_represented(records%3A%20list%5Bdict%5D%2C%20province%3A%20str)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22How%20many%20farm%20households%20a%20province's%20interviewed%20households%20stand%20for%3A%20the%20sum%20of%20their%20weights%2C%20rounded.%22%22%22%0A%20%20%20%20%23%20Exercise%201.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20start%20a%20total%20at%200%0A%20%20%20%20%23%20Step%202%3A%20for%20each%20record%20whose%20province%20is%20%60province%60%2C%20add%20its%20weight%20to%20the%20total%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20total%2C%20rounded%20to%20a%20whole%20number%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.3%22)%0A%0A%0A%40app.function%0Adef%20small_records()%20-%3E%20list%5Bdict%5D%3A%0A%20%20%20%20%22%22%22Four%20households%2C%20used%20by%20the%20task's%20example%20and%20the%20check%20of%20Exercise%201.3.%22%22%22%0A%20%20%20%20return%20%5B%0A%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22A%22%2C%20%22province%22%3A%20%22Iloilo%22%2C%20%22weight%22%3A%20250.4%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22B%22%2C%20%22province%22%3A%20%22Iloilo%22%2C%20%22weight%22%3A%20310.2%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22C%22%2C%20%22province%22%3A%20%22Bukidnon%22%2C%20%22weight%22%3A%20512.75%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22hh_id%22%3A%20%22D%22%2C%20%22province%22%3A%20%22Iloilo%22%2C%20%22weight%22%3A%2099.1%7D%2C%0A%20%20%20%20%5D%0A%0A%0A%40app.function%0Adef%20survey_records()%20-%3E%20list%5Bdict%5D%3A%0A%20%20%20%20%22%22%22The%20survey's%20households%20(each%20once)%2C%20as%20a%20list%20of%20dictionaries%20with%20their%20ID%2C%20province%20and%20weight.%22%22%22%0A%20%20%20%20survey%20%3D%20course.households()%0A%20%20%20%20return%20%5B%7B%22hh_id%22%3A%20i%2C%20%22province%22%3A%20p%2C%20%22weight%22%3A%20w%7D%20for%20i%2C%20p%2C%20w%20in%20zip(survey%5B%22hh_id%22%5D%2C%20survey%5B%22province%22%5D%2C%20survey%5B%22weight%22%5D)%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_small%2C%20_survey%20%3D%20small_records()%2C%20survey_records()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%201.3%22%2C%20households_represented%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%20four%20households%20above%2C%20Iloilo%22%2C%20(_small%2C%20%22Iloilo%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20four%20households%20above%2C%20Bukidnon%22%2C%20(_small%2C%20%22Bukidnon%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20four%20households%20above%2C%20Cotabato%20(none)%22%2C%20(_small%2C%20%22Cotabato%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%2C%20Nueva%20Ecija%22%2C%20(_survey%2C%20%22Nueva%20Ecija%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%2C%20Ilocos%20Norte%22%2C%20(_survey%2C%20%22Ilocos%20Norte%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22lists%20of%20households%22%2C%20hint%3D%22Add%20up%20the%20weights%20of%20the%20province's%20households%2C%20not%201%20for%20each%3B%20round%20once%2C%20at%20the%20end.%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%20farm%20households%20Iloilo's%20interviews%20stand%20for.%20Change%20the%20province%20and%20run%20the%20cell.%0A%20%20%20%20try_it(households_represented%2C%20survey_records()%2C%20%22Iloilo%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%201.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%603%60%20for%20Iloilo%20in%20the%20four%20households%20%7C%20The%20function%20counts%20the%20households%20instead%20of%20adding%20their%20weights.%20%7C%20%60total%20%3D%20total%20%2B%20record%5B%22weight%22%5D%60%20%7C%0A%20%20%20%20%7C%20%60659.7%60%20for%20Iloilo%20%7C%20The%20total%20is%20not%20rounded.%20%7C%20%60return%20round(total)%60%20%7C%0A%20%20%20%20%7C%20%60659%60%20for%20Iloilo%20%7C%20%60int(...)%60%20cuts%20the%20decimals%20off%20instead%20of%20rounding.%20%7C%20%60round(total)%60%20%7C%0A%20%20%20%20%7C%20%60250%60%20for%20Iloilo%20%7C%20%60return%60%20is%20inside%20the%20loop%2C%20so%20the%20function%20stops%20at%20the%20first%20household.%20%7C%20%60return%60%20at%20the%20function's%20own%20indentation%2C%20after%20the%20loop%20%7C%0A%20%20%20%20%7C%20The%20same%20total%20for%20every%20province%20%7C%20The%20%60if%60%20is%20missing%2C%20so%20every%20household%20is%20added.%20%7C%20%60if%20record%5B%22province%22%5D%20%3D%3D%20province%3A%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'Weight'*%20%7C%20The%20key%20is%20spelled%20differently%20from%20the%20dictionaries'.%20Keys%20are%20exact%2C%20lower%20case%20included.%20%7C%20%60record%5B%22weight%22%5D%60%2C%20%60record%5B%22province%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(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_records%20%3D%20survey_records()%0A%20%20%20%20_provinces%20%3D%20%5B%22Ilocos%20Norte%22%2C%20%22Isabela%22%2C%20%22Nueva%20Ecija%22%2C%20%22Camarines%20Sur%22%2C%20%22Iloilo%22%2C%20%22Bukidnon%22%2C%20%22Cotabato%22%5D%0A%20%20%20%20_totals%20%3D%20%7Bp%3A%20attempt(households_represented%2C%20_records%2C%20p)%20for%20p%20in%20_provinces%7D%0A%20%20%20%20mo.stop(any(t%20is%20NOT_YET%20for%20t%20in%20_totals.values())%2C%20waiting_for(%22Exercise%201.3%22))%0A%20%20%20%20_counts%20%3D%20%7Bp%3A%20sum(r%5B%22province%22%5D%20%3D%3D%20p%20for%20r%20in%20_records)%20for%20p%20in%20_provinces%7D%0A%20%20%20%20mo.md(%22**Your%20function%2C%20for%20every%20province%3A**%5Cn%5Cn%7C%20province%20%7C%20households%20interviewed%20%7C%20farm%20households%20represented%20%7C%5Cn%7C%3A--%7C--%3A%7C--%3A%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%2B%20%22%5Cn%22.join(f%22%7C%20%7Bp%7D%20%7C%20%7B_counts%5Bp%5D%3A%2C%7D%20%7C%20%7B_totals%5Bp%5D%3A%2C%7D%20%7C%22%20for%20p%20in%20_provinces))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Before%20you%20analyze%3A%20five%20questions%0A%0A%20%20%20%20%7C%20question%20%7C%20where%20the%20codebook%20answers%20it%20%7C%20the%20farm%20survey's%20answer%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20What%20is%20one%20row%3F%20(the%20unit%20of%20observation)%20%7C%20the%20file%20each%20variable%20is%20in%2C%20and%20its%20ID%20%7C%20in%20households%2C%20one%20household%20interviewed%3B%20in%20plots%2C%20one%20plot%2C%20linked%20to%20its%20household%20by%20%60hh_id%60%20%7C%0A%20%20%20%20%7C%20What%20does%20each%20column%20hold%3F%20(the%20variables)%20%7C%20%60variable%60%2C%20%60label%60%2C%20%60type%60%20%7C%208%20variables%20in%20each%20file%20%7C%0A%20%20%20%20%7C%20Which%20numbers%20are%20codes%3F%20%7C%20%60values%60%20%7C%20%60head_sex%60%20(1%2C%202%2C%209)%20and%20%60head_age%60's%20999%3B%20%60area_unit%60%20and%20%60irrigated%60%20hold%20codes%20as%20text%3B%20%60psgc%60%20is%20a%20code%20that%20must%20stay%20text%20%7C%0A%20%20%20%20%7C%20In%20what%20unit%20is%20each%20measure%3F%20%7C%20%60unit%60%20%7C%20persons%2C%20years%2C%20kilograms%3B%20area%20in%20hectares%20or%20square%20meters%2C%20plot%20by%20plot%20%7C%0A%20%20%20%20%7C%20Does%20every%20row%20count%20the%20same%3F%20(weights)%20%7C%20a%20variable%20labeled%20*survey%20weight*%20%7C%20no%3A%20%60weight%60%20says%20how%20many%20farm%20households%20each%20interview%20stands%20for%20%7C%0A%0A%20%20%20%20The%20codebook%20does%20not%20list%20everything%20that%20can%20go%20wrong%3A%20households%20entered%20twice%2C%20plots%20with%20no%20crop%20recorded%2C%20harvests%20written%20with%20thousands%20separators.%20Those%20are%20for%20modules%203%20and%205%2C%20which%20read%20the%20files%20themselves.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20A%20first%20chart%2C%20three%20ways%3A%20matplotlib%2C%20seaborn%2C%20Altair%0A%0A%20%20%20%20Every%20province%20had%20the%20same%20number%20of%20interviews.%20Does%20every%20province%20have%20as%20many%20farm%20households%3F%20The%20weights%20say%20no%3B%20a%20chart%20says%20it%20at%20a%20glance.%20Here%20is%20the%20table%20to%20chart%3A%20per%20province%2C%20the%20households%20interviewed%20and%20the%20sum%20of%20their%20weights%2C%20largest%20first.%20(It%20is%20made%20with%20%60groupby%60%2C%20which%20module%204%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%20by_province%20%3D%20(%0A%20%20%20%20%20%20%20%20course.households()%0A%20%20%20%20%20%20%20%20.groupby(%22province%22%2C%20sort%3DFalse)%0A%20%20%20%20%20%20%20%20.agg(interviewed%3D(%22hh_id%22%2C%20%22size%22)%2C%20farm_households%3D(%22weight%22%2C%20%22sum%22))%0A%20%20%20%20%20%20%20%20.round(%7B%22farm_households%22%3A%200%7D)%0A%20%20%20%20%20%20%20%20.astype(%7B%22farm_households%22%3A%20int%7D)%0A%20%20%20%20%20%20%20%20.sort_values(%22farm_households%22%2C%20ascending%3DFalse)%0A%20%20%20%20%20%20%20%20.reset_index()%0A%20%20%20%20)%0A%20%20%20%20by_province%0A%20%20%20%20return%20(by_province%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%20Python%20has%20many%20chart%20libraries.%20This%20course%20uses%20three%2C%20and%20each%20asks%20you%20for%20something%20different%3A%20**matplotlib**%20wants%20a%20figure%20and%20axes%2C%20and%20one%20call%20for%20each%20thing%20drawn%3B%20**seaborn**%20wants%20the%20table%20and%20the%20names%20of%20its%20columns%3B%20**Altair**%20wants%20the%20table%2C%20a%20mark%20and%20**encodings**%20that%20say%20which%20column%20drives%20which%20property%20of%20the%20mark.%20Here%20is%20the%20same%20bar%20chart%20in%20each%2C%20as%20a%20function%20that%20takes%20the%20table%20and%20returns%20the%20chart.%20Read%20the%20three%2C%20then%20compare%20what%20they%20draw%20in%20the%20tabs%20below.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20chart_matplotlib(table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22Farm%20households%20by%20province%2C%20as%20horizontal%20bars%2C%20drawn%20with%20matplotlib.%22%22%22%0A%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(figsize%3D(7%2C%203.2))%20%20%23%201.%20a%20figure%20(the%20whole%20picture)%20and%20its%20axes%20(the%20area%20with%20the%20x%20and%20y%20axes)%0A%20%20%20%20ax.barh(table%5B%22province%22%5D%2C%20table%5B%22farm_households%22%5D)%20%20%23%202.%20one%20call%20per%20thing%20drawn%3A%20a%20bar%20per%20province%2C%20this%20long%0A%20%20%20%20ax.invert_yaxis()%20%20%23%20matplotlib%20puts%20the%20first%20row%20at%20the%20bottom%3B%20this%20puts%20it%20at%20the%20top%2C%20as%20in%20the%20table%0A%20%20%20%20ax.set_xlabel(%22farm%20households%20(sum%20of%20the%20survey%20weights)%22)%20%20%23%203.%20labels%2C%20one%20call%20each%2C%20on%20the%20axes%0A%20%20%20%20ax.set_title(%22Farm%20households%20represented%2C%20by%20province%22)%0A%20%20%20%20return%20ax%0A%0A%0A%40app.function%0Adef%20chart_seaborn(table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22The%20same%20chart%2C%20drawn%20with%20seaborn.%22%22%22%0A%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(figsize%3D(7%2C%203.2))%20%20%23%20seaborn%20draws%20on%20matplotlib's%20axes%3A%20make%20a%20fresh%20pair%20first%0A%20%20%20%20%23%20The%20table%2C%20and%20which%20column%20goes%20on%20which%20axis.%20errorbar%3DNone%3A%20one%20row%20per%20province%2C%20so%20no%20error%20bar%20to%20draw.%0A%20%20%20%20sns.barplot(data%3Dtable%2C%20x%3D%22farm_households%22%2C%20y%3D%22province%22%2C%20errorbar%3DNone%2C%20ax%3Dax)%0A%20%20%20%20ax.set_xlabel(%22farm%20households%20(sum%20of%20the%20survey%20weights)%22)%20%20%23%20labels%3A%20matplotlib's%2C%20on%20the%20same%20axes%0A%20%20%20%20ax.set_title(%22Farm%20households%20represented%2C%20by%20province%22)%0A%20%20%20%20return%20ax%0A%0A%0A%40app.function%0Adef%20chart_altair(table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22The%20same%20chart%2C%20drawn%20with%20Altair.%22%22%22%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20alt.Chart(table%2C%20title%3D%22Farm%20households%20represented%2C%20by%20province%22)%20%20%23%20the%20table%0A%20%20%20%20%20%20%20%20.mark_bar()%20%20%23%20the%20mark%3A%20a%20bar%20for%20each%20row%0A%20%20%20%20%20%20%20%20.encode(%20%20%23%20the%20encodings%3A%20which%20column%20drives%20which%20property%20of%20the%20bars%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dalt.X(%22farm_households%22%2C%20title%3D%22farm%20households%20(sum%20of%20the%20survey%20weights)%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dalt.Y(%22province%22%2C%20sort%3DNone)%2C%20%20%23%20sort%3DNone%20keeps%20the%20table's%20order%3B%20Altair%20sorts%20names%20alphabetically%20otherwise%0A%20%20%20%20%20%20%20%20%20%20%20%20tooltip%3D%5B%22province%22%2C%20%22interviewed%22%2C%20%22farm_households%22%5D%2C%20%20%23%20what%20shows%20when%20you%20hover%20over%20a%20bar%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.properties(width%3D520%2C%20height%3D220)%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(by_province)%3A%0A%20%20%20%20mo.ui.tabs(%7B%0A%20%20%20%20%20%20%20%20%22matplotlib%22%3A%20chart_matplotlib(by_province)%2C%0A%20%20%20%20%20%20%20%20%22seaborn%22%3A%20chart_seaborn(by_province)%2C%0A%20%20%20%20%20%20%20%20%22Altair%22%3A%20chart_altair(by_province)%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(by_province)%3A%0A%20%20%20%20_top%2C%20_bottom%20%3D%20by_province.iloc%5B0%5D%2C%20by_province.iloc%5B-1%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20The%20same%20%7B_top%5B%22interviewed%22%5D%7D%20interviews%20stand%20for%20**%7B_top%5B%22farm_households%22%5D%3A%2C%7D**%20farm%20households%20in%20%7B_top%5B%22province%22%5D%7D%20and%20**%7B_bottom%5B%22farm_households%22%5D%3A%2C%7D**%20in%20%7B_bottom%5B%22province%22%5D%7D%2C%20%7B_top%5B%22farm_households%22%5D%20%2F%20_bottom%5B%22farm_households%22%5D%3A.1f%7D%20times%20as%20many.%20A%20result%20that%20ignores%20the%20weights%20gives%20every%20province%20the%20same%20say.%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%20What%20each%20library%20asked%20for%3A%0A%0A%20%20%20%20%7C%20%7C%20matplotlib%20%7C%20seaborn%20%7C%20Altair%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20you%20start%20from%20%7C%20a%20figure%20and%20its%20axes%3A%20%60fig%2C%20ax%20%3D%20plt.subplots()%60%20%7C%20the%20table%3A%20%60data%3Dtable%60%20%7C%20the%20table%3A%20%60alt.Chart(table)%60%20%7C%0A%20%20%20%20%7C%20you%20say%20what%20to%20draw%20%7C%20one%20call%20per%20thing%20drawn%2C%20given%20the%20values%20themselves%3A%20%60ax.barh(table%5B%22province%22%5D%2C%20table%5B%22farm_households%22%5D)%60%20%7C%20the%20kind%20of%20chart%2C%20and%20column%20**names**%3A%20%60sns.barplot(...%2C%20x%3D%22farm_households%22%2C%20y%3D%22province%22)%60%20%7C%20a%20mark%20and%20its%20encodings%2C%20by%20column%20name%3A%20%60.mark_bar().encode(x%3D%22farm_households%22%2C%20y%3D%22province%22)%60%20%7C%0A%20%20%20%20%7C%20labels%20and%20title%20%7C%20set%20on%20the%20axes%2C%20one%20call%20each%3A%20%60ax.set_xlabel(...)%60%2C%20%60ax.set_title(...)%60%20%7C%20the%20same%20calls%2C%20on%20the%20same%20matplotlib%20axes%20%7C%20given%20in%20the%20chart%3A%20%60title%3D%60%20%7C%0A%20%20%20%20%7C%20you%20get%20back%20%7C%20the%20axes%2C%20drawn%20on%20%7C%20matplotlib's%20axes%2C%20drawn%20on%20by%20seaborn%20%7C%20a%20Chart%3A%20a%20description%20of%20the%20chart%2C%20which%20your%20browser%20draws%20%7C%0A%20%20%20%20%7C%20on%20the%20page%20%7C%20a%20picture%20%7C%20a%20picture%20%7C%20interactive%3A%20hover%20over%20a%20bar%20to%20see%20its%20tooltip%20%7C%0A%0A%20%20%20%20Two%20words%20to%20keep%20apart%3A%20in%20matplotlib%2C%20**axes**%20(%60ax%60)%20is%20one%20chart's%20drawing%20area%2C%20with%20its%20x%20axis%20and%20its%20y%20axis%3B%20a%20**figure**%20is%20the%20whole%20picture%2C%20which%20can%20hold%20several%20axes.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22One%20more%20difference%3A%20which%20library%20does%20the%20arithmetic%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20matplotlib%20draws%20the%20numbers%20you%20give%20it%2C%20so%20the%20totals%20had%20to%20be%20computed%20first%2C%20in%20the%20table.%20seaborn%20and%20Altair%20can%20summarize%20the%20rows%20themselves.%20Both%20lines%20below%20draw%20the%20same%20seven%20totals%20straight%20from%20the%20survey's%20households%2C%20one%20row%20per%20household%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20sns.barplot(data%3Dhouseholds%2C%20x%3D%22weight%22%2C%20y%3D%22province%22%2C%20estimator%3D%22sum%22%2C%20errorbar%3DNone%2C%20ax%3Dax)%0A%20%20%20%20alt.Chart(households).mark_bar().encode(x%3D%22sum(weight)%22%2C%20y%3D%22province%22)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Without%20%60estimator%3D%22sum%22%60%2C%20seaborn's%20bar%20chart%20draws%20each%20province's%20**mean**%20weight%2C%20and%20an%20error%20bar%20for%20it.%20That%20is%20why%20the%20examples%20above%20give%20seaborn%20one%20row%20per%20province%3A%20the%20mean%20of%20one%20value%20is%20that%20value.%20Module%207%20returns%20to%20choosing%20between%20the%20three.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20bars_of(chart)%20-%3E%20list%5Bdict%5D%3A%0A%20%20%20%20%22%22%22What%20a%20chart%20draws%2C%20its%20bars%20only%3A%20the%20checks%20of%20Exercises%201.4%20and%201.5%20compare%20these%2C%20whichever%20library%20drew%20them.%0A%20%20%20%20seaborn%20may%20add%20error%20bars%20as%20lines%2C%20which%20the%20answer%20may%20leave%20out.%22%22%22%0A%20%20%20%20%23%20Colors%20are%20the%20learner's%20to%20choose%2C%20so%20which%20series%20a%20bar%20is%20in%20is%20left%20out%3A%20a%20bar%20per%20category%20in%20its%20own%20color%20is%20the%20same%20answer%0A%20%20%20%20return%20course.chart_data(chart%2C%20marks%3D(%22bar%22%2C)%2C%20series%3DFalse)%0A%0A%0A%40app.function%0Adef%20show_chart(function%2C%20*args)%3A%0A%20%20%20%20%22%22%22A%20chart%20exercise's%20Try%20it%3A%20the%20chart%20the%20function%20returns%2C%20or%20what%20went%20wrong.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20chart%20%3D%20function(*args)%0A%20%20%20%20except%20Exception%3A%20%20%23%20not%20written%20yet%2C%20or%20an%20error%3A%20try_it%20says%20which%2C%20and%20what%20the%20error%20usually%20means%0A%20%20%20%20%20%20%20%20return%20try_it(function%2C%20*args)%0A%20%20%20%20if%20chart%20is%20None%3A%0A%20%20%20%20%20%20%20%20return%20course.look(%22No%20chart%20came%20back%22%2C%20f%22%60%7Bfunction.__name__%7D%60%20gave%20back%20%60None%60.%20End%20it%20with%20%60return%20ax%60%20(matplotlib%2C%20seaborn)%20or%20%60return%20chart%60%20(Altair)%3A%20a%20function's%20result%20is%20what%20it%20returns%2C%20not%20what%20it%20draws.%22)%0A%20%20%20%20return%20chart%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%201.4.%20Plots%20by%20crop%2C%20in%20the%20library%20of%20your%20choice%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20each%20library%20asks%20for%20the%20same%20chart%20in%20its%20own%20terms.%20Drawing%20one%20yourself%2C%20from%20a%20table%20you%20have%20not%20charted%20yet%2C%20is%20how%20you%20learn%20which%20terms.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20with%20a%20%60crop%60%20column%20and%20a%20%60plots%60%20column%20%E2%86%92%20a%20bar%20chart%20with%20one%20bar%20per%20crop%2C%20as%20long%20as%20its%20number%20of%20plots.%20Return%20the%20chart%3A%20the%20axes%20for%20matplotlib%20or%20seaborn%2C%20the%20Chart%20for%20Altair.%0A%0A%20%20%20%20%7C%20crop%20%7C%20plots%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20Rice%20%7C%2012%20%7C%0A%20%20%20%20%7C%20Corn%20%7C%205%20%7C%0A%0A%20%20%20%20gives%20two%20bars%2C%20Rice%2012%20long%20and%20Corn%205.%20The%20survey's%20own%20table%20is%20under%20this%20text%3B%20the%20Try%20it%20cell%20charts%20it.%0A%0A%20%20%20%20Use%20**any%20one**%20of%20the%20three%20libraries%3A%20the%20check%20compares%20the%20bars%20drawn%20(each%20bar's%20crop%20and%20its%20length)%2C%20not%20the%20picture%2C%20so%20titles%2C%20colors%20and%20whether%20the%20bars%20run%20up%20or%20across%20are%20yours%20to%20choose.%0A%0A%20%20%20%20**Tools%3A**%20the%20three%20functions%20above.%20In%20matplotlib%2C%20%60ax.bar(x%20values%2C%20heights)%60%20draws%20upright%20bars%20and%20%60ax.barh(...)%60%20bars%20across%3B%20in%20seaborn%2C%20%60sns.barplot(data%3Dtable%2C%20x%3D...%2C%20y%3D...%2C%20errorbar%3DNone%2C%20ax%3Dax)%60%3B%20in%20Altair%2C%20%60alt.Chart(table).mark_bar().encode(x%3D...%2C%20y%3D...)%60.%20For%20matplotlib%20and%20seaborn%2C%20make%20a%20fresh%20figure%20inside%20the%20function%3A%20%60fig%2C%20ax%20%3D%20plt.subplots()%60.%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%20plots_per_crop%20%3D%20course.plots()%5B%22crop%22%5D.value_counts().rename_axis(%22crop%22).reset_index(name%3D%22plots%22)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22%60plots_per_crop%60%2C%20the%20survey's%20plots%20by%20crop%20(plots%20with%20no%20crop%20recorded%20are%20not%20counted)%3A%22)%2C%20plots_per_crop%5D)%0A%20%20%20%20return%20(plots_per_crop%2C)%0A%0A%0A%40app.function%0Adef%20crop_chart(table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22A%20bar%20chart%20of%20the%20number%20of%20plots%20of%20each%20crop.%22%22%22%0A%20%20%20%20%23%20Exercise%201.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Pick%20one%20library%20and%20follow%20its%20example%20in%20section%203%3A%0A%20%20%20%20%23%20%20%20matplotlib%3A%20fig%2C%20ax%20%3D%20plt.subplots()%3B%20draw%20the%20bars%20with%20ax.bar(...)%3B%20return%20ax%0A%20%20%20%20%23%20%20%20seaborn%3A%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots()%3B%20sns.barplot(data%3Dtable%2C%20x%3D...%2C%20y%3D...%2C%20errorbar%3DNone%2C%20ax%3Dax)%3B%20return%20ax%0A%20%20%20%20%23%20%20%20Altair%3A%20%20%20%20%20return%20alt.Chart(table).mark_bar().encode(x%3D...%2C%20y%3D...)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.4%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(plots_per_crop)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%201.4%22%2C%20crop_chart%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%20plots%20by%20crop%22%2C%20(plots_per_crop%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20crops%3A%20Rice%2012%2C%20Corn%205%22%2C%20(pd.DataFrame(%7B%22crop%22%3A%20%5B%22Rice%22%2C%20%22Corn%22%5D%2C%20%22plots%22%3A%20%5B12%2C%205%5D%7D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22one%20crop%3A%20Banana%203%22%2C%20(pd.DataFrame(%7B%22crop%22%3A%20%5B%22Banana%22%5D%2C%20%22plots%22%3A%20%5B3%5D%7D)%2C))%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%3Dbars_of%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Draw%20the%20input%20table's%20plots%20against%20its%20crops%2C%20in%20a%20fresh%20figure%20(matplotlib%2C%20seaborn)%2C%20and%20return%20the%20chart.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plots_per_crop)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20chart%20of%20the%20survey's%20plots%20by%20crop.%0A%20%20%20%20show_chart(crop_chart%2C%20plots_per_crop)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%201.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*TypeError%3A%20chart_data%20reads%20a%20matplotlib%2C%20seaborn%20or%20Altair%20chart%2C%20not%20a%20builtins.NoneType*%20%7C%20The%20function%20draws%20the%20chart%20but%20does%20not%20return%20it.%20%7C%20the%20last%20line%3A%20%60return%20ax%60%20(matplotlib%2C%20seaborn)%2C%20%60return%60%20the%20Chart%20(Altair)%20%7C%0A%20%20%20%20%7C%20Some%20tables%20are%20right%2C%20others%20show%20extra%20bars%20%7C%20The%20bars%20were%20drawn%20on%20a%20chart%20already%20there%20(seaborn%20without%20%60ax%3D%60%2C%20or%20%60plt.bar%60)%2C%20so%20each%20table's%20bars%20pile%20onto%20the%20ones%20before.%20%7C%20%60fig%2C%20ax%20%3D%20plt.subplots()%60%20inside%20the%20function%2C%20and%20%60ax%3Dax%60%20for%20seaborn%20%7C%0A%20%20%20%20%7C%20Every%20bar%20is%201%20long%20%7C%20The%20chart%20counts%20the%20rows%20(seaborn's%20%60countplot%60%2C%20Altair's%20%60count()%60)%20instead%20of%20drawing%20the%20%60plots%60%20column.%20%7C%20%60ax.bar(table%5B%22crop%22%5D%2C%20table%5B%22plots%22%5D)%60%2C%20or%20%60y%3D%22plots%22%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'Crop'*%20(matplotlib)%2C%20*Could%20not%20interpret%20value%20%60Crop%60*%20(seaborn)%20or%20*Unable%20to%20determine%20data%20type%20for%20the%20field%20%22Crop%22*%20(Altair)%20%7C%20A%20column%20name%20that%20is%20not%20in%20the%20table.%20Names%20are%20exact%2C%20lower%20case%20included.%20%7C%20the%20columns%3A%20%60crop%60%20and%20%60plots%60%20%7C%0A%20%20%20%20%7C%20Right%20for%20the%20survey's%20table%2C%20wrong%20for%20the%20small%20ones%20%7C%20The%20function%20charts%20%60plots_per_crop%60%20instead%20of%20its%20input.%20%7C%20use%20%60table%60%20everywhere%20inside%20the%20function%20%7C%0A%20%20%20%20%7C%20%600%20of%203%20tables%20are%20right%60%2C%20and%20the%20chart%20shows%20a%20line%20%7C%20A%20line%20chart%20(%60ax.plot%60%2C%20%60sns.lineplot%60%2C%20%60mark_line%60)%3A%20the%20check%20reads%20bars.%20%7C%20%60ax.bar%60%2C%20%60sns.barplot%60%2C%20%60mark_bar%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%201.5.%20Any%20column%2C%20by%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20chart%20function%20that%20takes%20the%20column%20to%20draw%20as%20an%20input%20works%20for%20every%20column%20of%20the%20table.%20It%20is%20also%20where%20a%20common%20slip%20shows%3A%20the%20input%20%60column%60%20**holds**%20a%20column's%20name%2C%20so%20it%20is%20written%20without%20quotes.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20with%20a%20%60province%60%20column%2C%20and%20the%20name%20of%20another%20of%20its%20columns%20%E2%86%92%20a%20bar%20chart%20with%20one%20bar%20per%20province%2C%20as%20long%20as%20that%20column's%20value.%0A%0A%20%20%20%20%7C%20call%20%7C%20draws%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60province_chart(by_province%2C%20%22farm_households%22)%60%20%7C%20the%20chart%20above%2C%20in%20any%20library%20%7C%0A%20%20%20%20%7C%20%60province_chart(by_province%2C%20%22interviewed%22)%60%20%7C%20seven%20bars%2C%20all%20170%20long%20%7C%0A%0A%20%20%20%20**Tools%3A**%20as%20in%20Exercise%201.4.%20Where%20the%20examples%20write%20%60%22farm_households%22%60%2C%20write%20%60column%60%3A%20%60table%5Bcolumn%5D%60%20in%20matplotlib%2C%20%60x%3Dcolumn%60%20in%20seaborn%20and%20Altair.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20province_chart(table%3A%20pd.DataFrame%2C%20column%3A%20str)%3A%0A%20%20%20%20%22%22%22A%20bar%20chart%20of%20one%20column%20of%20the%20table%2C%20a%20bar%20per%20province.%22%22%22%0A%20%20%20%20%23%20Exercise%201.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20pick%20a%20library%2C%20and%20draw%20a%20bar%20per%20province%20as%20in%20section%203%0A%20%20%20%20%23%20Step%202%3A%20wherever%20the%20example%20names%20the%20column%20%22farm_households%22%2C%20use%20the%20input%20%60column%60%20(no%20quotes)%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20chart%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%201.5%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(by_province)%3A%0A%20%20%20%20_small%20%3D%20pd.DataFrame(%7B%22province%22%3A%20%5B%22Iloilo%22%2C%20%22Cotabato%22%5D%2C%20%22interviewed%22%3A%20%5B12%2C%209%5D%2C%20%22farm_households%22%3A%20%5B4100%2C%202650%5D%7D)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%201.5%22%2C%20province_chart%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22by_province%2C%20'farm_households'%22%2C%20(by_province%2C%20%22farm_households%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22by_province%2C%20'interviewed'%22%2C%20(by_province%2C%20%22interviewed%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20two-province%20table%2C%20'farm_households'%22%2C%20(_small%2C%20%22farm_households%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20two-province%20table%2C%20'interviewed'%22%2C%20(_small%2C%20%22interviewed%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20and%20columns%22%2C%20summarize%3Dbars_of%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Use%20the%20input%20column%20without%20quotes%3A%20it%20holds%20the%20column's%20name.%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%20column_pick%20%3D%20mo.ui.dropdown(options%3D%5B%22farm_households%22%2C%20%22interviewed%22%5D%2C%20value%3D%22interviewed%22%2C%20label%3D%22Column%20to%20chart%3A%22)%0A%20%20%20%20column_pick%0A%20%20%20%20return%20(column_pick%2C)%0A%0A%0A%40app.cell%0Adef%20_(by_province%2C%20column_pick)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20chart%20of%20the%20column%20picked%20above.%0A%20%20%20%20show_chart(province_chart%2C%20by_province%2C%20column_pick.value)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%201.5%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'column'*%20%7C%20The%20input's%20name%20is%20written%20in%20quotes%2C%20%60table%5B%22column%22%5D%60%2C%20so%20pandas%20looks%20for%20a%20column%20called%20*column*.%20%7C%20%60table%5Bcolumn%5D%60%2C%20without%20quotes%20%7C%0A%20%20%20%20%7C%20*Could%20not%20interpret%20value%20%60column%60*%20(seaborn)%20or%20*Unable%20to%20determine%20data%20type%20for%20the%20field%20%22column%22*%20(Altair)%20%7C%20The%20same%2C%20in%20seaborn%20or%20Altair%3A%20%60x%3D%22column%22%60.%20%7C%20%60x%3Dcolumn%60%20%7C%0A%20%20%20%20%7C%20Right%20for%20%60farm_households%60%2C%20wrong%20for%20%60interviewed%60%20%7C%20The%20function%20still%20names%20the%20example's%20column%20instead%20of%20using%20its%20input.%20%7C%20every%20place%20a%20column%20is%20named%2C%20except%20%60%22province%22%60%20%7C%0A%20%20%20%20%7C%20Right%20for%20%60by_province%60%2C%20wrong%20for%20the%20two-province%20table%20%7C%20The%20function%20charts%20%60by_province%60%20instead%20of%20its%20input.%20%7C%20use%20%60table%60%20everywhere%20inside%20the%20function%20%7C%0A%20%20%20%20%7C%20Some%20cases%20are%20right%2C%20others%20show%20extra%20bars%20%7C%20The%20bars%20pile%20onto%20a%20chart%20already%20there.%20%7C%20%60fig%2C%20ax%20%3D%20plt.subplots()%60%20inside%20the%20function%2C%20and%20%60ax%3Dax%60%20for%20seaborn%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20chart_data%20reads%20%E2%80%A6%20not%20a%20builtins.NoneType*%20%7C%20No%20%60return%60.%20%7C%20%60return%20ax%60%2C%20or%20%60return%60%20the%20Chart%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%20You%20are%20ready%0A%0A%20%20%20%20You%20have%20a%20working%20course%20folder%2C%20you%20know%20what%20one%20row%20of%20the%20farm%20survey%20is%20and%20what%20its%20codes%2C%20units%20and%20weights%20mean%2C%20and%20you%20have%20drawn%20a%20chart%20in%20the%20library%20of%20your%20choice.%20Next%20is%20module%202%2C%20**Arrays%20with%20NumPy**%3A%20open%20%6010_arrays.py%60%20from%20the%20file%20list%20(in%20marimo%2C%20the%20menu%20at%20the%20top%20left)%2C%20or%20go%20back%20to%20the%20list%20of%20notebooks%20in%20your%20browser%20tab.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%221.3%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
82077205da53e6574fd881b533e3252e