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%22Stretch%3A%20DataFrames%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%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%2C%20the%20survey%20read%20the%20way%20the%20course%20teaches%2C%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20check_cases%0A%0A%20%20%20%20%23%20The%20four%20kinds%20of%20plot%20in%20Exercise%20S1.4%2C%20in%20the%20order%20the%20table%20shows%20them%0A%20%20%20%20PLOT_TYPES%20%3D%20%5B%22small%2C%20low%20yield%22%2C%20%22small%2C%20high%20yield%22%2C%20%22large%2C%20low%20yield%22%2C%20%22large%2C%20high%20yield%22%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Stretch%3A%20DataFrames%0A%0A%20%20%20%20For%20when%20you%20have%20finished%20module%205%20(%6040_cleaning_and_time.py%60)%20and%20want%20more.%20Six%20exercises%20on%20the%20farm%20survey%2C%20each%20going%20a%20little%20further%20than%20the%20modules%2C%20with%20less%20help%3A%20a%20task%2C%20an%20example%2C%20a%20check.%20Do%20them%20in%20any%20order%3B%20none%20of%20them%20is%20needed%20later%20in%20the%20course.%0A%0A%20%20%20%20They%20come%20from%20the%20DataFrame%20exercises%20of%20an%20earlier%20version%20of%20this%20course%3A%20**fertilizer%20use%20and%20crop%20response**%2C%20**labor%20productivity**%2C%20and%20**a%20farm%20typology**%20that%20picks%20farms%20for%20follow-up.%20Those%20exercises%20used%20small%20invented%20tables%3B%20here%20they%20run%20on%20the%20whole%20survey%2C%20with%20its%20survey%20weights%2C%20its%20missing%20values%20and%20its%20code%20list.%20Where%20the%20survey%20cannot%20answer%20the%20old%20question%20as%20asked%2C%20the%20exercise%20says%20so%20and%20answers%20the%20closest%20honest%20one.%0A%0A%20%20%20%20Every%20exercise%20starts%20from%20the%20clean%20tables%2C%20read%20the%20way%20module%205%20leaves%20them%3A%0A%0A%20%20%20%20%7C%20call%20%7C%20gives%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60course.plots()%60%20%7C%20one%20row%20per%20plot%2C%20duplicates%20dropped%2C%20with%20%60area_ha%60%3A%20every%20area%20in%20hectares%20%7C%0A%20%20%20%20%7C%20%60course.households()%60%20%7C%20one%20row%20per%20household%2C%20duplicates%20dropped%2C%20not-stated%20codes%20made%20missing%20%7C%0A%20%20%20%20%7C%20%60course.code_list()%60%20%7C%20the%20PSA%20code%20list%20of%20the%20seven%20provinces'%20cities%20and%20municipalities%2C%20codes%20as%20text%20%7C%0A%0A%20%20%20%20Each%20check%20runs%20your%20function%20on%20two%20surveys%3A%20a%20**small%20survey**%20of%20four%20households%20and%20nine%20plots%2C%20built%20by%20hand%20so%20that%20every%20trap%20is%20in%20it%20once%20(a%20plot%20measured%20in%20square%20meters%2C%20a%20plot%20with%20no%20fertilizer%20recorded%2C%20a%20plot%20with%20no%20crop%2C%20ties%20at%20the%20median)%2C%20and%20the%20**farm%20survey**%20itself.%20The%20small%20survey%20is%20in%20the%20next%20cell%3A%20when%20a%20check%20is%20red%2C%20run%20your%20function%20on%20it%20and%20work%20the%20answer%20out%20by%20hand.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20small_survey()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22A%20survey%20of%20four%20households%20and%20nine%20plots%2C%20with%20one%20of%20each%20trap%2C%20used%20by%20every%20check%20in%20this%20notebook%3A%0A%20%20%20%20the%20plots%2C%20the%20households%20and%20a%20code%20list%2C%20in%20the%20shapes%20course.plots()%2C%20course.households()%20and%20course.code_list()%20give.%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%22H1%22%2C%20%22H2%22%2C%20%22H3%22%2C%20%22H4%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Aurora%22%2C%20%22Aurora%22%2C%20%22Benguet%22%2C%20%22Benguet%22%5D%2C%0A%20%20%20%20%20%20%20%20%22psgc%22%3A%20%5B%220100100000%22%2C%20%220100200000%22%2C%20%220200100000%22%2C%20%220200200000%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_size%22%3A%20%5B4%2C%203%2C%205%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B120.0%2C%20300.0%2C%2050.0%2C%20200.0%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22H1%22%2C%201%2C%20%22Rice%22%2C%201.0%2C%20%22ha%22%2C%20%22No%22%2C%203500%2C%20150.0)%2C%0A%20%20%20%20%20%20%20%20(%22H1%22%2C%202%2C%20%22Rice%22%2C%205000.0%2C%20%22sqm%22%2C%20%22Yes%22%2C%202500%2C%2060.0)%2C%20%20%23%20half%20a%20hectare%2C%20written%20in%20square%20meters%0A%20%20%20%20%20%20%20%20(%22H2%22%2C%201%2C%20%22Rice%22%2C%202.0%2C%20%22ha%22%2C%20%22Yes%22%2C%2011000%2C%20400.0)%2C%0A%20%20%20%20%20%20%20%20(%22H2%22%2C%202%2C%20%22Corn%22%2C%201.0%2C%20%22ha%22%2C%20%22No%22%2C%204000%2C%20180.0)%2C%0A%20%20%20%20%20%20%20%20(%22H3%22%2C%201%2C%20%22Rice%22%2C%201.0%2C%20%22ha%22%2C%20%22No%22%2C%207000%2C%20None)%2C%20%20%23%20no%20fertilizer%20recorded%0A%20%20%20%20%20%20%20%20(%22H3%22%2C%202%2C%20%22Corn%22%2C%200.5%2C%20%22ha%22%2C%20%22No%22%2C%201500%2C%2090.0)%2C%0A%20%20%20%20%20%20%20%20(%22H3%22%2C%203%2C%20None%2C%200.4%2C%20%22ha%22%2C%20%22No%22%2C%20800%2C%2050.0)%2C%20%20%23%20no%20crop%20recorded%0A%20%20%20%20%20%20%20%20(%22H4%22%2C%201%2C%20%22Rice%22%2C%200.8%2C%20%22ha%22%2C%20%22Yes%22%2C%203200%2C%20160.0)%2C%0A%20%20%20%20%20%20%20%20(%22H4%22%2C%202%2C%20%22Corn%22%2C%202.0%2C%20%22ha%22%2C%20%22No%22%2C%209000%2C%20300.0)%2C%0A%20%20%20%20%5D%0A%20%20%20%20plots%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22crop%22%2C%20%22area%22%2C%20%22area_unit%22%2C%20%22irrigated%22%2C%20%22production_kg%22%2C%20%22fertilizer_kg%22%5D)%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%20code_list%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22code%22%3A%20%5B%220100100000%22%2C%20%220100200000%22%2C%20%220200100000%22%2C%20%220200200000%22%5D%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%5B%22San%20Isidro%22%2C%20%22Santa%20Cruz%22%2C%20%22San%20Isidro%22%2C%20%22Burgos%22%5D%2C%20%20%23%20two%20places%20called%20San%20Isidro%2C%20in%20different%20provinces%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Aurora%22%2C%20%22Aurora%22%2C%20%22Benguet%22%2C%20%22Benguet%22%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%20plots%2C%20households%2C%20code_list%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_plots%2C%20_households%2C%20_codes%20%3D%20small_survey()%0A%20%20%20%20mo.accordion(%7B%22The%20small%20survey%2C%20table%20by%20table%22%3A%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Plots.**%20H1's%20second%20plot%20is%205%2C000%20m%C2%B2%2C%20which%20is%200.5%20ha.%20H3's%20first%20plot%20has%20no%20fertilizer%20recorded%2C%20and%20its%20third%20plot%20no%20crop.%22)%2C%20_plots%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**Households.**%20The%20weights%20differ%2C%20so%20a%20weighted%20answer%20and%20an%20unweighted%20one%20differ%20too.%22)%2C%20_households%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**Code%20list.**%20Two%20municipalities%20share%20the%20name%20San%20Isidro%3A%20only%20the%20code%20tells%20them%20apart.%22)%2C%20_codes%2C%0A%20%20%20%20%5D)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S1.1%20%C2%B7%20Fertilizer%20per%20hectare%2C%20and%20harvest%20per%20kilogram%20of%20it%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20an%20extension%20office%20deciding%20where%20to%20run%20fertilizer%20trials%20wants%20two%20numbers%20for%20each%20crop%3A%20how%20much%20fertilizer%20farmers%20put%20on%20a%20hectare%2C%20and%20how%20many%20kilograms%20of%20harvest%20each%20kilogram%20of%20fertilizer%20goes%20with.%20The%20old%20exercise%20divided%20production%20by%20fertilizer%20farm%20by%20farm.%20On%20the%20survey%2C%20that%20runs%20into%20three%20traps%20at%20once%3A%20fertilizer%20is%20blank%20where%20it%20was%20not%20recorded%2C%20areas%20come%20in%20two%20units%2C%20and%20each%20household%20stands%20for%20a%20different%20number%20of%20farm%20households.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20and%20the%20households%20%E2%86%92%20one%20row%20per%20crop%2C%20the%20crop%20as%20the%20index%2C%20with%20three%20columns%2C%20sorted%20by%20%60harvest_kg_per_kg_fertilizer%60%2C%20largest%20first%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%60plots%60%20%7C%20how%20many%20plots%20the%20figures%20use%3A%20plots%20with%20a%20crop%20**and**%20a%20fertilizer%20amount%20recorded%20%7C%0A%20%20%20%20%7C%20%60fertilizer_kg_per_ha%60%20%7C%20weighted%20total%20fertilizer%20%C3%B7%20weighted%20total%20area%20in%20hectares%2C%20to%201%20decimal%20%7C%0A%20%20%20%20%7C%20%60harvest_kg_per_kg_fertilizer%60%20%7C%20weighted%20total%20harvest%20%C3%B7%20weighted%20total%20fertilizer%2C%20to%201%20decimal%20%7C%0A%0A%20%20%20%20A%20**weighted%20total**%20multiplies%20each%20plot's%20value%20by%20its%20household's%20%60weight%60%20(how%20many%20farm%20households%20that%20household%20stands%20for)%20before%20adding%20up%20(%5Bmodule%204%2C%20section%203%5D(%2F%3Ffile%3D30_summarize_and_combine.py)).%20In%20the%20small%20survey%2C%20Rice%20uses%20four%20plots%3A%20H3's%20first%20plot%20drops%20out%2C%20because%20its%20fertilizer%20was%20not%20recorded.%0A%0A%20%20%20%20**Two%20decisions%2C%20made%20for%20you%3A**%0A%0A%20%20%20%20-%20**A%20blank%20fertilizer%20amount%20is%20unknown%2C%20not%20zero.**%20Filling%20it%20with%200%20would%20say%20those%20farmers%20used%20none%2C%20and%20their%20harvest%20would%20count%20as%20grown%20without%20fertilizer%20(%5Bmodule%205%2C%20section%201%5D(%2F%3Ffile%3D40_cleaning_and_time.py)).%20Leave%20those%20plots%20out%20of%20all%20three%20figures.%0A%20%20%20%20-%20**Totals%2C%20then%20divide.**%20Dividing%20plot%20by%20plot%20and%20averaging%20lets%20the%20smallest%20plots%2C%20where%20a%20few%20kilograms%20either%20way%20change%20the%20ratio%20a%20lot%2C%20pull%20the%20answer%20around.%20Dividing%20the%20totals%20answers%20%22per%20hectare%20of%20all%20the%20crop's%20land%22%20and%20%22per%20kilogram%20of%20all%20the%20fertilizer%22.%0A%0A%20%20%20%20Tools%3A%20%60dropna(subset%3D%5B...%5D)%60%20(%5Bmodule%205%2C%20section%201%5D(%2F%3Ffile%3D40_cleaning_and_time.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))%2C%20%60groupby(...).agg(name%3D(column%2C%20%22sum%22))%60%20(%5Bmodule%204%2C%20section%201%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%20fertilizer_by_crop(plots%3A%20pd.DataFrame%2C%20households%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Per%20crop%3A%20plots%20used%2C%20fertilizer%20per%20hectare%20and%20harvest%20per%20kilogram%20of%20fertilizer%2C%20weighted.%20Largest%20harvest%20per%20kilogram%20first.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20plots%20with%20a%20crop%20and%20a%20fertilizer%20amount%2C%20and%20bring%20in%20each%20plot's%20household%20weight%0A%20%20%20%20%23%20Step%202%3A%20weighted%20fertilizer%2C%20area%20(in%20hectares)%20and%20harvest%3A%20each%20times%20the%20weight%0A%20%20%20%20%23%20Step%203%3A%20per%20crop%2C%20count%20the%20plots%20and%20add%20up%20the%20three%20weighted%20columns%0A%20%20%20%20%23%20Step%204%3A%20divide%20the%20totals%2C%20round%20to%201%20decimal%2C%20sort%20by%20harvest%20per%20kilogram%2C%20largest%20first%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.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%22Stretch%20S1.1%22%2C%20fertilizer_by_crop%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20small%20survey%22%2C%20small_survey()%5B%3A2%5D)%2C%20(%22the%20farm%20survey%22%2C%20(course.plots()%2C%20course.households()))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22surveys%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20survey%2C%20Rice%20uses%204%20plots%20and%20Corn%203.%20Work%20out%20Rice's%20fertilizer%20per%20hectare%20by%20hand%3A%20(150%C3%97120%20%2B%2060%C3%97120%20%2B%20400%C3%97300%20%2B%20160%C3%97200)%20%C3%B7%20(1%C3%97120%20%2B%200.5%C3%97120%20%2B%202%C3%97300%20%2B%200.8%C3%97200).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20table%20on%20the%20farm%20survey.%0A%20%20%20%20course.try_it(fertilizer_by_crop%2C%20course.plots()%2C%20course.households())%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%20S1.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%20Rice%20uses%205%20plots%20in%20the%20small%20survey%2C%20not%204%20%7C%20Missing%20fertilizer%20was%20filled%20with%200%2C%20or%20not%20dropped.%20%7C%20%60dropna(subset%3D%5B%22crop%22%2C%20%22fertilizer_kg%22%5D)%60%20%7C%0A%20%20%20%20%7C%20The%20small%20survey%20is%20right%20in%20shape%2C%20but%20every%20number%20is%20off%20%7C%20The%20weights%20are%20missing%3A%20the%20figures%20are%20unweighted.%20%7C%20multiply%20by%20%60weight%60%20before%20%60sum%60%20%7C%0A%20%20%20%20%7C%20%60fertilizer_kg_per_ha%60%20far%20too%20small%20on%20the%20farm%20survey%20%7C%20%60area%60%20was%20used%2C%20so%20square%20meters%20count%20as%20hectares.%20%7C%20%60area_ha%60%20%7C%0A%20%20%20%20%7C%20The%20numbers%20are%20close%20but%20not%20equal%20%7C%20Ratios%20were%20computed%20plot%20by%20plot%20and%20then%20averaged.%20%7C%20add%20up%20first%2C%20divide%20the%20totals%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20Merge%20keys%20are%20not%20unique%20in%20right%20dataset*%20%7C%20The%20households%20have%20a%20duplicate.%20%7C%20%60course.households()%60%20drops%20them%3B%20check%20what%20you%20pass%20in%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'weight'*%20%7C%20The%20weight%20was%20not%20brought%20in%20from%20the%20households.%20%7C%20the%20%60merge%60%20in%20Step%201%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%20S1.2%20%C2%B7%20Rice%20harvest%20per%20household%20member%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20old%20exercise%20measured%20**labor%20productivity**%3A%20production%20per%20labor%20day.%20The%20farm%20survey%20has%20no%20labor%20column%3A%20it%20asks%20nobody%20how%20many%20days%20they%20worked.%20What%20it%20has%20is%20%60hh_size%60%2C%20the%20number%20of%20people%20in%20the%20household.%20Harvest%20per%20household%20member%20is%20the%20closest%20honest%20measure%2C%20and%20it%20is%20a%20different%20one%3A%20it%20counts%20children%2C%20students%20and%20elders%20as%20well%20as%20the%20people%20who%20farm%2C%20so%20it%20says%20how%20much%20rice%20each%20mouth%20in%20the%20household%20has%2C%20not%20how%20productive%20anyone's%20work%20is.%20Name%20it%20for%20what%20it%20is%2C%20in%20the%20column%20and%20in%20any%20report.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20and%20the%20households%20%E2%86%92%20one%20value%20per%20province%3A%20the%20**weighted%20mean**%2C%20over%20households%20that%20grow%20rice%2C%20of%20each%20household's%20rice%20harvest%20(kilograms%2C%20all%20its%20rice%20plots%20together)%20divided%20by%20its%20%60hh_size%60.%20A%20Series%20named%20%60rice_kg_per_member%60%2C%20provinces%20as%20the%20index%2C%20rounded%20to%201%20decimal%2C%20largest%20first.%0A%0A%20%20%20%20In%20the%20small%20survey%2C%20H1%20harvests%203%2C500%20%2B%202%2C500%20%3D%206%2C000%20kg%20of%20rice%20and%20has%204%20members%3A%201%2C500%20kg%20each.%20H2%3A%2011%2C000%20%C3%B7%203%20%3D%203%2C666.7%20kg%20each.%20Aurora's%20weighted%20mean%20is%20(1%2C500%20%C3%97%20120%20%2B%203%2C666.7%20%C3%97%20300)%20%C3%B7%20(120%20%2B%20300).%0A%0A%20%20%20%20**The%20trap%3A**%20the%20weight%20belongs%20to%20the%20household%2C%20but%20the%20harvest%20comes%20by%20plot.%20Join%20the%20households%20to%20the%20plots%20first%20and%20a%20household%20with%20three%20rice%20plots%20is%20counted%20three%20times.%20**Add%20up%20to%20one%20row%20per%20household%20first%2C%20then%20join.**%0A%0A%20%20%20%20Tools%3A%20%60groupby(%22hh_id%22)%5B%22production_kg%22%5D.sum()%60%20(%5Bmodule%204%2C%20section%201%5D(%2F%3Ffile%3D30_summarize_and_combine.py))%2C%20%60merge(...%2C%20validate%3D%22one_to_one%22)%60%20(%5Bmodule%204%2C%20section%205%5D(%2F%3Ffile%3D30_summarize_and_combine.py))%2C%20a%20weighted%20mean%20per%20group%3A%20the%20sum%20of%20value%20%C3%97%20weight%20over%20the%20sum%20of%20weights%20(%5Bmodule%204%2C%20section%203%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%20rice_per_member(plots%3A%20pd.DataFrame%2C%20households%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Per%20province%3A%20the%20weighted%20mean%20of%20rice%20harvested%20per%20household%20member%2C%20over%20households%20that%20grow%20rice.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20rice-growing%20household's%20rice%20harvest%3A%20the%20rice%20plots%2C%20added%20up%20by%20hh_id%0A%20%20%20%20%23%20Step%202%3A%20join%20the%20households%20(one%20row%20each%20on%20both%20sides)%20and%20divide%20the%20harvest%20by%20hh_size%0A%20%20%20%20%23%20Step%203%3A%20per%20province%2C%20the%20sum%20of%20(kg%20per%20member%20%C3%97%20weight)%20over%20the%20sum%20of%20weight%0A%20%20%20%20%23%20Step%204%3A%20round%20to%201%20decimal%2C%20sort%20largest%20first%2C%20name%20the%20Series%20%22rice_kg_per_member%22%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.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%22Stretch%20S1.2%22%2C%20rice_per_member%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20small%20survey%22%2C%20small_survey()%5B%3A2%5D)%2C%20(%22the%20farm%20survey%22%2C%20(course.plots()%2C%20course.households()))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22surveys%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20survey%2C%20Aurora%20is%20(1%2C500%20%C3%97%20120%20%2B%203%2C666.67%20%C3%97%20300)%20%C3%B7%20420.%20If%20you%20get%20another%20number%2C%20check%20that%20H1%20is%20counted%20once%2C%20with%20both%20its%20rice%20plots%20together.%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%20figures%20on%20the%20farm%20survey.%0A%20%20%20%20course.try_it(rice_per_member%2C%20course.plots()%2C%20course.households())%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%20S1.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*MergeError%3A%20Merge%20keys%20are%20not%20unique%20in%20left%20dataset*%20%7C%20The%20plots%20were%20joined%20before%20being%20added%20up%20by%20household.%20%7C%20Step%201%20comes%20before%20the%20join%20%7C%0A%20%20%20%20%7C%20Aurora%20is%20too%20high%20or%20too%20low%20in%20the%20small%20survey%2C%20Benguet%20is%20right%20%7C%20A%20household%20with%20two%20rice%20plots%20is%20weighted%20twice.%20%7C%20one%20row%20per%20household%20before%20weighting%20%7C%0A%20%20%20%20%7C%20Every%20province%20is%20a%20little%20high%20%7C%20Corn%2C%20vegetable%20or%20banana%20plots%20are%20in%20the%20harvest.%20%7C%20%60plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%60%20%7C%0A%20%20%20%20%7C%20Numbers%20are%20right%20but%20in%20another%20order%20%7C%20Not%20sorted%2C%20or%20sorted%20smallest%20first.%20%7C%20%60sort_values(ascending%3DFalse)%60%20%7C%0A%20%20%20%20%7C%20The%20check%20says%20the%20name%20differs%20%7C%20The%20Series%20is%20not%20named.%20%7C%20%60.rename(%22rice_kg_per_member%22)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S1.3%20%C2%B7%20Each%20plot%20against%20its%20crop's%20typical%20yield%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20farm%20typology%20exercise%20began%20by%20asking%20which%20farms%20do%20better%20than%20the%20typical%20farm.%20Comparing%20a%20rice%20plot%20with%20a%20banana%20plantation%20tells%20you%20nothing%3A%20bananas%20yield%20about%20five%20times%20as%20much%20per%20hectare.%20Compare%20each%20plot%20with%20plots%20of%20**the%20same%20crop**.%20And%20use%20the%20**median**%20as%20%22typical%22%3A%20the%20plot%20in%20the%20middle%2C%20which%20one%20mistyped%20harvest%20cannot%20drag%20the%20way%20it%20drags%20a%20mean.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20%E2%86%92%20one%20value%20per%20plot%2C%20in%20the%20plots'%20own%20order%20and%20index%3A%20the%20plot's%20yield%20(%60production_kg%20%C3%B7%20area_ha%60)%20divided%20by%20the%20median%20yield%20of%20the%20plots%20of%20the%20same%20crop%2C%20rounded%20to%203%20decimals.%20Missing%20where%20the%20crop%20was%20not%20recorded.%20A%20Series%20named%20%60relative_yield%60.%0A%0A%20%20%20%20%7C%20plot%20(small%20survey)%20%7C%20crop%20%7C%20yield%2C%20kg%2Fha%20%7C%20median%20of%20its%20crop%20%7C%20relative%20yield%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20H1%2C%201%20%7C%20Rice%20%7C%203%2C500%20%7C%205%2C000%20%7C%200.700%20%7C%0A%20%20%20%20%7C%20H3%2C%201%20%7C%20Rice%20%7C%207%2C000%20%7C%205%2C000%20%7C%201.400%20%7C%0A%20%20%20%20%7C%20H3%2C%203%20%7C%20(none)%20%7C%202%2C000%20%7C%20%7C%20missing%20%7C%0A%0A%20%20%20%20A%20relative%20yield%20of%201.4%20means%2040%25%20above%20the%20typical%20plot%20of%20that%20crop%3B%200.7%20means%2030%25%20below.%0A%0A%20%20%20%20**Unweighted%2C%20on%20purpose.**%20This%20compares%20plots%20in%20the%20sample%2C%20to%20find%20the%20ones%20worth%20a%20second%20look%2C%20not%20to%20say%20anything%20about%20all%20the%20province's%20farms.%20Every%20plot%20counts%20once.%0A%0A%20%20%20%20Tools%3A%20%60groupby(...).transform(%22median%22)%60%2C%20which%20gives%20every%20row%20its%20own%20group's%20median%2C%20so%20the%20result%20lines%20up%20with%20the%20plots%20(%5Bmodule%204%2C%20section%202%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%20relative_yield(plots%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20plot's%20yield%20divided%20by%20the%20median%20yield%20of%20its%20crop's%20plots%3B%20missing%20where%20the%20crop%20is.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20plot's%20yield%20in%20kg%20per%20hectare%20(use%20area_ha)%0A%20%20%20%20%23%20Step%202%3A%20each%20plot's%20crop%20median%2C%20lined%20up%20with%20the%20plots%3A%20group%20the%20yields%20by%20crop%20and%20transform%0A%20%20%20%20%23%20Step%203%3A%20divide%2C%20round%20to%203%20decimals%2C%20name%20the%20Series%20%22relative_yield%22%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S1.3%22%2C%20relative_yield%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20small%20survey%22%2C%20small_survey()%5B%3A1%5D)%2C%20(%22the%20farm%20survey%22%2C%20(course.plots()%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22surveys%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20survey%20the%20rice%20median%20is%205%2C000%20kg%2Fha%20and%20the%20corn%20median%204%2C000%20kg%2Fha%3B%20H3's%20third%20plot%20has%20no%20crop%20and%20stays%20missing.%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%20relative%20yields%20of%20the%20farm%20survey's%20plots%2C%20beside%20the%20plots.%0A%20%20%20%20def%20_try_relative()%3A%0A%20%20%20%20%20%20%20%20_plots%20%3D%20course.plots()%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20_plots%5B%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22crop%22%2C%20%22area_ha%22%2C%20%22production_kg%22%5D%5D.assign(relative_yield%3Drelative_yield(_plots)).head(10)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20%60relative_yield%60%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20its%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%0A%20%20%20%20_try_relative()%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%20S1.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%20Rice%20plots%20compared%20with%20corn%20%7C%20One%20median%20for%20all%20plots.%20%7C%20group%20by%20%60crop%60%20%7C%0A%20%20%20%20%7C%20Values%20are%20close%20but%20off%20%7C%20The%20mean%20was%20used%2C%20not%20the%20median.%20%7C%20%60transform(%22median%22)%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20cannot%20reindex%20on%20an%20axis%20with%20duplicate%20labels*%2C%20or%20missing%20values%20everywhere%20%7C%20%60.agg%60%20or%20%60.median()%60%20gives%20one%20row%20per%20crop%2C%20which%20does%20not%20line%20up%20with%20the%20plots.%20%7C%20%60transform%60%20%7C%0A%20%20%20%20%7C%20One%20small-survey%20plot%20is%2010%2C000%20times%20off%20%7C%20%60area%60%20was%20used%20instead%20of%20%60area_ha%60.%20%7C%20Step%201%20%7C%0A%20%20%20%20%7C%20H3's%20third%20plot%20is%20a%20number%20%7C%20The%20crop-less%20plot%20was%20compared%20with%20something.%20%7C%20let%20%60transform%60%20leave%20it%20missing%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%20S1.4%20%C2%B7%20A%20farm%20typology%3A%20small%20or%20large%2C%20low%20or%20high%20yield%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20typology%20exercise%20sorted%20farms%20into%20kinds%20by%20two%20things%20at%20once%2C%20size%20and%20yield%2C%20so%20that%20advice%20can%20be%20aimed%3A%20a%20large%20plot%20with%20a%20low%20yield%20needs%20something%20different%20from%20a%20small%20plot%20with%20a%20high%20one.%20Here%20the%20kinds%20are%20set%20within%20each%20crop%2C%20and%20the%20result%20says%20what%20share%20of%20each%20crop's%20plots%2C%20across%20the%20seven%20provinces%2C%20is%20of%20each%20kind.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20and%20the%20households%20%E2%86%92%20a%20table%20with%20one%20row%20per%20crop%20(the%20crop%20as%20the%20index)%20and%20the%20four%20kinds%20as%20columns%2C%20in%20this%20order%3A%20%60small%2C%20low%20yield%60%2C%20%60small%2C%20high%20yield%60%2C%20%60large%2C%20low%20yield%60%2C%20%60large%2C%20high%20yield%60.%20Each%20cell%20is%20the%20**weighted**%20share%20of%20the%20crop's%20plots%20of%20that%20kind%2C%20in%20percent%2C%20to%201%20decimal%3A%20each%20row%20adds%20up%20to%20100.%20A%20kind%20no%20plot%20falls%20in%20shows%200.%0A%0A%20%20%20%20The%20rules%2C%20for%20plots%20with%20a%20crop%20recorded%3A%0A%0A%20%20%20%20%7C%20%7C%20rule%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**large**%20%7C%20%60area_ha%60%20**at%20or%20above**%20the%20median%20%60area_ha%60%20of%20plots%20of%20the%20same%20crop%3B%20otherwise%20**small**%20%7C%0A%20%20%20%20%7C%20**high%20yield**%20%7C%20yield%20(kg%2Fha)%20**at%20or%20above**%20the%20median%20yield%20of%20plots%20of%20the%20same%20crop%3B%20otherwise%20**low%20yield**%20%7C%0A%20%20%20%20%7C%20**share**%20%7C%20the%20sum%20of%20the%20plots'%20household%20weights%20in%20that%20kind%20%C3%B7%20the%20sum%20for%20the%20crop%2C%20%C3%97%20100%20%7C%0A%0A%20%20%20%20In%20the%20small%20survey%2C%20rice%20plots%20measure%201.0%2C%200.5%2C%202.0%2C%201.0%20and%200.8%20ha%3A%20the%20median%20is%201.0%2C%20and%20both%201.0%20ha%20plots%20are%20**large**.%20Ties%20at%20the%20median%20are%20why%20%22at%20or%20above%22%20matters.%0A%0A%20%20%20%20**Why%20the%20medians%20are%20unweighted%20and%20the%20shares%20weighted%3A**%20the%20medians%20are%20only%20where%20the%20lines%20are%20drawn%2C%20from%20the%20plots%20in%20the%20sample.%20The%20shares%20are%20a%20statement%20about%20all%20the%20crop's%20farm%20plots%20in%20the%20provinces%2C%20and%20each%20plot%20stands%20for%20as%20many%20plots%20as%20its%20household's%20weight.%0A%0A%20%20%20%20Tools%3A%20%60transform(%22median%22)%60%20(Exercise%20S1.3)%2C%20%60np.where(condition%2C%20%22large%22%2C%20%22small%22)%60%20(%5Bmodule%202%5D(%2F%3Ffile%3D10_arrays.py))%2C%20%60pd.crosstab(rows%2C%20columns%2C%20values%3D...%2C%20aggfunc%3D%22sum%22%2C%20normalize%3D%22index%22)%60%20(%5Bmodule%204%2C%20section%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py))%2C%20and%20%60.reindex(columns%3DPLOT_TYPES%2C%20fill_value%3D0)%60%20to%20put%20the%20four%20columns%20in%20order%2C%20even%20a%20kind%20that%20is%20empty.%20%60PLOT_TYPES%60%20is%20set%20at%20the%20top%20of%20this%20notebook.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20plot_types(plots%3A%20pd.DataFrame%2C%20households%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Per%20crop%3A%20the%20weighted%20percentage%20of%20plots%20that%20are%20small%20or%20large%2C%20with%20a%20low%20or%20high%20yield%2C%20against%20the%20crop's%20medians.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20plots%20with%20a%20crop%2C%20each%20with%20its%20household's%20weight%2C%20and%20each%20plot's%20yield%20in%20kg%20per%20hectare%0A%20%20%20%20%23%20Step%202%3A%20%22large%22%20or%20%22small%22%2C%20and%20%22high%20yield%22%20or%20%22low%20yield%22%2C%20against%20the%20crop's%20medians%20(at%20or%20above%20the%20median%20counts%20as%20large%2C%20or%20high)%0A%20%20%20%20%23%20Step%203%3A%20one%20label%20per%20plot%2C%20such%20as%20%22large%2C%20low%20yield%22%0A%20%20%20%20%23%20Step%204%3A%20a%20crosstab%20of%20crop%20by%20label%2C%20adding%20up%20the%20weights%2C%20as%20shares%20of%20each%20row%3B%20then%20in%20percent%2C%20PLOT_TYPES%20order%2C%201%20decimal%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.4%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%22Stretch%20S1.4%22%2C%20plot_types%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20small%20survey%22%2C%20small_survey()%5B%3A2%5D)%2C%20(%22the%20farm%20survey%22%2C%20(course.plots()%2C%20course.households()))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22surveys%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Ties%20at%20the%20median%20count%20as%20large%2C%20or%20high%20yield%3A%20in%20the%20small%20survey%2C%20both%201.0%20ha%20rice%20plots%20are%20large%2C%20and%20the%205%2C000%20kg%2Fha%20rice%20plot%20is%20high-yield.%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%20typology%20of%20the%20farm%20survey's%20plots.%0A%20%20%20%20course.try_it(plot_types%2C%20course.plots()%2C%20course.households())%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%20S1.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%20Rice%20is%20wrong%20in%20the%20small%20survey%2C%20corn%20right%20%7C%20%60%3E%60%20instead%20of%20%60%3E%3D%60%3A%20plots%20at%20the%20median%20turn%20small%20or%20low.%20%7C%20Step%202%20%7C%0A%20%20%20%20%7C%20Rows%20add%20up%20to%20100%20but%20the%20shares%20are%20off%20%7C%20The%20shares%20count%20plots%2C%20not%20weights.%20%7C%20%60values%3D...%2C%20aggfunc%3D%22sum%22%60%20%7C%0A%20%20%20%20%7C%20Rows%20do%20not%20add%20up%20to%20100%20%7C%20Not%20normalized%20by%20row.%20%7C%20%60normalize%3D%22index%22%60%20%7C%0A%20%20%20%20%7C%20A%20column%20is%20missing%2C%20or%20the%20columns%20are%20in%20another%20order%20%7C%20The%20kinds%20are%20in%20the%20order%20the%20crosstab%20found%20them.%20%7C%20%60.reindex(columns%3DPLOT_TYPES%2C%20fill_value%3D0)%60%20%7C%0A%20%20%20%20%7C%20A%20row%20for%20missing%20crops%2C%20or%20*TypeError*%20comparing%20with%20NaN%20%7C%20The%20plots%20without%20a%20crop%20were%20not%20dropped.%20%7C%20Step%201%20%7C%0A%20%20%20%20%7C%20Values%20are%200%20to%201%2C%20not%200%20to%20100%20%7C%20Not%20multiplied%20by%20100.%20%7C%20%60*%20100%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%20S1.5%20%C2%B7%20The%20follow-up%20list%3A%20who%20to%20visit%2C%20and%20where%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20typology%20exercise%20ended%20by%20ranking%20farms%20for%20follow-up%3A%20the%20ones%20furthest%20from%20typical%20are%20either%20remarkable%20or%20wrong%2C%20and%20either%20way%20someone%20should%20go%20and%20look.%20A%20field%20supervisor%20needs%20the%20list%20with%20**where**%20each%20household%20is%3A%20its%20province%20and%20its%20municipality's%20name%2C%20which%20the%20survey%20does%20not%20carry.%20The%20survey%20has%20the%20PSGC%20code%3B%20the%20PSA%20code%20list%20has%20the%20name.%0A%0A%20%20%20%20**How%20far%20from%20typical%3A**%20a%20plot%20at%20half%20its%20crop's%20median%20yield%20is%20as%20far%20off%20as%20a%20plot%20at%20double%20it.%20The%20old%20exercise%20subtracted%2C%20which%20treats%20them%20differently%20(0.5%20below%2C%201.0%20above).%20Here%20distance%20is%20**times%20off**%3A%20the%20relative%20yield%20or%20its%20inverse%2C%20whichever%20is%20larger.%20Half%20and%20double%20are%20both%202%20times%20off%3B%201.0%20is%201%20time%20off%2C%20exactly%20typical.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%2C%20the%20households%2C%20the%20code%20list%20and%20a%20number%20%60n%60%20%E2%86%92%20the%20%60n%60%20plots%20furthest%20from%20typical%2C%20one%20row%20each%2C%20with%20the%20columns%20%60hh_id%60%2C%20%60plot_no%60%2C%20%60crop%60%2C%20%60province%60%2C%20%60municipality%60%2C%20%60yield_t_ha%60%20(tonnes%20per%20hectare%2C%202%20decimals)%2C%20%60relative_yield%60%20(as%20in%20Exercise%20S1.3%2C%203%20decimals)%20and%20%60times_off%60%20(2%20decimals).%20Sorted%20by%20%60times_off%60%2C%20largest%20first%3B%20ties%20by%20%60hh_id%60%2C%20then%20%60plot_no%60.%20Plots%20with%20no%20crop%20are%20left%20out.%20The%20index%20runs%200%2C%201%2C%202%2C%20%E2%80%A6%0A%0A%20%20%20%20In%20the%20small%20survey%2C%20H3's%20first%20rice%20plot%20(relative%20yield%201.4%3A%201.40%20times%20off)%20comes%20after%20H1's%20first%20(0.7%3A%201.43%20times%20off).%20Subtracting%20would%20put%20them%20the%20other%20way%20round.%0A%0A%20%20%20%20**The%20join%20that%20matters%3A**%20two%20municipalities%20in%20the%20small%20survey%2C%20in%20different%20provinces%2C%20are%20called%20San%20Isidro%2C%20as%20five%20names%20repeat%20in%20the%20real%20code%20list.%20Join%20on%20the%20code%2C%20and%20validate%20that%20each%20code%20is%20in%20the%20code%20list%20once.%0A%0A%20%20%20%20Tools%3A%20Exercise%20S1.3's%20steps%2C%20%60np.maximum(a%2C%20b)%60%20(%5Bmodule%202%5D(%2F%3Ffile%3D10_arrays.py))%2C%20two%20%60merge%60s%20with%20%60validate%3D%60%20(%5Bmodule%204%2C%20section%205%5D(%2F%3Ffile%3D30_summarize_and_combine.py))%2C%20%60sort_values(%5B...%5D%2C%20ascending%3D%5BFalse%2C%20True%2C%20True%5D)%60%2C%20%60.head(n)%60%2C%20%60.reset_index(drop%3DTrue)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20follow_up_list(plots%3A%20pd.DataFrame%2C%20households%3A%20pd.DataFrame%2C%20code_list%3A%20pd.DataFrame%2C%20n%3A%20int)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20n%20plots%20furthest%20from%20their%20crop's%20median%20yield%2C%20with%20where%20each%20household%20is.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20plots%20with%20a%20crop%3B%20each%20plot's%20yield%20in%20t%2Fha%2C%20relative%20yield%20and%20times%20off%0A%20%20%20%20%23%20Step%202%3A%20where%20each%20household%20is%3A%20its%20province%2C%20and%20its%20municipality's%20name%20from%20the%20code%20list%20(join%20on%20the%20code)%0A%20%20%20%20%23%20Step%203%3A%20join%20that%20to%20the%20plots%3B%20sort%20by%20times_off%20(largest%20first)%2C%20then%20hh_id%2C%20then%20plot_no%3B%20keep%20the%20first%20n%0A%20%20%20%20%23%20Step%204%3A%20round%2C%20keep%20the%20eight%20columns%20in%20order%2C%20and%20number%20the%20rows%20from%200%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.5%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%22Stretch%20S1.5%22%2C%20follow_up_list%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20small%20survey%2C%20every%20plot%20with%20a%20crop%22%2C%20(*small_survey()%2C%208))%2C%20(%22the%20farm%20survey%2C%20the%20first%2015%22%2C%20(course.plots()%2C%20course.households()%2C%20course.code_list()%2C%2015))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22lists%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20survey%2C%20H1's%20first%20plot%20(0.7%20of%20typical)%20comes%20before%20H3's%20first%20(1.4%20of%20typical)%3A%201%20%C3%B7%200.7%20%3D%201.43%20times%20off%2C%20against%201.40.%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%20how_many%20%3D%20mo.ui.slider(5%2C%2040%2C%20value%3D10%2C%20step%3D5%2C%20label%3D%22How%20many%20plots%20on%20the%20list%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20how_many%0A%20%20%20%20return%20(how_many%2C)%0A%0A%0A%40app.cell%0Adef%20_(how_many)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20follow-up%20list%20for%20the%20farm%20survey.%20Move%20the%20slider%20to%20change%20its%20length.%0A%20%20%20%20course.try_it(follow_up_list%2C%20course.plots()%2C%20course.households()%2C%20course.code_list()%2C%20how_many.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%20S1.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%20H3's%20first%20plot%20is%20above%20H1's%20first%20in%20the%20small%20survey%20%7C%20Distance%20by%20subtraction%2C%20%60abs(rel%20-%201)%60.%20%7C%20%60np.maximum(rel%2C%201%20%2F%20rel)%60%20%7C%0A%20%20%20%20%7C%20More%20rows%20than%20plots%2C%20or%20*MergeError*%20%7C%20Joined%20on%20the%20name%2C%20or%20the%20code%20list%20has%20a%20code%20twice.%20%7C%20join%20on%20%60psgc%60%20%3D%20%60code%60%2C%20%60validate%3D%22many_to_one%22%60%20%7C%0A%20%20%20%20%7C%20A%20%60province_x%60%20and%20%60province_y%60%20%7C%20The%20code%20list's%20%60province%60%20came%20along%20in%20the%20join.%20%7C%20take%20only%20%60code%60%20and%20%60name%60%20from%20it%20%7C%0A%20%20%20%20%7C%20%60municipality%60%20is%20empty%20%7C%20The%20codes%20did%20not%20match%3A%20one%20side%20is%20a%20number.%20%7C%20%60course.households()%60%20keeps%20%60psgc%60%20as%20text%20%7C%0A%20%20%20%20%7C%20The%20right%20plots%2C%20in%20another%20order%20%7C%20Ties%20are%20not%20broken%2C%20or%20the%20index%20was%20not%20reset.%20%7C%20sort%20by%20three%20columns%3B%20%60reset_index(drop%3DTrue)%60%20%7C%0A%20%20%20%20%7C%20A%20plot%20with%20no%20crop%20on%20the%20list%20%7C%20Its%20relative%20yield%20is%20missing%20and%20was%20not%20dropped%20first.%20%7C%20Step%201%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%20S1.6%20%C2%B7%20Irrigation%20and%20rice%20yield%2C%20province%20by%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20fertilizer%20exercise%20asked%20how%20a%20crop%20responds%20to%20what%20goes%20into%20it.%20Water%20is%20the%20other%20big%20input.%20A%20province%20where%20irrigated%20rice%20yields%20far%20more%20than%20rainfed%20rice%20is%20one%20where%20irrigation%20pays%3B%20where%20the%20gap%20is%20small%2C%20money%20may%20do%20more%20elsewhere.%20The%20answer%20must%20stand%20for%20all%20the%20province's%20rice%20plots%2C%20so%20it%20is%20weighted.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20and%20the%20households%20%E2%86%92%20one%20row%20per%20province%20(the%20province%20as%20the%20index)%2C%20with%20three%20columns%2C%20sorted%20by%20%60gain%60%2C%20largest%20first%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%60No%60%20%7C%20the%20weighted%20mean%20yield%20of%20rainfed%20rice%20plots%20(%60irrigated%60%20is%20%22No%22)%2C%20in%20tonnes%20per%20hectare%2C%202%20decimals%20%7C%0A%20%20%20%20%7C%20%60Yes%60%20%7C%20the%20same%20for%20irrigated%20rice%20plots%20%7C%0A%20%20%20%20%7C%20%60gain%60%20%7C%20%60Yes%60%20minus%20%60No%60%20%7C%0A%0A%20%20%20%20The%20**weighted%20mean%20yield**%20is%20the%20one%20module%204%20uses%3A%20each%20plot's%20yield%2C%20weighted%20by%20its%20household's%20weight%2C%20summed%2C%20divided%20by%20the%20sum%20of%20the%20weights.%20On%20the%20farm%20survey%2C%20all%20rice%20plots%20together%20give%204.50%20t%2Fha%20this%20way%20(4.35%20unweighted).%0A%0A%20%20%20%20In%20the%20small%20survey%2C%20Benguet's%20rainfed%20rice%20is%20one%20plot%2C%20H3's%3A%207.0%20t%2Fha.%20Its%20irrigated%20rice%20is%20H4's%20plot%3A%204.0%20t%2Fha.%20A%20gain%20of%20%E2%88%923.0%3A%20in%20a%20sample%20this%20small%2C%20one%20plot%20decides%20everything%2C%20which%20is%20why%20the%20farm%20survey's%20figures%20need%20many%20plots%20behind%20each%20cell.%0A%0A%20%20%20%20Tools%3A%20%60pivot_table(index%3D...%2C%20columns%3D...%2C%20values%3D%5B...%5D%2C%20aggfunc%3D%22sum%22)%60%20(%5Bmodule%204%2C%20section%204%5D(%2F%3Ffile%3D30_summarize_and_combine.py))%2C%20then%20a%20weighted%20mean%20from%20the%20two%20sums%2C%20as%20in%20Exercise%20S1.2.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20irrigation_gain(plots%3A%20pd.DataFrame%2C%20households%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Per%20province%3A%20the%20weighted%20mean%20yield%20of%20rainfed%20and%20of%20irrigated%20rice%2C%20in%20t%2Fha%2C%20and%20the%20gain%20from%20irrigation.%22%22%22%0A%20%20%20%20%23%20Exercise%20S1.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20rice%20plots%2C%20each%20with%20its%20household's%20province%20and%20weight%2C%20and%20its%20yield%20in%20t%2Fha%0A%20%20%20%20%23%20Step%202%3A%20yield%20%C3%97%20weight%2C%20and%20the%20sums%20of%20it%20and%20of%20the%20weight%20by%20province%20(rows)%20and%20irrigated%20(columns)%0A%20%20%20%20%23%20Step%203%3A%20weighted%20means%3A%20one%20sum%20divided%20by%20the%20other%3B%20then%20gain%20%3D%20Yes%20%E2%88%92%20No%0A%20%20%20%20%23%20Step%204%3A%20the%20columns%20No%2C%20Yes%2C%20gain%3B%202%20decimals%3B%20sorted%20by%20gain%2C%20largest%20first%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S1.6%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%22Stretch%20S1.6%22%2C%20irrigation_gain%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20small%20survey%22%2C%20small_survey()%5B%3A2%5D)%2C%20(%22the%20farm%20survey%22%2C%20(course.plots()%2C%20course.households()))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22surveys%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20survey%2C%20Aurora's%20rainfed%20rice%20is%20H1's%20first%20plot%20(3.5%20t%2Fha)%20and%20its%20irrigated%20rice%20two%20plots%3A%20(5.0%20%C3%97%20120%20%2B%205.5%20%C3%97%20300)%20%C3%B7%20420%20%3D%205.36%20t%2Fha.%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%20gain%20from%20irrigation%20on%20the%20farm%20survey.%0A%20%20%20%20course.try_it(irrigation_gain%2C%20course.plots()%2C%20course.households())%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%20S1.6%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Close%20to%20the%20right%20numbers%2C%20but%20off%20%7C%20Unweighted%20means.%20%7C%20weighted%20sums%20over%20the%20sum%20of%20weights%20%7C%0A%20%20%20%20%7C%20Off%2C%20and%20the%20farm%20survey's%20yields%20too%20high%20or%20low%20overall%20%7C%20Total%20harvest%20%C3%B7%20total%20area%20instead%20of%20the%20mean%20of%20plot%20yields.%20%7C%20the%20definition%20above%20the%20cell%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%20*KeyError%3A%20'Yes'*%20%7C%20The%20pivot's%20columns%20are%20not%20%60No%60%20and%20%60Yes%60%3A%20%60columns%3D%22irrigated%22%60%20is%20missing%2C%20or%20the%20values%20were%20pivoted%20wrongly.%20%7C%20%60pivot_table(...%2C%20columns%3D%22irrigated%22)%60%20%7C%0A%20%20%20%20%7C%20The%20check%20says%20the%20columns%20differ%20%7C%20Extra%20columns%2C%20or%20the%20label%20%22irrigated%22%20over%20them.%20%7C%20%60means%5B%5B%22No%22%2C%20%22Yes%22%2C%20%22gain%22%5D%5D%60%2C%20%60columns.name%20%3D%20None%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%20What%20these%20six%20add%20up%20to%0A%0A%20%20%20%20%7C%20exercise%20%7C%20the%20old%20question%20%7C%20what%20the%20survey%20made%20of%20it%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20S1.1%20%7C%20fertilizer%20efficiency%2C%20farm%20by%20farm%20%7C%20per%20crop%2C%20weighted%2C%20with%20unrecorded%20fertilizer%20left%20out%20rather%20than%20counted%20as%20zero%20%7C%0A%20%20%20%20%7C%20S1.2%20%7C%20production%20per%20labor%20day%20%7C%20harvest%20per%20household%20member%3A%20the%20survey%20has%20no%20labor%20days%2C%20so%20the%20measure%20is%20renamed%2C%20not%20pretended%20%7C%0A%20%20%20%20%7C%20S1.3%20%7C%20farms%20above%20the%20typical%20farm%20%7C%20each%20plot%20against%20its%20own%20crop's%20median%20%7C%0A%20%20%20%20%7C%20S1.4%20%7C%20farm%20types%20by%20size%20and%20yield%20%7C%20four%20kinds%20within%20each%20crop%2C%20as%20weighted%20shares%20%7C%0A%20%20%20%20%7C%20S1.5%20%7C%20ranking%20farms%20for%20follow-up%20%7C%20%22times%20off%22%20typical%2C%20with%20each%20household's%20municipality%20from%20the%20code%20list%20%7C%0A%20%20%20%20%7C%20S1.6%20%7C%20how%20crops%20respond%20to%20inputs%20%7C%20the%20yield%20gain%20from%20irrigation%2C%20province%20by%20province%20%7C%0A%0A%20%20%20%20The%20other%20stretch%20notebooks%20go%20on%20from%20here%3A%20%6082_stretch_polars.py%60%20after%20module%206%2C%20%6083_stretch_charts.py%60%20after%20module%207.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
7abee1e1facc62b7a6f3baf4b665d0b6