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%22Capstone%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20settings%20every%20other%20cell%20uses.%0A%20%20%20%20import%20io%0A%20%20%20%20import%20re%0A%20%20%20%20from%20datetime%20import%20date%0A%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20openpyxl.styles%20import%20Font%0A%20%20%20%20from%20openpyxl.utils%20import%20get_column_letter%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%2C%20the%20survey%20read%20the%20way%20the%20course%20teaches%2C%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20attempt%2C%20check_cases%2C%20waiting_for%0A%0A%20%20%20%20CAPSTONE%20%3D%20PATHS.root%20%2F%20%22capstone%22%20%20%23%20where%20each%20group's%20plan%20and%20notebook%20go%0A%20%20%20%20PROBLEMS%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%221%20%C2%B7%20Which%20areas'%20yields%20fell%20this%20season%3F%22%3A%20%22yields%22%2C%0A%20%20%20%20%20%20%20%20%222%20%C2%B7%20The%20summary%20table%2C%20retyped%20into%20Excel%20every%20quarter%22%3A%20%22workbook%22%2C%0A%20%20%20%20%20%20%20%20%223%20%C2%B7%20Monthly%20price%20files%20whose%20layout%20changes%22%3A%20%22prices%22%2C%0A%20%20%20%20%20%20%20%20%224%20%C2%B7%20The%20survey's%20totals%20don't%20match%20the%20published%20ones%22%3A%20%22totals%22%2C%0A%20%20%20%20%20%20%20%20%225%20%C2%B7%20Your%20own%20problem%22%3A%20%22own%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20%23%20Problem%201's%20reference%3A%20last%20season's%20yields%20as%20the%20extension%20office%20has%20them%2C%20in%20tonnes%20per%20hectare.%20A%20stand-in%2C%0A%20%20%20%20%23%20typed%20in%20for%20the%20capstone%20(the%20survey%20covers%20one%20season)%3B%20replace%20it%20with%20the%20office's%20real%20figures.%0A%20%20%20%20LAST_SEASON%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Bukidnon%22%2C%20%22Camarines%20Sur%22%2C%20%22Cotabato%22%2C%20%22Ilocos%20Norte%22%2C%20%22Iloilo%22%2C%20%22Isabela%22%2C%20%22Nueva%20Ecija%22%5D%20*%202%2C%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%5D%20*%207%20%2B%20%5B%22Corn%22%5D%20*%207%2C%0A%20%20%20%20%20%20%20%20%22last_season_t_ha%22%3A%20%5B4.1%2C%203.9%2C%203.6%2C%204.3%2C%204.2%2C%204.6%2C%205.2%2C%204.0%2C%203.4%2C%203.9%2C%203.8%2C%203.9%2C%204.2%2C%204.5%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20group_slug(group%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20%22%22%22A%20group's%20name%20as%20a%20file%20name%3A%20lower%20case%2C%20words%20joined%20by%20hyphens.%22%22%22%0A%20%20%20%20return%20re.sub(r%22%5B%5Ea-z0-9%5D%2B%22%2C%20%22-%22%2C%20group.lower()).strip(%22-%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Capstone%0A%0A%20%20%20%20**Build%20something%20that%20fixes%20a%20problem.**%0A%0A%20%20%20%20The%20capstone%20is%20done%20in%20groups%3A%20your%20table%20is%20your%20group.%20Each%20group%20chooses%20one%20problem%20from%20the%20brief%20below%2C%20or%20one%20from%20its%20own%20work%2C%20and%20builds%20the%20tool%20that%20solves%20it.%20At%20the%20end%2C%20each%20group%20shows%20its%20tool%20working%2C%20to%20everyone%2C%20in%20five%20minutes.%0A%0A%20%20%20%20There%20is%20no%20single%20right%20tool.%20There%20is%20a%20user%20with%20a%20problem%2C%20and%20a%20tool%20that%20either%20solves%20it%20or%20does%20not.%20Everything%20you%20need%20you%20have%20used%20in%20the%20course%3A%20reading%20with%20the%20right%20types%20(module%203)%2C%20groups%2C%20weights%20and%20joins%20(module%204)%2C%20cleaning%20and%20dates%20(module%205)%2C%20charts%20and%20a%20marimo%20app%20(module%207).%0A%0A%20%20%20%20Every%20group%20aims%20for%20the%20baseline%20first%2C%20then%20goes%20as%20far%20as%20it%20can%3A%0A%0A%20%20%20%20%7C%20tier%20%7C%20what%20you%20show%20%7C%20what%20makes%20it%20done%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**Baseline%3A%20a%20working%20notebook**%20%7C%20a%20notebook%20that%20answers%20the%20user's%20question%20%7C%20it%20runs%20from%20top%20to%20bottom%20after%20a%20restart%2C%20its%20numbers%20are%20right%20(checked%20against%20something%20you%20worked%20out%20by%20hand)%2C%20and%20the%20answer%20is%20a%20table%20or%20chart%20the%20user%20can%20read%20without%20you%20%7C%0A%20%20%20%20%7C%20**Full%3A%20an%20interactive%20marimo%20app**%20%7C%20the%20same%20tool%20as%20an%20app%20the%20user%20runs%20%7C%20the%20user%20changes%20an%20input%20(a%20crop%2C%20a%20month%2C%20a%20file)%20and%20the%20answer%20changes%20with%20it%3B%20it%20runs%20with%20%60uv%20run%20marimo%20run%20capstone%2F%3Cyour%20group%3E.py%60%2C%20code%20hidden%20(%5Bmodule%207%2C%20section%203%5D(%2F%3Ffile%3D60_charts_and_sharing.py))%20%7C%0A%20%20%20%20%7C%20**Stretch%3A%20your%20own%20data**%20%7C%20the%20tool%20on%20a%20file%20from%20your%20work%20%7C%20the%20same%20tool%2C%20unchanged%20or%20nearly%2C%20reads%20your%20file%20and%20answers%20the%20same%20question%20%7C%0A%0A%20%20%20%20The%20timings%20are%20your%20facilitator's%3B%20on%20your%20own%2C%20give%20the%20plan%20an%20hour%20and%20the%20build%20a%20day.%0A%0A%20%20%20%20Type%20your%20group's%20name%20and%20choose%20your%20problem%3A%20the%20plan%20form%20and%20the%20checklist%20below%20use%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20group_name%20%3D%20mo.ui.text(label%3D%22Your%20group's%20name%20(your%20table)%3A%22%2C%20placeholder%3D%22like%20Table%203%22%2C%20full_width%3DTrue)%0A%20%20%20%20problem_choice%20%3D%20mo.ui.dropdown(options%3Dlist(PROBLEMS)%2C%20label%3D%22Your%20problem%3A%22%2C%20full_width%3DTrue)%0A%20%20%20%20mo.vstack(%5Bgroup_name%2C%20problem_choice%5D)%0A%20%20%20%20return%20group_name%2C%20problem_choice%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%20problem%20and%20the%20plan%0A%0A%20%20%20%20Each%20problem%20below%20has%20a%20**user**%3A%20a%20person%20who%20will%20use%20your%20tool%20and%20decide%20something%20with%20it.%20Read%20all%20five%2C%20then%20choose%20as%20a%20group.%20Choose%20the%20one%20whose%20user%20you%20understand%20best%2C%20not%20the%20one%20that%20sounds%20easiest%3A%20the%20hard%20part%20of%20every%20one%20of%20them%20is%20getting%20the%20numbers%20right.%0A%0A%20%20%20%20%23%23%23%20Problem%201%20%C2%B7%20Which%20areas'%20yields%20fell%20this%20season%3F%0A%0A%20%20%20%20**The%20user%3A**%20the%20head%20of%20a%20provincial%20extension%20office.%20Before%20planning%20next%20season's%20visits%2C%20she%20wants%20to%20know%20where%20yields%20fell%2C%20crop%20by%20crop%2C%20so%20her%20technicians%20go%20there%20first.%20Today%20she%20gets%20one%20national%20figure%2C%20weeks%20late.%0A%0A%20%20%20%20**The%20data%3A**%20the%20farm%20survey%2C%20%60data%2Ffarm_survey%2Fplots.csv%60%20and%20%60households.csv%60%20(or%20%60course.plots()%60%20and%20%60course.households()%60)%2C%20with%20the%20code%20list%20%60data%2Fpsgc_municipalities.csv%60%20for%20municipality%20names.%20Last%20season's%20figures%20are%20in%20%60LAST_SEASON%60%2C%20set%20at%20the%20top%20of%20this%20notebook%3A%20a%20stand-in%20for%20the%20office's%20own%20table%2C%20typed%20in%20for%20the%20capstone%2C%20because%20the%20survey%20covers%20one%20season.%0A%0A%20%20%20%20%7C%20tier%20%7C%20done%20looks%20like%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20baseline%20%7C%20a%20notebook%20that%20gives%2C%20for%20a%20crop%2C%20each%20province's%20**weighted**%20yield%20this%20season%20beside%20last%20season's%2C%20and%20flags%20the%20provinces%20where%20it%20fell%3B%20a%20chart%20of%20the%20same%20%7C%0A%20%20%20%20%7C%20full%20%7C%20an%20app%3A%20she%20picks%20the%20crop%2C%20and%20the%20table%20and%20chart%20change%3B%20provinces%20that%20fell%20stand%20out%20%7C%0A%20%20%20%20%7C%20stretch%20%7C%20down%20to%20municipalities%20(from%20the%20code%20list)%2C%20or%20her%20own%20office's%20survey%20%7C%0A%0A%20%20%20%20**What%20will%20go%20wrong%3A**%20yields%20from%20square-meter%20plots%20(use%20%60area_ha%60)%3B%20an%20unweighted%20mean%2C%20which%20favors%20provinces%20with%20many%20small%20sampled%20households%3B%20a%20municipality%20with%20two%20plots%20that%20%22fell%22%20by%20chance.%20Show%20how%20many%20plots%20stand%20behind%20each%20number.%20**Checked%20piece%3A**%20Exercise%20K1.1.%0A%0A%20%20%20%20%23%23%23%20Problem%202%20%C2%B7%20The%20summary%20table%2C%20retyped%20into%20Excel%20every%20quarter%0A%0A%20%20%20%20**The%20user%3A**%20a%20statistician%20in%20a%20regional%20office.%20Every%20quarter%20the%20survey%20system%20exports%20the%20raw%20files%2C%20and%20she%20spends%20a%20day%20retyping%20the%20summary%20into%20the%20workbook%20her%20director%20reads%2C%20one%20sheet%20per%20province.%20Typing%20errors%20have%20reached%20the%20director%20twice.%0A%0A%20%20%20%20**The%20data%3A**%20the%20raw%20export%2C%20%60data%2Ffarm_survey%2Fhouseholds.csv%60%20and%20%60plots.csv%60%2C%20read%20as%20they%20come%20(%60course.households(clean%3DFalse)%60%2C%20%60course.plots(clean%3DFalse)%60).%20The%20workbook's%20layout%20is%20yours%20to%20design%20with%20her%3A%20a%20**Summary**%20sheet%20with%20one%20row%20per%20province%2C%20and%20one%20sheet%20per%20province%20with%20its%20crops.%0A%0A%20%20%20%20%7C%20tier%20%7C%20done%20looks%20like%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20baseline%20%7C%20a%20notebook%20that%20reads%20the%20raw%20export%20and%20writes%20the%20formatted%20workbook%2C%20every%20sheet%2C%20with%20titles%2C%20readable%20column%20widths%20and%20thousands%20separators%3B%20reading%20it%20back%20gives%20the%20same%20numbers%20%7C%0A%20%20%20%20%7C%20full%20%7C%20an%20app%3A%20she%20presses%20a%20button%20and%20downloads%20the%20workbook%20(%60mo.download%60)%20%7C%0A%20%20%20%20%7C%20stretch%20%7C%20her%20office's%20real%20summary%20layout%2C%20or%20another%20export%20it%20receives%20%7C%0A%0A%20%20%20%20**What%20will%20go%20wrong%3A**%20the%20raw%20export%20has%20duplicate%20rows%2C%20square%20meters%20and%20blank%20crops%20(module%205%20found%20them%20all)%3B%20a%20sheet%20name%20longer%20than%2031%20characters%2C%20which%20Excel%20refuses%3B%20numbers%20written%20as%20text%2C%20which%20Excel%20will%20not%20add%20up.%20**Checked%20piece%3A**%20none%3B%20your%20check%20is%20reading%20the%20workbook%20back%20with%20%60pd.read_excel(...%2C%20sheet_name%3DNone)%60%20and%20comparing.%0A%0A%20%20%20%20%23%23%23%20Problem%203%20%C2%B7%20Monthly%20price%20files%20whose%20layout%20changes%0A%0A%20%20%20%20**The%20user%3A**%20an%20analyst%20who%20reports%20farmgate%20prices.%20A%20file%20arrives%20each%20month%3B%20in%20January%202025%20the%20system%20changed%20and%20the%20columns%20were%20renamed%20and%20reordered%2C%20and%20province%20names%20became%20codes.%20She%20also%20suspects%20a%20month%20never%20came%2C%20but%20nobody%20checked.%0A%0A%20%20%20%20**The%20data%3A**%20%60data%2Fprices%2Ffarmgate_YYYY-MM.csv%60%2C%20January%202022%20to%20December%202025%2C%20and%20the%20code%20list%20for%20the%20province%20codes.%20%60course.prices()%60%20is%20the%20course's%20own%20reader%3A%20start%20from%20it%20if%20you%20like%2C%20but%20your%20tool%20must%20say%20which%20months%20are%20missing%20and%20must%20stop%20with%20a%20clear%20message%2C%20not%20a%20wrong%20table%2C%20if%20a%20third%20layout%20arrives.%0A%0A%20%20%20%20%7C%20tier%20%7C%20done%20looks%20like%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20baseline%20%7C%20a%20notebook%20that%20reads%20every%20file%20in%20either%20layout%20into%20one%20long%20table%2C%20lists%20the%20missing%20months%2C%20and%20charts%20a%20commodity's%20price%20by%20month%20with%20the%20gap%20visible%2C%20not%20bridged%20%7C%0A%20%20%20%20%7C%20full%20%7C%20an%20app%3A%20she%20picks%20the%20commodity%20and%20the%20provinces%3B%20the%20missing%20months%20are%20marked%20on%20the%20chart%20%7C%0A%20%20%20%20%7C%20stretch%20%7C%20her%20own%20monthly%20files%2C%20in%20whatever%20layouts%20they%20come%20%7C%0A%0A%20%20%20%20**What%20will%20go%20wrong%3A**%20a%20line%20drawn%20straight%20across%20the%20missing%20month%2C%20as%20if%20it%20were%20known%3B%20province%20codes%20read%20as%20numbers%20(leading%20zero%20lost)%3B%20a%20new%20layout%20that%20reads%20without%20an%20error%20and%20fills%20the%20table%20with%20missing%20values.%20**Checked%20piece%3A**%20Exercise%20K1.2.%0A%0A%20%20%20%20%23%23%23%20Problem%204%20%C2%B7%20The%20survey's%20totals%20don't%20match%20the%20published%20ones%0A%0A%20%20%20%20**The%20user%3A**%20a%20provincial%20statistics%20officer.%20Someone%20summed%20the%20survey's%20export%20in%20a%20hurry%2C%20and%20the%20totals%20do%20not%20match%20the%20published%20table%3A%20one%20figure%20is%20out%20by%20a%20factor%20of%20hundreds.%20The%20governor's%20office%20wants%20to%20know%20which%20number%20is%20right%2C%20and%20why%20they%20differ%2C%20by%20Friday.%0A%0A%20%20%20%20**The%20data%3A**%20the%20raw%20export%20(%60course.households(clean%3DFalse)%60%2C%20%60course.plots(clean%3DFalse)%60)%2C%20the%20quick%20totals%20and%20the%20published%20table%2C%20both%20in%20section%202.%20The%20published%20table%20is%20a%20stand-in%3A%20the%20survey's%20own%20totals%2C%20rounded%20as%20an%20agency%20would%20publish%20them.%0A%0A%20%20%20%20%7C%20tier%20%7C%20done%20looks%20like%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20baseline%20%7C%20a%20tested%20function%20that%20computes%20the%20weighted%20totals%20from%20the%20raw%20export%2C%20and%20a%20table%20that%20explains%20the%20gap%20between%20the%20quick%20totals%20and%20the%20published%20ones%2C%20step%20by%20step%2C%20each%20step%20a%20number%20%7C%0A%20%20%20%20%7C%20full%20%7C%20an%20app%3A%20she%20loads%20an%20export%20and%20sees%20the%20totals%20and%20the%20explanation%20%7C%0A%20%20%20%20%7C%20stretch%20%7C%20a%20real%20published%20table%20against%20the%20survey%20it%20came%20from%20%7C%0A%0A%20%20%20%20**What%20will%20go%20wrong%3A**%20each%20trap%20moves%20the%20totals%20by%20a%20different%20amount%2C%20and%20in%20a%20different%20column%3B%20find%20each%20one's%20share%2C%20not%20just%20the%20total.%20**Checked%20piece%3A**%20Exercise%20K1.3.%0A%0A%20%20%20%20%23%23%23%20Problem%205%20%C2%B7%20Your%20own%20problem%0A%0A%20%20%20%20**The%20user%3A**%20someone%20at%20your%20work%2C%20by%20name%20or%20by%20role.%20Bring%20the%20problem%2C%20the%20data%20and%20what%20%22done%22%20looks%20like%20for%20them.%0A%0A%20%20%20%20**The%20data%3A**%20your%20own%2C%20**with%20the%20agreement%20of%20whoever%20owns%20it**%20before%20it%20comes%20into%20the%20room%3B%20tell%20your%20facilitator%20what%20it%20is.%20If%20it%20cannot%20leave%20its%20owner%2C%20make%20a%20**stand-in**%3A%20the%20same%20columns%20and%20kinds%20of%20values%2C%20invented%2C%20as%20this%20course's%20survey%20is.%0A%0A%20%20%20%20%7C%20tier%20%7C%20done%20looks%20like%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20baseline%20%7C%20a%20notebook%20that%20reads%20the%20data%20with%20the%20right%20types%20and%20answers%20the%20user's%20question%20%7C%0A%20%20%20%20%7C%20full%20%7C%20an%20app%20the%20user%20could%20run%20%7C%0A%20%20%20%20%7C%20stretch%20%7C%20your%20real%20data%2C%20if%20you%20started%20on%20a%20stand-in%20%7C%0A%0A%20%20%20%20**What%20will%20go%20wrong%3A**%20the%20data.%20Spend%20the%20first%20part%20on%20reading%20it%20right%20(module%203%2C%20section%202%2C%20and%20module%205%20are%20your%20checklist)%2C%20not%20on%20the%20chart.%20**Checked%20piece%3A**%20none%3B%20write%20one%20check%20of%20your%20own%2C%20on%20a%20small%20table%20you%20work%20out%20by%20hand.%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%20Your%20plan%0A%0A%20%20%20%20Before%20anyone%20writes%20code%2C%20answer%20these%20as%20a%20group.%20Ten%20minutes%20here%20saves%20an%20hour%20later%3A%20most%20capstones%20that%20fail%2C%20fail%20because%20the%20group%20built%20a%20tool%20for%20a%20question%20nobody%20asked%2C%20or%20found%20out%20at%20the%20demo%20that%20a%20number%20was%20wrong.%0A%0A%20%20%20%20The%20plan%20is%20saved%20as%20%60capstone%2F%3Cgroup%3E-plan.md%60%2C%20a%20text%20file.%20It%20is%20also%20your%20demo's%20script.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20plan_questions()%20-%3E%20list%5Btuple%5Bstr%2C%20str%5D%5D%3A%0A%20%20%20%20%22%22%22The%20planning%20template%3A%20each%20question's%20key%20and%20its%20text.%22%22%22%0A%20%20%20%20return%20%5B%0A%20%20%20%20%20%20%20%20(%22user%22%2C%20%22**The%20user.**%20Who%20will%20use%20the%20tool%2C%20and%20what%20will%20they%20decide%20with%20it%3F%22)%2C%0A%20%20%20%20%20%20%20%20(%22question%22%2C%20%22**The%20question.**%20In%20one%20sentence%2C%20what%20does%20the%20tool%20answer%3F%22)%2C%0A%20%20%20%20%20%20%20%20(%22done%22%2C%20%22**Done.**%20What%20does%20the%20baseline%20show%3F%20The%20full%20version%3F%20What%20would%20the%20stretch%20be%3F%22)%2C%0A%20%20%20%20%20%20%20%20(%22data%22%2C%20%22**The%20data.**%20Which%20files%2C%20which%20columns%3F%20Which%20traps%20do%20you%20expect%20(codes%2C%20units%2C%20duplicates%2C%20missing%20values%2C%20dates)%3F%22)%2C%0A%20%20%20%20%20%20%20%20(%22steps%22%2C%20%22**The%20steps.**%20The%20functions%20you%20will%20write%2C%20in%20order%2C%20each%20with%20what%20goes%20in%20and%20what%20comes%20out.%22)%2C%0A%20%20%20%20%20%20%20%20(%22check%22%2C%20%22**The%20check.**%20How%20will%20you%20know%20a%20number%20is%20right%3F%20A%20small%20table%20worked%20out%20by%20hand%2C%20a%20figure%20you%20already%20know%2C%20a%20total%20that%20must%20match.%22)%2C%0A%20%20%20%20%20%20%20%20(%22who%22%2C%20%22**Who%20does%20what.**%20Who%20reads%20the%20data%2C%20who%20writes%20which%20function%2C%20who%20builds%20the%20app%2C%20who%20keeps%20the%20demo.%22)%2C%0A%20%20%20%20%20%20%20%20(%22demo%22%2C%20%22**The%20demo.**%20What%20will%20you%20show%20working%2C%20and%20on%20which%20input%3F%22)%2C%0A%20%20%20%20%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(group_name)%3A%0A%20%20%20%20_saved%20%3D%20(CAPSTONE%20%2F%20f%22%7Bgroup_slug(group_name.value)%7D-plan.md%22)%20if%20group_slug(group_name.value)%20else%20None%0A%20%20%20%20_text%20%3D%20_saved.read_text(encoding%3D%22utf-8%22)%20if%20_saved%20and%20_saved.exists()%20else%20%22%22%0A%0A%20%20%20%20def%20_answer(key%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20found%20%3D%20re.search(rf%22%3C!--%20%7Bkey%7D%20--%3E%5Cn(.*%3F)%5Cn%3C!--%20%2F%7Bkey%7D%20--%3E%22%2C%20_text%2C%20flags%3Dre.S)%0A%20%20%20%20%20%20%20%20return%20found.group(1).strip()%20if%20found%20else%20%22%22%0A%0A%20%20%20%20plan_form%20%3D%20mo.ui.dictionary(%7Bkey%3A%20mo.ui.text_area(value%3D_answer(key)%2C%20label%3Dquestion%2C%20full_width%3DTrue%2C%20rows%3D3)%20for%20key%2C%20question%20in%20plan_questions()%7D)%0A%20%20%20%20save_plan%20%3D%20mo.ui.run_button(label%3D%22Save%20the%20plan%22)%0A%20%20%20%20mo.vstack(%5Bplan_form%2C%20save_plan%5D)%0A%20%20%20%20return%20plan_form%2C%20save_plan%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(group_name%2C%20plan_form%2C%20problem_choice%2C%20save_plan)%3A%0A%20%20%20%20mo.stop(not%20save_plan.value)%0A%20%20%20%20_slug%20%3D%20group_slug(group_name.value)%0A%20%20%20%20_missing%20%3D%20(%5B%22the%20group's%20name%22%5D%20if%20not%20_slug%20else%20%5B%5D)%20%2B%20(%5B%22the%20problem%22%5D%20if%20not%20problem_choice.value%20else%20%5B%5D)%0A%20%20%20%20if%20_missing%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22Not%20saved%20yet%3A%20choose%20%7B'%20and%20'.join(_missing)%7D%20at%20the%20top.%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20CAPSTONE.mkdir(exist_ok%3DTrue)%0A%20%20%20%20%20%20%20%20_path%20%3D%20CAPSTONE%20%2F%20f%22%7B_slug%7D-plan.md%22%0A%20%20%20%20%20%20%20%20_body%20%3D%20%5Bf%22%23%20Capstone%20plan%3A%20%7Bgroup_name.value.strip()%7D%22%2C%20f%22Problem%20%7Bproblem_choice.value%7D.%20Saved%20%7Bdate.today().isoformat()%7D.%22%5D%0A%20%20%20%20%20%20%20%20_body%20%2B%3D%20%5Bf%22%7Bquestion%7D%5Cn%5Cn%3C!--%20%7Bkey%7D%20--%3E%5Cn%7Bplan_form.value%5Bkey%5D.strip()%7D%5Cn%3C!--%20%2F%7Bkey%7D%20--%3E%22%20for%20key%2C%20question%20in%20plan_questions()%5D%0A%20%20%20%20%20%20%20%20_path.write_text(%22%5Cn%5Cn%22.join(_body)%20%2B%20%22%5Cn%22%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22Saved%3A%20%60%7B_path.relative_to(PATHS.root).as_posix()%7D%60.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20_out%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%20Build%20it%2C%20in%20groups%0A%0A%20%20%20%20**Work%20in%20your%20own%20notebook**%2C%20not%20in%20this%20one%3A%20this%20one%20is%20the%20brief%2C%20and%20the%20course's%20next%20update%20replaces%20it.%20In%20a%20terminal%20in%20the%20course%20folder%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20uv%20run%20marimo%20edit%20capstone%2F%3Cyour-group%3E.py%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20marimo%20makes%20the%20file%20if%20it%20does%20not%20exist.%20Copy%20your%20problem's%20starter%20cell%20from%20below%20into%20it%2C%20and%20build%20from%20there.%20One%20person%20types%2C%20the%20others%20read%20the%20data%2C%20check%20numbers%20by%20hand%20and%20look%20things%20up%3B%20swap%20often.%0A%0A%20%20%20%20**The%20order%20that%20works%3A**%0A%0A%20%20%20%201.%20**Read%20the%20data%20and%20look%20at%20it.**%20Its%20types%2C%20its%20first%20rows%2C%20the%20counts%20of%20its%20codes.%20Most%20of%20the%20problems'%20traps%20show%20here.%0A%20%20%20%202.%20**Write%20the%20functions%2C%20one%20at%20a%20time**%2C%20each%20checked%20on%20a%20small%20table%20whose%20answer%20you%20worked%20out%20by%20hand.%20Where%20your%20problem%20has%20a%20checked%20exercise%20below%2C%20write%20it%20here%20first%3A%20its%20check%20tells%20you%20when%20it%20is%20right.%0A%20%20%20%203.%20**Put%20them%20together%20into%20the%20notebook**%20that%20answers%20the%20question%3A%20the%20baseline.%20Restart%20and%20run%20it%20from%20the%20top%20before%20you%20call%20it%20done.%0A%20%20%20%204.%20**Then%20the%20app**%3A%20the%20same%20functions%2C%20with%20a%20%60mo.ui.dropdown%60%20or%20a%20button%20in%20front%20of%20them%20(%5Bmodule%207%2C%20section%203%5D(%2F%3Ffile%3D60_charts_and_sharing.py)).%0A%0A%20%20%20%20When%20something%20goes%20red%2C%20read%20the%20error's%20last%20line%20first%3A%20it%20names%20what%20went%20wrong%2C%20and%20usually%20where.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(group_name)%3A%0A%20%20%20%20_slug%20%3D%20group_slug(group_name.value)%0A%20%20%20%20_rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22The%20group's%20name%20typed%20at%20the%20top%22%2C%20bool(_slug)%2C%20group_name.value.strip()%2C%20%22Type%20it%20in%20the%20box%20at%20the%20top.%22)%2C%0A%20%20%20%20%20%20%20%20(%22The%20plan%20saved%22%2C%20bool(_slug)%20and%20(CAPSTONE%20%2F%20f%22%7B_slug%7D-plan.md%22).exists()%2C%20f%22capstone%2F%7B_slug%7D-plan.md%22%20if%20_slug%20else%20%22%22%2C%20%22Section%201%3A%20answer%20the%20questions%2C%20then%20Save%20the%20plan.%22)%2C%0A%20%20%20%20%20%20%20%20(%22The%20group's%20notebook%20made%22%2C%20bool(_slug)%20and%20(CAPSTONE%20%2F%20f%22%7B_slug%7D.py%22).exists()%2C%20f%22capstone%2F%7B_slug%7D.py%22%20if%20_slug%20else%20%22%22%2C%20f%22In%20a%20terminal%3A%20uv%20run%20marimo%20edit%20capstone%2F%7B_slug%20or%20'%3Cyour-group%3E'%7D.py%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Where%20your%20group%20is**%20(it%20reads%20the%20files%20in%20%60capstone%2F%60%3B%20run%20this%20cell%20again%20to%20refresh)%3A%22)%2C%0A%20%20%20%20%20%20%20%20pd.DataFrame(%5B%7B%22step%22%3A%20s%2C%20%22done%22%3A%20%22yes%22%20if%20ok%20else%20%22not%20yet%22%2C%20%22detail%22%3A%20d%2C%20%22next%22%3A%20%22%22%20if%20ok%20else%20n%7D%20for%20s%2C%20ok%2C%20d%2C%20n%20in%20_rows%5D)%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%20Problem%201%20%C2%B7%20Starter%3A%20yields%20by%20area%20and%20crop%0A%0A%20%20%20%20The%20starter%20reads%20the%20survey%20and%20shows%20the%20shape%20of%20the%20data.%20The%20function%20at%20its%20heart%20is%20Exercise%20K1.1%3B%20the%20rest%20is%20your%20group's.%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%20Starter%20for%20problem%201%3A%20copy%20this%20cell%20into%20your%20group's%20notebook%0A%20%20%20%20plots%20%3D%20course.plots()%20%20%23%20one%20row%20per%20plot%2C%20with%20area_ha%0A%20%20%20%20households%20%3D%20course.households()%20%20%23%20one%20row%20per%20household%2C%20with%20province%2C%20psgc%20and%20weight%0A%20%20%20%20code_list%20%3D%20course.code_list()%20%20%23%20municipality%20names%20by%20PSGC%20code%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(plots)%3A%2C%7D%20plots**%20of%20%7Bplots%5B'hh_id'%5D.nunique()%3A%2C%7D%20households%3B%20crops%3A%20%7B'%2C%20'.join(plots%5B'crop'%5D.dropna().unique())%7D.%22)%2C%0A%20%20%20%20%20%20%20%20plots.head(3)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**Last%20season's%20reference**%20(%60LAST_SEASON%60%2C%20a%20stand-in)%3A%22)%2C%0A%20%20%20%20%20%20%20%20LAST_SEASON.pivot(index%3D%22province%22%2C%20columns%3D%22crop%22%2C%20values%3D%22last_season_t_ha%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%20K1.1.%20Weighted%20yields%20by%20province%2C%20for%20one%20crop%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20number%20the%20extension%20office%20has%20never%20had%3A%20each%20province's%20yield%20for%20a%20crop%2C%20standing%20for%20all%20its%20farm%20plots%2C%20with%20the%20count%20of%20plots%20behind%20it%20so%20a%20figure%20from%20five%20plots%20is%20not%20read%20like%20one%20from%20two%20hundred.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%2C%20the%20households%20and%20a%20crop%20%E2%86%92%20one%20row%20per%20province%20where%20the%20crop%20is%20grown%2C%20the%20province%20as%20the%20index%2C%20with%20%60plots%60%20(how%20many%20of%20the%20crop's%20plots)%20and%20%60yield_t_ha%60%20(the%20weighted%20mean%20of%20the%20plots'%20yields%2C%20in%20tonnes%20per%20hectare%2C%202%20decimals).%20Lowest%20yield%20first%2C%20so%20the%20provinces%20to%20look%20at%20come%20first%3B%20ties%20by%20province%20name.%0A%0A%20%20%20%20A%20plot's%20yield%20is%20%60production_kg%20%C3%B7%20area_ha%20%C3%B7%201000%60.%20The%20weighted%20mean%20multiplies%20each%20plot's%20yield%20by%20its%20household's%20weight%2C%20adds%20those%20up%2C%20and%20divides%20by%20the%20sum%20of%20the%20weights%20(%5Bmodule%204%2C%20section%203%5D(%2F%3Ffile%3D30_summarize_and_combine.py)).%20On%20the%20farm%20survey%2C%20all%20rice%20plots%20together%20give%204.50%20t%2Fha%20this%20way.%0A%0A%20%20%20%20%7C%20province%20%7C%20plots%20%7C%20yield_t_ha%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20Camarines%20Sur%20%7C%20161%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%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20weighted_yields(plots%3A%20pd.DataFrame%2C%20households%3A%20pd.DataFrame%2C%20crop%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Per%20province%3A%20how%20many%20plots%20of%20the%20crop%2C%20and%20their%20weighted%20mean%20yield%20in%20t%2Fha.%20Lowest%20first.%22%22%22%0A%20%20%20%20%23%20Exercise%20K1.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20crop's%20plots%2C%20each%20with%20its%20household's%20province%20and%20weight%20(a%20many_to_one%20join)%0A%20%20%20%20%23%20Step%202%3A%20each%20plot's%20yield%20in%20t%2Fha%2C%20and%20yield%20%C3%97%20weight%0A%20%20%20%20%23%20Step%203%3A%20per%20province%2C%20count%20the%20plots%2C%20and%20divide%20the%20sum%20of%20yield%20%C3%97%20weight%20by%20the%20sum%20of%20weight%0A%20%20%20%20%23%20Step%204%3A%20round%20to%202%20decimals%3B%20sort%20by%20yield%2C%20lowest%20first%2C%20then%20by%20province%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20K1.1%22)%0A%0A%0A%40app.function%0Adef%20yields_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Three%20households%20and%20six%20plots%2C%20used%20by%20the%20check%20of%20Exercise%20K1.1%3A%20weights%20that%20differ%2C%20a%20plot%20in%20square%0A%20%20%20%20meters%2C%20and%20a%20crop%20grown%20in%20one%20province%20only.%22%22%22%0A%20%20%20%20households%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22A1%22%2C%20%22A2%22%2C%20%22B1%22%5D%2C%20%22province%22%3A%20%5B%22Aurora%22%2C%20%22Aurora%22%2C%20%22Benguet%22%5D%2C%20%22weight%22%3A%20%5B100.0%2C%20300.0%2C%2050.0%5D%7D)%0A%20%20%20%20plots%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B%22A1%22%2C%20%22A1%22%2C%20%22A2%22%2C%20%22B1%22%2C%20%22B1%22%2C%20%22A2%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%201%2C%202%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Rice%22%2C%20%22Rice%22%2C%20%22Rice%22%2C%20%22Corn%22%2C%20None%5D%2C%0A%20%20%20%20%20%20%20%20%22area%22%3A%20%5B1.0%2C%205000.0%2C%202.0%2C%201.0%2C%200.5%2C%201.0%5D%2C%0A%20%20%20%20%20%20%20%20%22area_unit%22%3A%20%5B%22ha%22%2C%20%22sqm%22%2C%20%22ha%22%2C%20%22ha%22%2C%20%22ha%22%2C%20%22ha%22%5D%2C%0A%20%20%20%20%20%20%20%20%22production_kg%22%3A%20%5B3000%2C%202500%2C%2011000%2C%204200%2C%201800%2C%20900%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20plots%5B%22area_ha%22%5D%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)%0A%20%20%20%20return%20plots%2C%20households%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_plots%2C%20_households%20%3D%20yields_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20K1.1%22%2C%20weighted_yields%2C%0A%20%20%20%20%20%20%20%20%5B(%22a%20small%20survey%2C%20rice%22%2C%20(_plots%2C%20_households%2C%20%22Rice%22))%2C%20(%22a%20small%20survey%2C%20corn%22%2C%20(_plots%2C%20_households%2C%20%22Corn%22))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20farm%20survey%2C%20rice%22%2C%20(course.plots()%2C%20course.households()%2C%20%22Rice%22))%2C%20(%22the%20farm%20survey%2C%20banana%22%2C%20(course.plots()%2C%20course.households()%2C%20%22Banana%22))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20survey%2C%20Aurora's%20rice%20is%20three%20plots%3A%20yields%203.0%2C%205.0%20and%205.5%20t%2Fha%2C%20weighted%20100%2C%20100%20and%20300.%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%20crop_choice%20%3D%20mo.ui.dropdown(options%3D%5B%22Rice%22%2C%20%22Corn%22%2C%20%22Vegetables%22%2C%20%22Banana%22%5D%2C%20value%3D%22Rice%22%2C%20label%3D%22Try%20it%20with%20the%20crop%3A%22)%0A%20%20%20%20crop_choice%0A%20%20%20%20return%20(crop_choice%2C)%0A%0A%0A%40app.cell%0Adef%20_(crop_choice)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20weighted%20yields%20for%20the%20crop%20chosen%20above.%20In%20your%20app%2C%20this%20dropdown%20is%20the%20user's.%0A%20%20%20%20course.try_it(weighted_yields%2C%20course.plots()%2C%20course.households()%2C%20crop_choice.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%20K1.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%20Aurora's%20rice%20is%204.5%20in%20the%20small%20survey%20%7C%20Unweighted%3A%20the%20mean%20of%203.0%2C%205.0%20and%205.5.%20%7C%20multiply%20by%20%60weight%60%20before%20adding%20up%20%7C%0A%20%20%20%20%7C%20Aurora's%20rice%20is%204.71%20or%205.13%20in%20the%20small%20survey%20%7C%20Total%20harvest%20%C3%B7%20total%20area%20(unweighted%20or%20weighted)%2C%20not%20the%20mean%20of%20the%20plots'%20yields.%20%7C%20the%20definition%20above%20%7C%0A%20%20%20%20%7C%20One%20plot's%20yield%20is%20tiny%20%7C%20%60area%60%20instead%20of%20%60area_ha%60.%20%7C%20Step%202%20%7C%0A%20%20%20%20%7C%20Yields%20in%20the%20thousands%20%7C%20Kilograms%20per%20hectare%2C%20not%20tonnes.%20%7C%20%60%2F%201000%60%20%7C%0A%20%20%20%20%7C%20The%20right%20numbers%20in%20another%20order%20%7C%20Not%20sorted%20lowest%20first%2C%20or%20ties%20not%20broken%20by%20province.%20%7C%20%60sort_values(%5B%22yield_t_ha%22%2C%20%22province%22%5D)%60%20%7C%0A%20%20%20%20%7C%20A%20province%20with%20no%20plots%20of%20the%20crop%20appears%20%7C%20The%20households%20were%20the%20left%20side%20of%20the%20join.%20%7C%20start%20from%20the%20crop's%20plots%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%20Problem%202%20%C2%B7%20Starter%3A%20one%20formatted%20sheet%2C%20downloaded%0A%0A%20%20%20%20The%20starter%20writes%20one%20small%20table%20to%20one%20sheet%2C%20the%20way%20the%20survey's%20own%20workbook%20is%20laid%20out%20(a%20title%20and%20a%20note%20above%20the%20table%2C%20which%20starts%20on%20row%204)%2C%20sets%20column%20widths%20and%20thousands%20separators%2C%20and%20offers%20the%20file%20as%20a%20download.%20Your%20workbook%20has%20more%20sheets%2C%20from%20the%20raw%20export%3A%20the%20pattern%20is%20the%20same%20for%20each.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20workbook_bytes(sheets%3A%20dict%5Bstr%2C%20pd.DataFrame%5D%2C%20title%3A%20str%2C%20note%3A%20str)%20-%3E%20bytes%3A%0A%20%20%20%20%22%22%22An%20Excel%20workbook%2C%20in%20memory%2C%20with%20one%20sheet%20per%20table%3A%20a%20title%20and%20a%20note%20above%20each%20table%2C%20readable%20column%0A%20%20%20%20widths%2C%20and%20whole%20numbers%20with%20thousands%20separators.%20The%20starter%20for%20problem%202.%22%22%22%0A%20%20%20%20buffer%20%3D%20io.BytesIO()%0A%20%20%20%20with%20pd.ExcelWriter(buffer%2C%20engine%3D%22openpyxl%22)%20as%20writer%3A%0A%20%20%20%20%20%20%20%20for%20name%2C%20table%20in%20sheets.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20table.to_excel(writer%2C%20sheet_name%3Dname%5B%3A31%5D%2C%20startrow%3D3)%20%20%23%20Excel%20refuses%20sheet%20names%20over%2031%20characters%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet%20%3D%20writer.sheets%5Bname%5B%3A31%5D%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet%5B%22A1%22%5D%20%3D%20title%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet%5B%22A1%22%5D.font%20%3D%20Font(bold%3DTrue%2C%20size%3D14)%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet%5B%22A2%22%5D%20%3D%20note%0A%20%20%20%20%20%20%20%20%20%20%20%20headings%20%3D%20%5Bstr(table.index.name%20or%20%22%22)%5D%20%2B%20%5Bstr(c)%20for%20c%20in%20table.columns%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20position%2C%20heading%20in%20enumerate(headings%2C%20start%3D1)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sheet.column_dimensions%5Bget_column_letter(position)%5D.width%20%3D%20max(14%2C%20len(heading)%20%2B%202)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20row%20in%20sheet.iter_rows(min_row%3D5%2C%20min_col%3D2%2C%20max_col%3Dlen(headings))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20cell%20in%20row%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cell.number_format%20%3D%20%22%23%2C%23%230%22%0A%20%20%20%20return%20buffer.getvalue()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Starter%20for%20problem%202%3A%20copy%20this%20cell%20(and%20workbook_bytes%2C%20above)%20into%20your%20group's%20notebook%0A%20%20%20%20_summary%20%3D%20course.households().groupby(%22province%22).agg(sampled%3D(%22hh_id%22%2C%20%22size%22)%2C%20farm_households%3D(%22weight%22%2C%20%22sum%22)).round(0).astype(int)%0A%20%20%20%20_workbook%20%3D%20workbook_bytes(%7B%22Summary%22%3A%20_summary%7D%2C%20title%3D%22Farm%20households%20by%20province%22%2C%20note%3Df%22Farm%20survey%202025.%20Made%20%7Bdate.today()%3A%25d%20%25B%20%25Y%7D.%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_summary%2C%0A%20%20%20%20%20%20%20%20mo.download(data%3D_workbook%2C%20filename%3D%22farm_survey_summary.xlsx%22%2C%20label%3D%22Download%20the%20workbook%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22Read%20back%2C%20as%20a%20check%3A%20%22%20%2B%20%22%2C%20%22.join(f%22sheet%20**%7Bname%7D**%2C%20%7Blen(table)%7D%20rows%22%20for%20name%2C%20table%20in%20pd.read_excel(io.BytesIO(_workbook)%2C%20sheet_name%3DNone%2C%20header%3D3).items()))%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%20Problem%203%20%C2%B7%20Starter%3A%20the%20price%20files%20and%20their%20two%20layouts%0A%0A%20%20%20%20The%20starter%20lists%20the%20files%20and%20shows%20one%20of%20each%20layout.%20Look%20at%20what%20changed%3A%20the%20column%20names%2C%20their%20order%2C%20and%20the%20province%2C%20which%20became%20a%20code.%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%20Starter%20for%20problem%203%3A%20copy%20this%20cell%20into%20your%20group's%20notebook%0A%20%20%20%20price_files%20%3D%20sorted(PATHS.prices.glob(%22farmgate_*.csv%22))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(price_files)%7D%20files**%2C%20from%20%60%7Bprice_files%5B0%5D.name%7D%60%20to%20%60%7Bprice_files%5B-1%5D.name%7D%60.%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**The%20old%20layout**%20(to%20December%202024)%3A%22)%2C%20pd.read_csv(price_files%5B0%5D).head(3)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**The%20new%20layout**%20(from%20January%202025)%3A%22)%2C%20pd.read_csv(PATHS.prices%20%2F%20%22farmgate_2025-01.csv%22%2C%20dtype%3D%7B%22Province%20Code%22%3A%20str%7D).head(3)%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%20K1.2.%20The%20months%20whose%20file%20never%20came%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20missing%20month%20is%20invisible%20in%20a%20combined%20table%3A%20there%20are%20simply%20no%20rows%20for%20it%2C%20and%20a%20line%20chart%20draws%20straight%20across.%20The%20analyst's%20report%20must%20say%20which%20months%20are%20missing%2C%20every%20time%2C%20without%20anyone%20looking.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20list%20of%20file%20names%20(as%20%60%5Bp.name%20for%20p%20in%20folder.iterdir()%5D%60%20gives%20them)%20%E2%86%92%20the%20months%20between%20the%20first%20and%20the%20last%20price%20file%20that%20have%20no%20file%2C%20as%20texts%20%60%22YYYY-MM%22%60%2C%20in%20order.%20A%20price%20file%20is%20named%20exactly%20%60farmgate_YYYY-MM.csv%60%3B%20any%20other%20name%20in%20the%20folder%20(a%20%60README.md%60%2C%20a%20copy%20named%20%60farmgate_2024-01%20(1).csv%60%2C%20a%20%60farmgate_2023-10_draft.csv%60)%20is%20not%20a%20price%20file%20and%20is%20ignored.%20No%20price%20files%20at%20all%3A%20an%20empty%20list.%0A%0A%20%20%20%20%7C%20file%20names%20%7C%20missing%20months%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60farmgate_2024-11.csv%60%2C%20%60farmgate_2025-02.csv%60%2C%20%60farmgate_2024-12.csv%60%20%7C%20%60%5B%222025-01%22%5D%60%20%7C%0A%20%20%20%20%7C%20%60farmgate_2023-01.csv%60%2C%20%60farmgate_2023-04.csv%60%20%7C%20%60%5B%222023-02%22%2C%20%222023-03%22%5D%60%20%7C%0A%0A%20%20%20%20Tools%3A%20%60re.fullmatch(r%22farmgate_(%5Cd%7B4%7D-%5Cd%7B2%7D)%5C.csv%22%2C%20name)%60%20picks%20out%20the%20month%20(Python%20Essentials%2C%20text)%3B%20%60pd.period_range(first%2C%20last%2C%20freq%3D%22M%22)%60%20lists%20every%20month%20between%20two%20(%5Bmodule%205%2C%20section%204%5D(%2F%3Ffile%3D40_cleaning_and_time.py))%2C%20and%20%60.strftime(%22%25Y-%25m%22)%60%20writes%20each%20as%20text.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20missing_months(names%3A%20list%5Bstr%5D)%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20months%20between%20the%20first%20and%20the%20last%20price%20file%20that%20have%20no%20file%2C%20as%20%22YYYY-MM%22%20texts%2C%20in%20order.%22%22%22%0A%20%20%20%20%23%20Exercise%20K1.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20months%20of%20the%20names%20that%20are%20price%20files%20(farmgate_YYYY-MM.csv%20exactly)%2C%20sorted%3B%20none%3A%20return%20%5B%5D%0A%20%20%20%20%23%20Step%202%3A%20every%20month%20from%20the%20first%20to%20the%20last%2C%20as%20%22YYYY-MM%22%20texts%0A%20%20%20%20%23%20Step%203%3A%20keep%20the%20months%20that%20have%20no%20file%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20K1.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%20K1.2%22%2C%20missing_months%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%20course's%20price%20files%22%2C%20(%5Bp.name%20for%20p%20in%20PATHS.prices.iterdir()%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20files%20out%20of%20order%2C%20across%20a%20new%20year%22%2C%20(%5B%22farmgate_2024-11.csv%22%2C%20%22farmgate_2025-02.csv%22%2C%20%22farmgate_2024-12.csv%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20months%20in%20a%20row%20missing%22%2C%20(%5B%22farmgate_2023-01.csv%22%2C%20%22farmgate_2023-04.csv%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22nothing%20missing%22%2C%20(%5B%22farmgate_2022-01.csv%22%2C%20%22farmgate_2022-02.csv%22%2C%20%22farmgate_2022-03.csv%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20README%2C%20a%20copy%20and%20a%20draft%20among%20the%20files%22%2C%20(%5B%22README.md%22%2C%20%22farmgate_2024-01.csv%22%2C%20%22farmgate_2024-01%20(1).csv%22%2C%20%22farmgate_2023-10_draft.csv%22%2C%20%22farmgate_2024-03.csv%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22no%20price%20files%20at%20all%22%2C%20(%5B%22notes.txt%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%22folders%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Only%20names%20that%20are%20exactly%20farmgate_YYYY-MM.csv%20count%2C%20in%20any%20order%3B%20the%20answer%20is%20a%20list%20of%20texts%20like%20%5C%222023-09%5C%22.%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%20months%20missing%20from%20the%20course's%20price%20files.%0A%20%20%20%20course.try_it(missing_months%2C%20%5Bp.name%20for%20p%20in%20PATHS.prices.iterdir()%5D)%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%20K1.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%20Wrong%20across%20the%20new%20year%20%7C%20The%20months%20were%20not%20sorted%2C%20or%20were%20compared%20as%20numbers.%20%7C%20%60sorted(...)%60%20of%20the%20texts%2C%20then%20%60period_range%60%20%7C%0A%20%20%20%20%7C%20Months%20before%20the%20first%20file%20are%20listed%20%7C%20A%20draft%20or%20another%20file%20was%20taken%20for%20a%20price%20file.%20%7C%20%60re.fullmatch%60%2C%20not%20%60re.search%60%20%7C%0A%20%20%20%20%7C%20*ValueError*%20or%20*IndexError*%20with%20no%20price%20files%20%7C%20%60found%5B0%5D%60%20of%20an%20empty%20list.%20%7C%20return%20%60%5B%5D%60%20first%20%7C%0A%20%20%20%20%7C%20Periods%20instead%20of%20texts%20%7C%20%60period_range%60%20gives%20periods.%20%7C%20%60.strftime(%22%25Y-%25m%22)%60%20%7C%0A%20%20%20%20%7C%20Every%20month%20is%20listed%20%7C%20The%20comparison%20is%20between%20a%20period%20and%20a%20text.%20%7C%20compare%20texts%20with%20texts%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%20Problem%204%20%C2%B7%20Starter%3A%20the%20quick%20totals%20and%20the%20published%20ones%0A%0A%20%20%20%20Here%20are%20the%20two%20tables%20that%20do%20not%20agree.%20The%20**quick%20totals**%20are%20what%20was%20summed%20in%20a%20hurry%20from%20the%20raw%20export%3A%20every%20row%20as%20it%20came%2C%20areas%20added%20as%20written.%20The%20**published%20table**%20is%20the%20stand-in%3A%20the%20survey's%20own%20weighted%20totals%2C%20rounded%20as%20an%20agency%20publishes%20them%20(farm%20households%20to%20the%20nearest%20100%2C%20hectares%20and%20tonnes%20to%20the%20nearest%2010).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20quick_totals()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20totals%20as%20they%20were%20summed%20in%20a%20hurry%20from%20the%20raw%20export%3A%20no%20duplicates%20removed%2C%20areas%20added%20as%20written.%22%22%22%0A%20%20%20%20homes%20%3D%20pd.read_csv(PATHS.survey%20%2F%20%22households.csv%22%2C%20dtype%3D%7B%22psgc%22%3A%20str%7D)%0A%20%20%20%20fields%20%3D%20pd.read_csv(PATHS.survey%20%2F%20%22plots.csv%22%2C%20thousands%3D%22%2C%22)%0A%20%20%20%20rice%20%3D%20fields%5Bfields%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D.merge(homes%5B%5B%22hh_id%22%2C%20%22province%22%2C%20%22weight%22%5D%5D%2C%20on%3D%22hh_id%22)%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22farm_households%22%3A%20homes.groupby(%22province%22)%5B%22weight%22%5D.sum()%2C%0A%20%20%20%20%20%20%20%20%22rice_area_ha%22%3A%20(rice%5B%22area%22%5D%20*%20rice%5B%22weight%22%5D).groupby(rice%5B%22province%22%5D).sum()%2C%0A%20%20%20%20%20%20%20%20%22rice_production_t%22%3A%20(rice%5B%22production_kg%22%5D%20*%20rice%5B%22weight%22%5D).groupby(rice%5B%22province%22%5D).sum()%20%2F%201000%2C%0A%20%20%20%20%7D).round(0).astype(int)%0A%0A%0A%40app.function%0Adef%20published_totals()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20published%20table%2C%20a%20stand-in%20for%20the%20capstone%3A%20the%20survey's%20weighted%20totals%20from%20the%20clean%20tables%2C%20rounded%0A%20%20%20%20as%20an%20agency%20would%20publish%20them%20(farm%20households%20to%20the%20nearest%20100%2C%20hectares%20and%20tonnes%20to%20the%20nearest%2010).%22%22%22%0A%20%20%20%20homes%2C%20fields%20%3D%20course.households()%2C%20course.plots()%0A%20%20%20%20rice%20%3D%20fields%5Bfields%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D.merge(homes%5B%5B%22hh_id%22%2C%20%22province%22%2C%20%22weight%22%5D%5D%2C%20on%3D%22hh_id%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20exact%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22farm_households%22%3A%20homes.groupby(%22province%22)%5B%22weight%22%5D.sum()%2C%0A%20%20%20%20%20%20%20%20%22rice_area_ha%22%3A%20(rice%5B%22area_ha%22%5D%20*%20rice%5B%22weight%22%5D).groupby(rice%5B%22province%22%5D).sum()%2C%0A%20%20%20%20%20%20%20%20%22rice_production_t%22%3A%20(rice%5B%22production_kg%22%5D%20*%20rice%5B%22weight%22%5D).groupby(rice%5B%22province%22%5D).sum()%20%2F%201000%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22farm_households%22%3A%20(exact%5B%22farm_households%22%5D%20%2F%20100).round()%20*%20100%2C%0A%20%20%20%20%20%20%20%20%22rice_area_ha%22%3A%20(exact%5B%22rice_area_ha%22%5D%20%2F%2010).round()%20*%2010%2C%0A%20%20%20%20%20%20%20%20%22rice_production_t%22%3A%20(exact%5B%22rice_production_t%22%5D%20%2F%2010).round()%20*%2010%2C%0A%20%20%20%20%7D).astype(int)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Starter%20for%20problem%204%3A%20copy%20this%20cell%20(and%20the%20two%20functions%20above)%20into%20your%20group's%20notebook%0A%20%20%20%20quick%2C%20published%20%3D%20quick_totals()%2C%20published_totals()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Quick%20totals**%2C%20from%20the%20raw%20export%3A%22)%2C%20quick%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**Published**%20(the%20stand-in)%3A%22)%2C%20published%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**Quick%20minus%20published.**%20Every%20column%20is%20off%2C%20and%20one%20by%20far%20more%20than%20the%20others%3A%22)%2C%20quick%20-%20published%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(published%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%20K1.3.%20The%20weighted%20totals%2C%20from%20the%20raw%20export%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20function%20the%20officer%20will%20defend%20on%20Friday.%20It%20must%20take%20the%20export%20exactly%20as%20it%20comes%20and%20give%20the%20totals%20the%20published%20table%20rounds%2C%20so%20that%20the%20only%20difference%20left%20is%20rounding.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20raw%20households%20and%20plots%20(%60course.households(clean%3DFalse)%60%2C%20%60course.plots(clean%3DFalse)%60%3A%20production%20already%20numbers%2C%20every%20trap%20still%20there)%20%E2%86%92%20one%20row%20per%20province%2C%20the%20province%20as%20the%20index%2C%20with%20whole%20numbers%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20what%20it%20is%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60farm_households%60%20%7C%20the%20sum%20of%20the%20households'%20weights%20%7C%0A%20%20%20%20%7C%20%60rice_area_ha%60%20%7C%20the%20weighted%20total%20area%20of%20rice%20plots%2C%20in%20hectares%20%7C%0A%20%20%20%20%7C%20%60rice_production_t%60%20%7C%20the%20weighted%20total%20rice%20harvest%2C%20in%20tonnes%20%7C%0A%0A%20%20%20%20Every%20province%20of%20the%20households%20has%20a%20row%3B%20a%20province%20with%20no%20rice%20plots%20shows%200%20for%20rice%20(a%20weighted%20total%20of%20nothing%20is%200%2C%20not%20unknown).%0A%0A%20%20%20%20Tools%3A%20%60drop_duplicates()%60%20(%5Bmodule%205%2C%20section%205%5D(%2F%3Ffile%3D40_cleaning_and_time.py))%2C%20one%20unit%20for%20area%20(%5Bmodule%205%2C%20section%202%5D(%2F%3Ffile%3D40_cleaning_and_time.py))%2C%20weighted%20totals%20(%5Bmodule%204%2C%20section%203%5D(%2F%3Ffile%3D30_summarize_and_combine.py))%2C%20%60merge(...%2C%20validate%3D%22many_to_one%22)%60%20(%5Bmodule%204%2C%20section%205%5D(%2F%3Ffile%3D30_summarize_and_combine.py)).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20weighted_totals(households%3A%20pd.DataFrame%2C%20plots%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Per%20province%2C%20from%20the%20raw%20export%3A%20farm%20households%2C%20rice%20area%20in%20hectares%20and%20rice%20harvest%20in%20tonnes%2C%20weighted.%22%22%22%0A%20%20%20%20%23%20Exercise%20K1.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20drop%20the%20duplicate%20rows%20of%20both%20tables%0A%20%20%20%20%23%20Step%202%3A%20every%20plot's%20area%20in%20hectares%20(square%20meters%20%C3%B7%2010%2C000)%0A%20%20%20%20%23%20Step%203%3A%20the%20rice%20plots%2C%20each%20with%20its%20household's%20province%20and%20weight%20(a%20many_to_one%20join)%0A%20%20%20%20%23%20Step%204%3A%20per%20province%3A%20the%20sum%20of%20weights%3B%20the%20sums%20of%20area%20%C3%97%20weight%20and%20harvest%20%C3%97%20weight%20(%C3%B7%201000%20for%20tonnes)%0A%20%20%20%20%23%20Step%205%3A%20one%20table%3B%20a%20province%20with%20no%20rice%20gets%200%3B%20rounded%20to%20whole%20numbers%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20K1.3%22)%0A%0A%0A%40app.function%0Adef%20totals_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22A%20small%20raw%20export%2C%20used%20by%20the%20check%20of%20Exercise%20K1.3%3A%20a%20household%20entered%20twice%2C%20a%20plot%20entered%20twice%2C%20a%20plot%0A%20%20%20%20in%20square%20meters%2C%20a%20plot%20with%20no%20crop%2C%20and%20a%20province%20that%20grows%20no%20rice.%22%22%22%0A%20%20%20%20households%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B%22A1%22%2C%20%22A2%22%2C%20%22A2%22%2C%20%22B1%22%5D%2C%20%20%23%20A2%20entered%20twice%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Aurora%22%2C%20%22Aurora%22%2C%20%22Aurora%22%2C%20%22Benguet%22%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B100.0%2C%20300.0%2C%20300.0%2C%2050.0%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20plots%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B%22A1%22%2C%20%22A1%22%2C%20%22A1%22%2C%20%22A2%22%2C%20%22A2%22%2C%20%22B1%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%202%2C%201%2C%202%2C%201%5D%2C%20%20%23%20A1's%20plot%202%20entered%20twice%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Rice%22%2C%20%22Rice%22%2C%20%22Rice%22%2C%20None%2C%20%22Corn%22%5D%2C%0A%20%20%20%20%20%20%20%20%22area%22%3A%20%5B1.2%2C%204000.0%2C%204000.0%2C%202.0%2C%200.5%2C%201.0%5D%2C%0A%20%20%20%20%20%20%20%20%22area_unit%22%3A%20%5B%22ha%22%2C%20%22sqm%22%2C%20%22sqm%22%2C%20%22ha%22%2C%20%22ha%22%2C%20%22ha%22%5D%2C%0A%20%20%20%20%20%20%20%20%22production_kg%22%3A%20%5B4800%2C%201800%2C%201800%2C%209000%2C%20700%2C%203500%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%20households%2C%20plots%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%20K1.3%22%2C%20weighted_totals%2C%0A%20%20%20%20%20%20%20%20%5B(%22a%20small%20raw%20export%22%2C%20totals_fixture())%2C%20(%22the%20farm%20survey's%20raw%20export%22%2C%20(course.households(clean%3DFalse)%2C%20course.plots(clean%3DFalse)))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22exports%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20export%2C%20Aurora%20has%20400%20farm%20households%20(A2%20once)%2C%20and%20its%20rice%20area%20is%201.2%20%C3%97%20100%20%2B%200.4%20%C3%97%20100%20%2B%202.0%20%C3%97%20300%20%3D%20760%20ha%3B%20Benguet%20grows%20no%20rice%3A%200.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20totals%20from%20the%20farm%20survey's%20raw%20export.%0A%20%20%20%20course.try_it(weighted_totals%2C%20course.households(clean%3DFalse)%2C%20course.plots(clean%3DFalse))%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%20K1.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%20Aurora%20has%20700%20farm%20households%20in%20the%20small%20export%20%7C%20The%20duplicate%20household%20was%20not%20dropped.%20%7C%20%60households.drop_duplicates()%60%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20Merge%20keys%20are%20not%20unique%20in%20right%20dataset*%20%7C%20The%20same%3A%20a%20household%20still%20listed%20twice.%20%7C%20Step%201%20before%20the%20join%20%7C%0A%20%20%20%20%7C%20Rice%20area%20in%20the%20thousands%20%7C%20Square%20meters%20added%20as%20hectares.%20%7C%20Step%202%20%7C%0A%20%20%20%20%7C%20Aurora's%20rice%20area%20is%20800%20%7C%20The%20duplicate%20plot%20was%20counted%20twice.%20%7C%20%60plots.drop_duplicates()%60%20%7C%0A%20%20%20%20%7C%20Benguet%20is%20missing%2C%20or%20shows%20a%20missing%20value%20%7C%20The%20rice%20totals%20have%20no%20row%20for%20a%20province%20with%20no%20rice.%20%7C%20start%20the%20table%20from%20the%20households%3B%20%60fillna(0)%60%20%7C%0A%20%20%20%20%7C%20*IntCastingNaNError*%20%7C%20%60astype(int)%60%20on%20a%20missing%20value.%20%7C%20%60fillna(0)%60%20before%20%60astype(int)%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_(published)%3A%0A%20%20%20%20_totals%20%3D%20attempt(weighted_totals%2C%20course.households(clean%3DFalse)%2C%20course.plots(clean%3DFalse))%0A%20%20%20%20mo.stop(_totals%20is%20NOT_YET%2C%20waiting_for(%22Exercise%20K1.3%22))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Your%20totals%20minus%20the%20published%20ones.**%20When%20your%20function%20is%20right%2C%20what%20is%20left%20is%20rounding%3A%20at%20most%2050%20farm%20households%2C%20and%20at%20most%205%20hectares%20or%20tonnes.%22)%2C%0A%20%20%20%20%20%20%20%20_totals%20-%20published%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**Explaining%20the%20difference**%20is%20the%20other%20half%20of%20problem%204.%20Start%20from%20the%20quick%20totals%20and%20apply%20one%20correction%20at%20a%20time%2C%20recording%20the%20totals%20after%20each%3B%20the%20change%20at%20each%20step%20is%20that%20trap's%20share.%20A%20table%20that%20fits%3A%0A%0A%20%20%20%20%7C%20step%20%7C%20farm%20households%20%7C%20rice%20area%20(ha)%20%7C%20rice%20harvest%20(t)%20%7C%20why%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%20quick%20totals%20%7C%20%7C%20%7C%20%7C%20summed%20as%20exported%20%7C%0A%20%20%20%20%7C%20duplicate%20households%20removed%20%7C%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%20duplicate%20plots%20removed%20%7C%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%20square%20meters%20converted%20%7C%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%20your%20weighted%20totals%20(K1.3)%20%7C%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%20published%20%7C%20%7C%20%7C%20%7C%20rounded%20%7C%0A%0A%20%20%20%20The%20order%20of%20the%20steps%20changes%20how%20much%20each%20one%20seems%20to%20explain%20(a%20duplicated%20household%20carries%20its%20plots%2C%20square%20meters%20and%20all).%20Say%20which%20order%20you%20chose%2C%20and%20why.%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%20Problem%205%20%C2%B7%20Starter%3A%20your%20own%20file%0A%0A%20%20%20%20Put%20your%20file%20in%20the%20course%20folder%20(a%20%60my_data%2F%60%20folder%20keeps%20it%20apart)%2C%20choose%20it%20below%2C%20and%20look%20at%20it%20before%20anything%20else.%20Then%20go%20down%20the%20checklist%3A%20it%20is%20the%20course's%20list%20of%20what%20goes%20wrong%20on%20the%20way%20in.%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%20Starter%20for%20problem%205%3A%20choose%20your%20file%0A%20%20%20%20own_file%20%3D%20mo.ui.file_browser(initial_path%3DPATHS.root%2C%20filetypes%3D%5B%22.csv%22%2C%20%22.xlsx%22%2C%20%22.parquet%22%5D%2C%20multiple%3DFalse%2C%20label%3D%22Your%20file%3A%22)%0A%20%20%20%20own_file%0A%20%20%20%20return%20(own_file%2C)%0A%0A%0A%40app.cell%0Adef%20_(own_file)%3A%0A%20%20%20%20mo.stop(not%20own_file.value%2C%20mo.callout(mo.md(%22Choose%20a%20file%20above%20to%20see%20its%20first%20rows%20and%20the%20type%20of%20each%20column.%22)%2C%20kind%3D%22neutral%22))%0A%20%20%20%20_path%20%3D%20own_file.path(0)%0A%20%20%20%20_read%20%3D%20%7B%22.csv%22%3A%20pd.read_csv%2C%20%22.xlsx%22%3A%20pd.read_excel%2C%20%22.parquet%22%3A%20pd.read_parquet%7D%5B_path.suffix.lower()%5D%0A%20%20%20%20_table%20%3D%20_read(_path)%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22**%7B_path.name%7D**%3A%20%7Blen(_table)%3A%2C%7D%20rows%2C%20%7B_table.shape%5B1%5D%7D%20columns.%20The%20first%20rows%2C%20then%20each%20column's%20type%3A%22)%2C%20_table.head(5)%2C%20_table.dtypes.astype(str).rename(%22type%22)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20check%20%7C%20what%20to%20look%20for%20%7C%20where%20it%20was%20taught%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20codes%20%7C%20an%20area%20code%20or%20ID%20read%20as%20a%20number%20has%20lost%20its%20leading%20zero%3A%20read%20it%20as%20text%20%7C%20%5Bmodule%203%2C%20section%202%5D(%2F%3Ffile%3D20_dataframes.py)%20%7C%0A%20%20%20%20%7C%20numbers%20as%20text%20%7C%20a%20column%20of%20numbers%20with%20type%20%60str%60%3A%20thousands%20separators%2C%20or%20a%20stray%20word%20%7C%20%5Bmodule%203%2C%20section%202%5D(%2F%3Ffile%3D20_dataframes.py)%20%7C%0A%20%20%20%20%7C%20header%20rows%20%7C%20a%20title%20above%20the%20table%20in%20Excel%3A%20%60header%3D%60%20%7C%20%5Bmodule%203%2C%20section%202%5D(%2F%3Ffile%3D20_dataframes.py)%20%7C%0A%20%20%20%20%7C%20missing%20and%20not-stated%20%7C%20blanks%2C%20and%20codes%20like%209%20or%20999%20that%20mean%20%22not%20stated%22%20%7C%20%5Bmodule%205%2C%20section%201%5D(%2F%3Ffile%3D40_cleaning_and_time.py)%20%7C%0A%20%20%20%20%7C%20units%20%7C%20one%20measure%20in%20two%20units%20(hectares%20and%20square%20meters)%20%7C%20%5Bmodule%205%2C%20section%202%5D(%2F%3Ffile%3D40_cleaning_and_time.py)%20%7C%0A%20%20%20%20%7C%20dates%20%7C%20day%20first%20or%20month%20first%3F%20%7C%20%5Bmodule%205%2C%20section%203%5D(%2F%3Ffile%3D40_cleaning_and_time.py)%20%7C%0A%20%20%20%20%7C%20duplicates%20%7C%20rows%20entered%20twice%3B%20keys%20that%20should%20be%20unique%20%7C%20%5Bmodule%205%2C%20section%205%5D(%2F%3Ffile%3D40_cleaning_and_time.py)%20%7C%0A%20%20%20%20%7C%20weights%20%7C%20if%20it%20is%20a%20survey%2C%20which%20column%20says%20how%20many%20each%20row%20stands%20for%20%7C%20%5Bmodule%204%2C%20section%203%5D(%2F%3Ffile%3D30_summarize_and_combine.py)%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20Demos%0A%0A%20%20%20%20Each%20group%20shows%20its%20tool%20working%2C%20from%20one%20laptop%2C%20in%20**five%20minutes**%3A%20a%20hard%20stop%2C%20so%20that%20every%20group%20gets%20its%20turn.%0A%0A%20%20%20%20%7C%20minutes%20%7C%20what%20%7C%20say%20or%20show%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%7C%20**the%20problem**%20%7C%20who%20the%20user%20is%2C%20what%20they%20decide%2C%20and%20the%20question%20in%20one%20sentence%20(your%20plan's%20first%20two%20answers)%20%7C%0A%20%20%20%20%7C%203%20%7C%20**the%20tool%20working**%20%7C%20live%2C%20on%20real%20input%3A%20the%20notebook%20run%20from%20the%20top%2C%20or%20the%20app%20with%20%60uv%20run%20marimo%20run%20capstone%2F%3Cyour-group%3E.py%60%3B%20change%20an%20input%20and%20let%20the%20answer%20change%3B%20point%20at%20one%20number%20and%20say%20how%20you%20know%20it%20is%20right%20%7C%0A%20%20%20%20%7C%201%20%7C%20**one%20thing%20that%20went%20wrong**%20%7C%20what%20it%20was%2C%20how%20you%20noticed%2C%20what%20you%20changed%3A%20a%20trap%20in%20the%20data%2C%20a%20wrong%20number%2C%20an%20error%20you%20learned%20to%20read%20%7C%0A%0A%20%20%20%20The%20last%20minute%20is%20the%20one%20people%20remember.%20A%20tool%20that%20works%20because%20the%20group%20caught%20a%20wrong%20number%20is%20a%20better%20story%20than%20one%20that%20never%20had%20any.%0A%0A%20%20%20%20**Before%20your%20turn%2C%20on%20the%20presenting%20laptop%3A**%0A%0A%20%20%20%201.%20Restart%20your%20notebook%20and%20run%20it%20from%20the%20top%3A%20it%20must%20run%20without%20you%20fixing%20anything.%0A%20%20%20%202.%20If%20you%20built%20the%20app%2C%20start%20it%20with%20%60uv%20run%20marimo%20run%20capstone%2F%3Cyour-group%3E.py%60%20and%20leave%20it%20open.%0A%20%20%20%203.%20Check%20the%20data%20is%20where%20the%20notebook%20looks%20for%20it%20on%20this%20laptop%2C%20especially%20your%20own%20file.%0A%20%20%20%204.%20Open%20your%20plan%20(%60capstone%2F%3Cyour-group%3E-plan.md%60)%3A%20it%20is%20your%20script.%0A%0A%20%20%20%20**While%20the%20others%20present%2C**%20write%20down%20one%20question%20for%20each%20group.%20Good%20questions%20name%20a%20number%3A%20%22How%20do%20you%20know%20that%20one%20is%20right%3F%22%2C%20%22What%20would%20the%20user%20do%20if%20this%20were%20missing%3F%22%2C%20%22What%20happens%20next%20month%3F%22%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**What%20you%20can%20do%20now%2C%20after%20the%20course**%0A%0A%20%20%20%20-%20Read%20a%20file%20with%20the%20right%20types%20the%20first%20time%3A%20codes%20as%20text%2C%20numbers%20with%20separators%2C%20Excel's%20header%20rows%2C%20dates%20day%20first.%0A%20%20%20%20-%20Clean%20what%20the%20file%20gets%20wrong%2C%20as%20code%20you%20can%20run%20again%3A%20missing%20and%20not-stated%20values%2C%20duplicates%2C%20one%20unit%20for%20a%20measure.%0A%20%20%20%20-%20Summarize%20by%20group%2C%20within%20groups%20and%20with%20survey%20weights%2C%20and%20know%20when%20an%20unweighted%20number%20misleads.%0A%20%20%20%20-%20Reshape%20between%20long%20and%20wide%2C%20and%20join%20tables%20without%20losing%20or%20multiplying%20rows.%0A%20%20%20%20-%20Work%20with%20months%20and%20prices%20over%20time%2C%20gaps%20included.%0A%20%20%20%20-%20Do%20the%20same%20in%20polars%2C%20and%20choose%20between%20the%20two.%0A%20%20%20%20-%20Choose%20the%20chart%20that%20answers%20the%20question%2C%20in%20matplotlib%2C%20seaborn%20or%20Altair%2C%20and%20put%20it%20in%20an%20app%20someone%20else%20can%20use.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%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
17e0a19c478c18b17e0d1cbb4fba0e60