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%22Summarize%20and%20combine%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20tables%20every%20other%20cell%20uses.%0A%20%20%20%20import%20altair%20as%20alt%0A%20%20%20%20import%20matplotlib.dates%20as%20mdates%20%20%23%20month%20ticks%20on%20a%20date%20axis%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20seaborn%20as%20sns%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey's%20tables%2C%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20%23%20The%20survey%2C%20cleaned%20as%20module%205%20teaches%3A%20duplicates%20dropped%2C%20not-stated%20codes%20made%20missing%2C%20every%20area%20in%20hectares%0A%20%20%20%20households%20%3D%20course.households()%0A%20%20%20%20plots%20%3D%20course.plots()%0A%20%20%20%20code_list%20%3D%20course.code_list()%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%20Summarize%20and%20combine%0A%0A%20%20%20%20Module%203%20read%20the%20farm%20survey%20into%20tables%20and%20picked%20out%20rows%20and%20columns.%20A%20report%20asks%20a%20different%20kind%20of%20question%3A%20**how%20much**%20land%20is%20under%20rice%20in%20each%20province%2C%20**what%20share**%20of%20a%20household's%20land%20each%20plot%20is%2C%20**how%20many**%20farm%20households%20the%20survey%20stands%20for.%20Every%20one%20of%20them%20is%20about%20groups%2C%20and%20most%20need%20two%20tables%20at%20once.%20This%20module%20answers%20them.%0A%0A%20%20%20%20%7C%20section%20%7C%20what%20you%20do%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20Group%20and%20aggregate%20%7C%20one%20number%20per%20group%3A%20totals%2C%20means%20and%20counts%2C%20and%20the%20rows%20%60groupby%60%20leaves%20out%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Within%20groups%20%7C%20a%20group's%20total%20beside%20each%20of%20its%20rows%3A%20shares%2C%20ranks%20and%20running%20totals%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Survey%20weights%20%7C%20estimates%20for%20the%20provinces%2C%20not%20for%20the%20sample%3A%20weighted%20means%2C%20totals%20and%20shares%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20Tidy%20data%20%7C%20the%20same%20facts%20long%20or%20wide%3A%20%60pivot_table%60%2C%20%60melt%60%2C%20%60crosstab%60%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20Joins%20and%20concatenation%20%7C%20tables%20combined%20without%20losing%20or%20multiplying%20rows%20%7C%0A%0A%20%20%20%20**The%20tables.**%20Every%20cell%20here%20starts%20from%20the%20survey%20as%20module%205%20leaves%20it%3A%20%60households%60%20(one%20row%20per%20household)%2C%20%60plots%60%20(one%20row%20per%20plot%2C%20with%20%60area_ha%60%2C%20every%20area%20in%20hectares)%20and%20%60code_list%60%20(the%20PSGC%20table%20of%20cities%20and%20municipalities).%20The%20setup%20cell%20at%20the%20top%20reads%20them%20with%20%60course.households()%60%2C%20%60course.plots()%60%20and%20%60course.code_list()%60.%20Module%205%20teaches%20the%20cleaning%20they%20have%20had%3B%20here%20they%20arrive%20clean%20so%20that%20every%20section%20starts%20from%20the%20same%20place.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20module%203%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20If%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%224.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Group%20and%20aggregate%0A%0A%20%20%20%20**The%20problem.**%20How%20much%20land%20is%20under%20each%20crop%3F%20You%20could%20filter%20the%20plots%20four%20times%2C%20once%20per%20crop%2C%20and%20add%20up%20each%20one.%20That%20works%20for%20four%20crops%3B%20it%20does%20not%20work%20for%20seven%20provinces%20times%20four%20crops%2C%20and%20it%20breaks%20the%20day%20a%20fifth%20crop%20appears.%0A%0A%20%20%20%20**The%20tool.**%20%60groupby%60%20does%20it%20in%20one%20step%2C%20in%20three%20moves%20often%20called%20*split%2C%20apply%2C%20combine*%3A%0A%0A%20%20%20%20%7C%20move%20%7C%20what%20happens%20%7C%20here%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20split%20%7C%20the%20rows%20are%20sorted%20into%20groups%2C%20one%20per%20value%20of%20the%20key%20%7C%20one%20group%20per%20crop%20%7C%0A%20%20%20%20%7C%20apply%20%7C%20a%20summary%20is%20computed%20on%20each%20group%20%7C%20the%20sum%20of%20%60area_ha%60%20%7C%0A%20%20%20%20%7C%20combine%20%7C%20the%20results%20come%20back%20as%20one%20table%2C%20one%20row%20per%20group%20%7C%20four%20rows%2C%20one%20per%20crop%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20total%20area%20of%20each%20crop%3A%20group%20the%20plots%20by%20crop%2C%20take%20the%20area%20column%2C%20add%20it%20up%20in%20each%20group%0A%20%20%20%20plots.groupby(%22crop%22)%5B%22area_ha%22%5D.sum()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20result%20is%20a%20Series%20whose%20**index**%20is%20the%20crop%3A%20the%20groups'%20names%20became%20the%20row%20labels%20(module%203's%20index).%20Read%20it%20like%20a%20dictionary%3A%20%60area_by_crop%5B%22Rice%22%5D%60%20would%20be%20rice's%20total.%0A%0A%20%20%20%20%23%23%23%20Missing%20keys%3A%20the%20rows%20%60groupby%60%20leaves%20out%0A%0A%20%20%20%20Add%20up%20those%20four%20numbers%20and%20compare%20them%20with%20the%20total%20area%20of%20all%20plots.%20They%20don't%20match.%20Fourteen%20plots%20have%20no%20crop%20recorded%20(their%20%60crop%60%20is%20missing)%2C%20and%20**%60groupby%60%20leaves%20out%20every%20row%20whose%20key%20is%20missing**%2C%20without%20a%20warning.%20Their%20land%20simply%20vanishes%20from%20the%20table.%0A%0A%20%20%20%20%60dropna%3DFalse%60%20keeps%20them%2C%20as%20a%20group%20of%20their%20own.%20Flip%20the%20switch%20and%20watch%20the%20totals.%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%20keep_missing%20%3D%20mo.ui.switch(label%3D%22%60dropna%3DFalse%60%3A%20keep%20the%20plots%20with%20no%20crop%22%2C%20value%3DFalse)%0A%20%20%20%20keep_missing%0A%20%20%20%20return%20(keep_missing%2C)%0A%0A%0A%40app.cell%0Adef%20_(keep_missing)%3A%0A%20%20%20%20%23%20The%20same%20sum%2C%20with%20or%20without%20the%20plots%20whose%20crop%20is%20missing%0A%20%20%20%20_by_crop%20%3D%20plots.groupby(%22crop%22%2C%20dropna%3Dnot%20keep_missing.value)%5B%22area_ha%22%5D.sum()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_by_crop%2C%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22These%20rows%20add%20up%20to%20**%7B_by_crop.sum()%3A%2C.2f%7D%20ha**.%20All%20%7Blen(plots)%3A%2C%7D%20plots%20together%20cover%20**%7Bplots%5B'area_ha'%5D.sum()%3A%2C.2f%7D%20ha**.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(%22They%20match%3A%20every%20plot%20is%20in%20the%20table%2C%20the%20ones%20with%20no%20crop%20in%20a%20row%20of%20their%20own%20(shown%20as%20%60NaN%60%2C%20missing).%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20keep_missing.value%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20difference%20is%20the%20%7Bplots%5B'crop'%5D.isna().sum()%7D%20plots%20with%20no%20crop%2C%20left%20out%20because%20their%20key%20is%20missing.%22)%0A%20%20%20%20%20%20%20%20)%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%20Which%20is%20right%20depends%20on%20the%20question.%20A%20table%20of%20**land%20by%20crop**%20that%20must%20add%20up%20to%20the%20survey's%20total%20needs%20%60dropna%3DFalse%60%2C%20so%20the%20reader%20sees%20the%20land%20whose%20crop%20nobody%20recorded.%20A%20table%20of%20**yields%20by%20crop**%20has%20no%20use%20for%20a%20row%20with%20no%20crop%2C%20and%20the%20default%20is%20fine%2C%20as%20long%20as%20the%20report%20says%2014%20plots%20were%20left%20out.%20Either%20way%2C%20decide%3B%20don't%20let%20the%20default%20decide%20for%20you.%0A%0A%20%20%20%20%23%23%23%20Several%20results%20at%20once%3A%20named%20aggregations%0A%0A%20%20%20%20One%20number%20per%20group%20is%20rarely%20enough.%20%60.agg()%60%20computes%20several%2C%20each%20with%20the%20name%20you%20give%20it%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20plots.groupby(%22irrigated%22).agg(%0A%20%20%20%20%20%20%20%20plots%3D(%22plot_no%22%2C%20%22size%22)%2C%20%20%20%20%20%20%20%20%20%20%23%20new%20column%20%3D%20(the%20column%20to%20use%2C%20the%20summary%20to%20compute)%0A%20%20%20%20%20%20%20%20mean_area%3D(%22area_ha%22%2C%20%22mean%22)%2C%0A%20%20%20%20%20%20%20%20largest%3D(%22area_ha%22%2C%20%22max%22)%2C%0A%20%20%20%20)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Each%20line%20reads%20*new%20name%20%3D%20(column%2C%20summary)*.%20The%20summaries%20are%20written%20as%20text%3A%20%60%22sum%22%60%2C%20%60%22mean%22%60%2C%20%60%22median%22%60%2C%20%60%22min%22%60%2C%20%60%22max%22%60%2C%20%60%22first%22%60%2C%20%60%22nunique%22%60%20(how%20many%20different%20values)%2C%20%60%22size%22%60%20and%20%60%22count%22%60.%20All%20of%20them%20but%20%60%22size%22%60%20skip%20missing%20values.%0A%0A%20%20%20%20%23%23%23%20%60size%60%20and%20%60count%60%20are%20not%20the%20same%0A%0A%20%20%20%20%7C%20summary%20%7C%20counts%20%7C%20on%20%60fertilizer_kg%60%2C%20all%20plots%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%22size%22%60%20%7C%20the%20**rows**%20in%20the%20group%2C%20missing%20values%20or%20not%20%7C%202%2C160%20%7C%0A%20%20%20%20%7C%20%60%22count%22%60%20%7C%20the%20**values%20that%20are%20not%20missing**%20in%20that%20column%20%7C%201%2C966%20%7C%0A%0A%20%20%20%20Use%20%60size%60%20for%20%22how%20many%20plots%22%3B%20use%20%60count%60%20for%20%22how%20many%20plots%20have%20a%20fertilizer%20amount%22.%20The%20column%20you%20give%20%60size%60%20makes%20no%20difference%3B%20the%20column%20you%20give%20%60count%60%20is%20the%20whole%20point.%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%20Irrigated%20or%20not%3A%20size%20counts%20rows%3B%20count%20counts%20the%20values%20that%20are%20present%20(194%20plots%20have%20no%20fertilizer%20amount)%0A%20%20%20%20plots.groupby(%22irrigated%22).agg(%0A%20%20%20%20%20%20%20%20plots%3D(%22plot_no%22%2C%20%22size%22)%2C%0A%20%20%20%20%20%20%20%20fertilizer_answered%3D(%22fertilizer_kg%22%2C%20%22count%22)%2C%0A%20%20%20%20%20%20%20%20mean_fertilizer_kg%3D(%22fertilizer_kg%22%2C%20%22mean%22)%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20mean%20fertilizer%20is%20the%20mean%20of%20the%20plots%20that%20**have**%20an%20amount%3A%20%60mean%60%20skips%20the%20missing%20values%20rather%20than%20counting%20them%20as%20zero.%20That%20is%20usually%20what%20you%20want%2C%20and%20section%203%20returns%20to%20it%2C%20because%20a%20weighted%20mean%20needs%20the%20same%20care.%0A%0A%20%20%20%20%23%23%23%20A%20chart%20of%20the%20land%20by%20crop%20(matplotlib)%0A%0A%20%20%20%20A%20bar%20per%20crop%2C%20the%20plots%20with%20no%20crop%20included%20and%20labeled%2C%20so%20the%20chart%20adds%20up%20to%20the%20survey.%20matplotlib%20draws%20what%20it%20is%20given%2C%20so%20the%20table%20is%20made%20first%3A%20missing%20crop%20renamed%20with%20%60fillna%60%2C%20then%20sorted.%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_area%20%3D%20plots.groupby(%22crop%22%2C%20dropna%3DFalse)%5B%22area_ha%22%5D.sum()%0A%20%20%20%20_area.index%20%3D%20_area.index.fillna(%22not%20recorded%22)%20%20%23%20a%20missing%20crop%20gets%20a%20label%20a%20reader%20can%20see%0A%20%20%20%20_area%20%3D%20_area.sort_values()%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6%2C%202.8))%0A%20%20%20%20_ax.barh(_area.index%2C%20_area.values%2C%20color%3D%22%234c78a8%22)%0A%20%20%20%20_ax.set_xlabel(%22area%20(hectares%2C%20all%20plots%20in%20the%20sample)%22)%0A%20%20%20%20_ax.set_title(%22Land%20by%20crop%2C%20survey%20sample%22)%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20you%20know%20pandas%202%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20-%20**Text%20columns%20are%20%60str%60%20now**%2C%20not%20%60object%60.%20%60plots%5B%22crop%22%5D.dtype%60%20is%20%60str%60%2C%20and%20its%20missing%20values%20are%20%60NaN%60.%20%60groupby%60%20treats%20them%20as%20before%3A%20left%20out%20by%20default%2C%20kept%20with%20%60dropna%3DFalse%60.%0A%20%20%20%20-%20**%60observed%3DTrue%60%20is%20the%20default**%20when%20you%20group%20by%20a%20%60category%60%20column%3A%20categories%20that%20no%20row%20has%20are%20left%20out%20of%20the%20result.%20In%20pandas%202%20they%20appeared%20with%20a%20count%20of%200.%20Pass%20%60observed%3DFalse%60%20to%20get%20them%20back.%0A%20%20%20%20-%20Write%20summaries%20as%20text%20(%60%22sum%22%60%2C%20%60%22mean%22%60)%3A%20pandas%203%20no%20longer%20swaps%20a%20NumPy%20function%20such%20as%20%60np.sum%60%20for%20its%20own%20version%20inside%20%60agg%60%2C%20so%20the%20text%20name%20is%20the%20way%20to%20be%20sure%20you%20get%20pandas'%20own.%0A%0A%20%20%20%20All%20of%20them%2C%20in%20one%20place%3A%20primer%20%5BP2%2C%20pandas%202%20to%203%5D(%2F%3Ffile%3D91_primer_pandas_2_to_3.py).%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.1.%20A%20summary%20by%20crop%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20first%20table%20of%20any%20report%20on%20the%20survey%3A%20how%20many%20plots%20of%20each%20crop%2C%20how%20many%20of%20them%20have%20the%20fertilizer%20question%20answered%2C%20and%20how%20much%20land%20they%20cover.%20If%20it%20leaves%20out%20the%20plots%20with%20no%20crop%2C%20its%20totals%20don't%20match%20the%20survey's%2C%20and%20a%20careful%20reader%20stops%20trusting%20the%20rest.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%20%E2%86%92%20one%20row%20per%20crop%2C%20**the%20plots%20with%20no%20crop%20included**%20(as%20a%20missing%20crop)%2C%20with%20three%20columns%3A%0A%0A%20%20%20%20%7C%20crop%20%7C%20plots%20%7C%20with_fertilizer%20%7C%20area_ha%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20Banana%20%7C%20136%20%7C%20120%20%7C%20124.96%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20*(missing)*%20%7C%2014%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20-%20%60plots%60%3A%20how%20many%20plots%3B%0A%20%20%20%20-%20%60with_fertilizer%60%3A%20how%20many%20plots%20have%20a%20%60fertilizer_kg%60%20amount%3B%0A%20%20%20%20-%20%60area_ha%60%3A%20their%20total%20area%20in%20hectares%20(from%20%60area_ha%60%2C%20not%20%60area%60%3A%20%60area%60%20mixes%20hectares%20and%20square%20meters).%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(...%2C%20dropna%3DFalse)%60%20and%20%60.agg()%60%20with%20named%20aggregations%2C%20%60%22size%22%60%2C%20%60%22count%22%60%20and%20%60%22sum%22%60%2C%20all%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20crop_summary(plots%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20crop%20(plots%20with%20no%20crop%20included)%3A%20plots%2C%20with_fertilizer%20and%20area_ha.%22%22%22%0A%20%20%20%20%23%20Exercise%204.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20group%20the%20plots%20by%20crop%2C%20keeping%20the%20plots%20whose%20crop%20is%20missing%0A%20%20%20%20%23%20Step%202%3A%20.agg()%20three%20named%20results%3A%20plots%20(size)%2C%20with_fertilizer%20(count%20of%20fertilizer_kg)%2C%20area_ha%20(sum%20of%20area_ha)%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20result%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.1%22)%0A%0A%0A%40app.function%0Adef%20small_farm()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Nine%20plots%20of%20five%20households%20in%20two%20provinces%2C%20used%20by%20the%20checks%3A%20a%20plot%20in%20square%20meters%2C%20two%20plots%20with%20no%0A%20%20%20%20crop%2C%20plots%20with%20no%20fertilizer%20amount%2C%20households%20with%20different%20weights%2C%20and%20Iloilo%20with%20no%20vegetables.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20hh_id%2C%20plot_no%2C%20crop%2C%20area%2C%20area_unit%2C%20irrigated%2C%20production_kg%2C%20fertilizer_kg%2C%20province%2C%20weight%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%22Yes%22%2C%204000%2C%20100.0%2C%20%22Iloilo%22%2C%20100.0)%2C%0A%20%20%20%20%20%20%20%20(%22H1%22%2C%202%2C%20%22Corn%22%2C%205000.0%2C%20%22sqm%22%2C%20%22No%22%2C%201500%2C%20None%2C%20%22Iloilo%22%2C%20100.0)%2C%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%22No%22%2C%2010000%2C%20200.0%2C%20%22Iloilo%22%2C%20300.0)%2C%0A%20%20%20%20%20%20%20%20(%22H2%22%2C%202%2C%20None%2C%200.25%2C%20%22ha%22%2C%20%22No%22%2C%20500%2C%2020.0%2C%20%22Iloilo%22%2C%20300.0)%2C%0A%20%20%20%20%20%20%20%20(%22H3%22%2C%201%2C%20%22Rice%22%2C%201.5%2C%20%22ha%22%2C%20%22No%22%2C%204500%2C%20None%2C%20%22Isabela%22%2C%2050.0)%2C%0A%20%20%20%20%20%20%20%20(%22H3%22%2C%202%2C%20%22Rice%22%2C%200.5%2C%20%22ha%22%2C%20%22Yes%22%2C%202000%2C%2030.0%2C%20%22Isabela%22%2C%2050.0)%2C%0A%20%20%20%20%20%20%20%20(%22H4%22%2C%201%2C%20%22Corn%22%2C%201.0%2C%20%22ha%22%2C%20%22No%22%2C%203000%2C%2050.0%2C%20%22Isabela%22%2C%20200.0)%2C%0A%20%20%20%20%20%20%20%20(%22H4%22%2C%202%2C%20%22Vegetables%22%2C%202500.0%2C%20%22sqm%22%2C%20%22No%22%2C%202500%2C%20None%2C%20%22Isabela%22%2C%20200.0)%2C%0A%20%20%20%20%20%20%20%20(%22H5%22%2C%201%2C%20None%2C%200.5%2C%20%22ha%22%2C%20%22No%22%2C%201000%2C%20None%2C%20%22Isabela%22%2C%20200.0)%2C%0A%20%20%20%20%5D%0A%20%20%20%20table%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%2C%20%22province%22%2C%20%22weight%22%5D)%0A%20%20%20%20table%5B%22area_ha%22%5D%20%3D%20table%5B%22area%22%5D.where(table%5B%22area_unit%22%5D%20%3D%3D%20%22ha%22%2C%20table%5B%22area%22%5D%20%2F%2010_000)%0A%20%20%20%20table%5B%22yield_t_ha%22%5D%20%3D%20table%5B%22production_kg%22%5D%20%2F%201000%20%2F%20table%5B%22area_ha%22%5D%0A%20%20%20%20return%20table%0A%0A%0A%40app.function%0Adef%20as_rows(result%2C%20keys%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20result%20table%20in%20one%20shape%20for%20the%20checks%3A%20its%20keys%20as%20columns%2C%20rows%20sorted%20by%20them%2C%20columns%20in%20name%20order%2C%0A%20%20%20%20numbers%20rounded.%20So%20a%20correct%20answer%20passes%20whether%20its%20keys%20came%20back%20as%20the%20index%20or%20as%20columns.%22%22%22%0A%20%20%20%20table%20%3D%20result.to_frame()%20if%20isinstance(result%2C%20pd.Series)%20else%20result%0A%20%20%20%20keys%20%3D%20%5Bkeys%5D%20if%20isinstance(keys%2C%20str)%20else%20list(keys)%0A%20%20%20%20if%20any(name%20in%20keys%20for%20name%20in%20table.index.names)%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20table.reset_index()%0A%20%20%20%20table%20%3D%20table.sort_values(keys%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%5Bsorted(table.columns)%5D%0A%20%20%20%20numbers%20%3D%20table.select_dtypes(%22number%22).columns%0A%20%20%20%20table%5Bnumbers%5D%20%3D%20table%5Bnumbers%5D.round(digits)%0A%20%20%20%20return%20table%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%204.1%22%2C%20crop_summary%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20plots%22%2C%20(plots%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table%3A%20a%20plot%20in%20square%20meters%2C%20plots%20with%20no%20crop%2C%20plots%20with%20no%20fertilizer%20amount%22%2C%20(small_farm()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20first%20300%20plots%22%2C%20(plots.head(300)%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20plots%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%22crop%22)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20the%20plots%20with%20no%20crop%20(dropna%3DFalse)%3B%20count%20rows%20with%20size%20and%20fertilizer%20amounts%20with%20count%3B%20add%20up%20area_ha.%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%20summary%20of%20all%20the%20plots.%20The%20last%20row%20should%20be%20the%20plots%20with%20no%20crop.%0A%20%20%20%20try_it(crop_summary%2C%20plots)%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%204.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%20Four%20rows%2C%20no%20missing%20crop%20%7C%20%60groupby%60%20left%20out%20the%20plots%20with%20no%20crop.%20%7C%20%60groupby(%22crop%22%2C%20dropna%3DFalse)%60%20%7C%0A%20%20%20%20%7C%20%60with_fertilizer%60%20equals%20%60plots%60%20in%20every%20row%20%7C%20%60size%60%20used%20for%20both.%20%7C%20%60(%22fertilizer_kg%22%2C%20%22count%22)%60%20%7C%0A%20%20%20%20%7C%20%60plots%60%20is%200%20for%20the%20missing%20crop%20%7C%20%60count%60%20on%20%60crop%60%20counts%20the%20crops%20that%20are%20present%2C%20and%20that%20group%20has%20none.%20%7C%20%60(%22plot_no%22%2C%20%22size%22)%60%20%7C%0A%20%20%20%20%7C%20%60area_ha%60%20in%20the%20thousands%20%7C%20%60area%60%20added%20instead%20of%20%60area_ha%60%3A%20square%20meters%20mixed%20with%20hectares.%20%7C%20%60(%22area_ha%22%2C%20%22sum%22)%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20Must%20provide%20'func'%20or%20tuples%20of%20'(column%2C%20aggfunc)'*%20%7C%20A%20named%20result%20written%20without%20its%20brackets%2C%20or%20with%20a%20list.%20%7C%20%60plots%3D(%22plot_no%22%2C%20%22size%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%23%20Several%20keys%2C%20and%20the%20plots'%20province%0A%0A%20%20%20%20A%20plot%20does%20not%20know%20its%20province%3A%20that%20is%20in%20the%20households%20table.%20The%20cell%20below%20gives%20every%20plot%20its%20household's%20province%2C%20municipality%20code%20(%60psgc%60)%20and%20survey%20weight%2C%20and%20a%20yield%20in%20metric%20tons%20per%20hectare%20(t%2Fha).%20The%20join%20is%20one%20line%20here%3B%20section%205%20teaches%20it%20properly%2C%20with%20the%20checks%20that%20keep%20it%20honest.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Each%20plot%2C%20with%20its%20household's%20province%2C%20municipality%20code%20and%20survey%20weight%20(section%205%20explains%20this%20join)%2C%0A%20%20%20%20%23%20and%20its%20yield%3A%20metric%20tons%20harvested%20per%20hectare%0A%20%20%20%20farm%20%3D%20plots.merge(households%5B%5B%22hh_id%22%2C%20%22province%22%2C%20%22psgc%22%2C%20%22weight%22%5D%5D%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20farm%5B%22yield_t_ha%22%5D%20%3D%20farm%5B%22production_kg%22%5D%20%2F%201000%20%2F%20farm%5B%22area_ha%22%5D%0A%20%20%20%20farm.head()%0A%20%20%20%20return%20(farm%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%20Give%20%60groupby%60%20a%20list%20of%20columns%20and%20each%20group%20is%20one%20**combination**%20of%20their%20values%3A%20one%20per%20province%20and%20crop.%20The%20result's%20index%20then%20has%20two%20levels%20(a%20*MultiIndex*).%20%60.reset_index()%60%20turns%20the%20levels%20back%20into%20ordinary%20columns%2C%20a%20**long**%20table%20with%20one%20row%20per%20combination%2C%20the%20shape%20most%20of%20this%20module%20works%20with.%20%60as_index%3DFalse%60%20does%20the%20same%20inside%20%60groupby%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20One%20row%20per%20province%20and%20crop%3B%20reset_index()%20makes%20province%20and%20crop%20ordinary%20columns%20again%0A%20%20%20%20farm.groupby(%5B%22province%22%2C%20%22crop%22%5D).agg(plots%3D(%22plot_no%22%2C%20%22size%22)%2C%20area_ha%3D(%22area_ha%22%2C%20%22sum%22)).reset_index().head(8)%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%204.2.%20Yields%20by%20province%20and%20crop%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22which%20provinces%20get%20the%20most%20from%20a%20hectare%20of%20rice%3F%22%20is%20the%20question%20an%20extension%20office%20asks%20first.%20This%20table%20answers%20it%20for%20every%20crop%20at%20once%2C%20and%20section%203%20shows%20what%20it%20still%20gets%20wrong.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60farm%60%20table%20(plots%20with%20their%20province%20and%20%60yield_t_ha%60)%20%E2%86%92%20one%20row%20per%20province%20and%20crop%2C%20as%20a%20long%20table%20with%20four%20columns%3A%0A%0A%20%20%20%20%7C%20province%20%7C%20crop%20%7C%20plots%20%7C%20mean_yield%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20Bukidnon%20%7C%20Banana%20%7C%2071%20%7C%2022.69%20%7C%0A%20%20%20%20%7C%20Bukidnon%20%7C%20Corn%20%7C%20130%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20-%20%60plots%60%3A%20how%20many%20plots%20of%20that%20crop%20in%20that%20province%3B%0A%20%20%20%20-%20%60mean_yield%60%3A%20the%20mean%20of%20their%20%60yield_t_ha%60.%0A%0A%20%20%20%20Plots%20with%20**no%20crop**%20have%20no%20place%20in%20a%20yield%20table%3A%20leave%20them%20out%2C%20as%20%60groupby%60%20does%20by%20default.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby%60%20with%20a%20list%20of%20keys%2C%20named%20aggregations%20and%20%60.reset_index()%60%2C%20all%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20crop_yields(farm%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20province%20and%20crop%20(plots%20with%20a%20crop%20only)%3A%20province%2C%20crop%2C%20plots%20and%20mean_yield.%22%22%22%0A%20%20%20%20%23%20Exercise%204.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20group%20the%20plots%20by%20province%20and%20crop%20(the%20default%20leaves%20out%20plots%20with%20no%20crop%2C%20as%20wanted%20here)%0A%20%20%20%20%23%20Step%202%3A%20.agg()%20plots%20(size)%20and%20mean_yield%20(mean%20of%20yield_t_ha)%0A%20%20%20%20%23%20Step%203%3A%20.reset_index()%20so%20province%20and%20crop%20are%20columns%2C%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.2%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(farm)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.2%22%2C%20crop_yields%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20plots%22%2C%20(farm%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table%20of%20two%20provinces%2C%20with%20plots%20that%20have%20no%20crop%22%2C%20(small_farm()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20rice%20and%20corn%20plots%20only%22%2C%20(farm%5Bfarm%5B%22crop%22%5D.isin(%5B%22Rice%22%2C%20%22Corn%22%5D)%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20plots%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%5B%22province%22%2C%20%22crop%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Group%20by%20both%20province%20and%20crop%3B%20leave%20the%20plots%20with%20no%20crop%20out%3B%20mean_yield%20is%20the%20mean%20of%20yield_t_ha.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20table%20of%20yields.%20Sort%20it%20to%20see%20which%20province%20leads%20in%20rice.%0A%20%20%20%20try_it(crop_yields%2C%20farm)%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%204.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%20Rows%20whose%20crop%20is%20missing%20%7C%20%60dropna%3DFalse%60%20kept%20the%20plots%20with%20no%20crop%3B%20a%20yield%20table%20has%20no%20row%20for%20them.%20%7C%20%60groupby(%5B%22province%22%2C%20%22crop%22%5D)%60%20%7C%0A%20%20%20%20%7C%20Four%20rows%2C%20or%20seven%20%7C%20Grouped%20by%20one%20key%20only.%20%7C%20a%20list%20of%20both%20keys%20%7C%0A%20%20%20%20%7C%20%60mean_yield%60%20close%20but%20not%20equal%20%7C%20Mean%20production%20divided%20by%20mean%20area%3A%20a%20different%20question.%20%7C%20%60(%22yield_t_ha%22%2C%20%22mean%22)%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'yield_t_ha'*%20%7C%20The%20function%20was%20given%20%60plots%60%2C%20which%20has%20no%20yield%3B%20the%20check%20gives%20it%20%60farm%60.%20%7C%20the%20Try%20it%20cell's%20argument%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%202%20%C2%B7%20Within%20groups%3A%20transform%2C%20shares%2C%20ranks%2C%20running%20totals%0A%0A%20%20%20%20**The%20problem.**%20What%20share%20of%20each%20household's%20land%20is%20each%20plot%3F%20The%20household's%20total%20is%20one%20number%20per%20household%3B%20the%20share%20needs%20that%20number%20**on%20every%20one%20of%20its%20plots'%20rows**.%20%60groupby(...).sum()%60%20gives%20the%20totals%2C%20but%20as%20a%20table%20of%201%2C190%20households%2C%20not%202%2C160%20plots%2C%20and%20dividing%20one%20by%20the%20other%20fails%20quietly%3A%20pandas%20lines%20the%20two%20up%20by%20their%20index%20(module%203's%20alignment)%2C%20finds%20no%20labels%20in%20common%2C%20and%20fills%20the%20answer%20with%20missing%20values.%20No%20error%2C%20just%20%60NaN%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20trap%3A%20the%20totals%20are%20indexed%20by%20hh_id%2C%20the%20plots%20by%20row%20number%2C%20so%20nothing%20lines%20up%0A%20%20%20%20_wrong%20%3D%20plots%5B%22area_ha%22%5D%20%2F%20plots.groupby(%22hh_id%22)%5B%22area_ha%22%5D.sum()%0A%20%20%20%20mo.md(f%22%60plots%5B'area_ha'%5D%20%2F%20plots.groupby('hh_id')%5B'area_ha'%5D.sum()%60%20gives%20**%7Blen(_wrong)%3A%2C%7D%20values%2C%20%7B_wrong.isna().sum()%3A%2C%7D%20of%20them%20missing**%3A%20the%20plots'%20row%20numbers%20and%20the%20households'%20IDs%2C%20side%20by%20side%2C%20every%20one%20unmatched.%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**The%20tool.**%20%60transform%60%20computes%20the%20same%20summary%20per%20group%2C%20then%20hands%20it%20back%20**once%20per%20row**%2C%20in%20the%20original%20rows'%20order%2C%20so%20it%20lines%20up%20with%20the%20table%20it%20came%20from%3A%0A%0A%20%20%20%20%7C%20%7C%20one%20result%20per%20%7C%20rows%20in%20the%20result%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20%60plots.groupby(%22hh_id%22)%5B%22area_ha%22%5D.sum()%60%20%7C%20group%20%7C%201%2C190%20%7C%0A%20%20%20%20%7C%20%60plots.groupby(%22hh_id%22)%5B%22area_ha%22%5D.transform(%22sum%22)%60%20%7C%20row%3A%20the%20group's%20total%2C%20repeated%20on%20each%20of%20its%20rows%20%7C%202%2C160%20%7C%0A%0A%20%20%20%20Dividing%20a%20column%20by%20its%20group's%20%60transform(%22sum%22)%60%20gives%20each%20row's%20**share%20of%20its%20group's%20total**.%20Choosing%20the%20group%20*is*%20choosing%20the%20denominator%3A%20by%20household%2C%20a%20plot's%20share%20of%20its%20farm%3B%20by%20province%2C%20a%20crop's%20share%20of%20the%20province's%20land.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20Each%20crop's%20share%20of%20its%20province's%20land%20in%20the%20sample%3A%20the%20denominator%20is%20the%20province's%20total%2C%20repeated%20by%20transform%0A%20%20%20%20_area%20%3D%20farm.groupby(%5B%22province%22%2C%20%22crop%22%5D%2C%20as_index%3DFalse)%5B%22area_ha%22%5D.sum()%0A%20%20%20%20_area%5B%22province_total%22%5D%20%3D%20_area.groupby(%22province%22)%5B%22area_ha%22%5D.transform(%22sum%22)%0A%20%20%20%20_area%5B%22share%22%5D%20%3D%20_area%5B%22area_ha%22%5D%20%2F%20_area%5B%22province_total%22%5D%0A%20%20%20%20_area%5B_area%5B%22province%22%5D%20%3D%3D%20%22Nueva%20Ecija%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%20The%20shares%20of%20each%20province%20add%20up%20to%201.%20These%20are%20shares%20of%20the%20**sample's**%20land%3B%20section%203%20turns%20them%20into%20estimates%20for%20the%20provinces.%0A%0A%20%20%20%20%23%23%23%20Ranks%20within%20a%20group%0A%0A%20%20%20%20%60rank%60%20numbers%20values%20from%20smallest%20to%20largest%3B%20%60ascending%3DFalse%60%20from%20largest.%20Grouped%2C%20%60groupby(%22province%22)%5B...%5D.rank(...)%60%20starts%20again%20at%201%20in%20every%20province.%20When%20two%20values%20tie%2C%20%60method%3D%60%20decides%20what%20they%20get.%20Pick%20a%20method%20to%20see%20what%20it%20does%20to%20four%20farms%20of%203.0%2C%202.0%2C%202.0%20and%201.0%20hectares%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20rank_method%20%3D%20mo.ui.dropdown(options%3D%5B%22min%22%2C%20%22average%22%2C%20%22dense%22%2C%20%22first%22%2C%20%22max%22%5D%2C%20value%3D%22min%22%2C%20label%3D%22method%3D%22)%0A%20%20%20%20rank_method%0A%20%20%20%20return%20(rank_method%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(rank_method)%3A%0A%20%20%20%20_farms%20%3D%20pd.DataFrame(%7B%22farm%22%3A%20%5B%22A%22%2C%20%22B%22%2C%20%22C%22%2C%20%22D%22%5D%2C%20%22farm_ha%22%3A%20%5B3.0%2C%202.0%2C%202.0%2C%201.0%5D%7D)%0A%20%20%20%20_farms%5B%22rank%22%5D%20%3D%20_farms%5B%22farm_ha%22%5D.rank(ascending%3DFalse%2C%20method%3Drank_method.value)%0A%20%20%20%20_meaning%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22min%22%3A%20%22tied%20farms%20share%20the%20**best**%20rank%20they%20cover%2C%20and%20the%20next%20rank%20is%20skipped%3A%201%2C%202%2C%202%2C%204.%20This%20is%20how%20sports%20tables%20rank%3A%20two%20farms%20are%20joint%20second%2C%20and%20nobody%20is%20third.%22%2C%0A%20%20%20%20%20%20%20%20%22average%22%3A%20%22tied%20farms%20share%20the%20**average**%20of%20the%20ranks%20they%20cover%3A%202.5%20for%20the%20two%20farms%20that%20cover%20ranks%202%20and%203.%20It%20is%20the%20default.%22%2C%0A%20%20%20%20%20%20%20%20%22dense%22%3A%20%22tied%20farms%20share%20a%20rank%20and%20**no%20rank%20is%20skipped**%3A%201%2C%202%2C%202%2C%203.%20The%20last%20farm%20is%20third%2C%20though%20three%20farms%20are%20larger.%22%2C%0A%20%20%20%20%20%20%20%20%22first%22%3A%20%22ties%20are%20broken%20by%20**order%20in%20the%20table**%3A%20B%20is%202%20and%20C%20is%203%20only%20because%20B%20comes%20first.%20Rarely%20fair.%22%2C%0A%20%20%20%20%20%20%20%20%22max%22%3A%20%22tied%20farms%20share%20the%20**worst**%20rank%20they%20cover%3A%201%2C%203%2C%203%2C%204.%22%2C%0A%20%20%20%20%7D%5Brank_method.value%5D%0A%20%20%20%20mo.hstack(%5B_farms%2C%20mo.md(f%22%60method%3D%5C%22%7Brank_method.value%7D%5C%22%60%3A%20%7B_meaning%7D%22)%5D%2C%20widths%3D%5B1%2C%202%5D%2C%20align%3D%22center%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%20Running%20totals%0A%0A%20%20%20%20%60cumsum()%60%20(cumulative%20sum)%20adds%20up%20a%20column%20as%20it%20goes%20down%3A%20each%20row%20holds%20the%20total%20so%20far.%20The%20order%20of%20the%20rows%20is%20the%20order%20of%20the%20adding%2C%20so%20**sort%20first**.%20Grouped%2C%20%60groupby(...)%5B...%5D.cumsum()%60%20starts%20again%20at%20zero%20in%20every%20group.%0A%0A%20%20%20%20The%20survey's%20fieldwork%20ran%20from%201%20October%20to%2031%20December%202025.%20How%20many%20households%20had%20been%20interviewed%20by%20each%20day%3F%20Count%20the%20interviews%20per%20day%2C%20then%20keep%20a%20running%20total.%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%20Interviews%20per%20day%2C%20in%20date%20order%2C%20and%20the%20running%20total%3A%20the%20fieldwork's%20progress%20for%20the%20whole%20survey%0A%20%20%20%20progress%20%3D%20households.groupby(%22interview_date%22).size().rename(%22interviews%22).reset_index()%0A%20%20%20%20progress%5B%22to_date%22%5D%20%3D%20progress%5B%22interviews%22%5D.cumsum()%0A%20%20%20%20progress.tail(3)%0A%20%20%20%20return%20(progress%2C)%0A%0A%0A%40app.cell%0Adef%20_(progress)%3A%0A%20%20%20%20%23%20The%20running%20total%20as%20a%20line%20(seaborn)%3A%20one%20point%20per%20day%20with%20interviews%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6%2C%203))%0A%20%20%20%20sns.lineplot(data%3Dprogress%2C%20x%3D%22interview_date%22%2C%20y%3D%22to_date%22%2C%20color%3D%22%234c78a8%22%2C%20ax%3D_ax)%0A%20%20%20%20_ax.set(xlabel%3D%22interview%20date%22%2C%20ylabel%3D%22households%20interviewed%20so%20far%22%2C%20title%3D%22Fieldwork%20progress%2C%20all%20provinces%22)%0A%20%20%20%20_ax.xaxis.set_major_locator(mdates.MonthLocator())%0A%20%20%20%20_ax.xaxis.set_major_formatter(mdates.DateFormatter(%22%25b%20%25Y%22))%0A%20%20%20%20sns.despine(ax%3D_ax)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20you%20know%20pandas%202%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20**Adding%20a%20column%20to%20a%20filtered%20table%20is%20safe%20now.**%20Under%20copy-on-write%2C%20%60rice%20%3D%20farm%5Bfarm%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%60%20is%20a%20table%20of%20its%20own%20as%20far%20as%20you%20can%20tell%3A%20%60rice%5B%22share%22%5D%20%3D%20rice%5B%22area_ha%22%5D%20%2F%20rice%5B%22area_ha%22%5D.sum()%60%20adds%20the%20column%20to%20%60rice%60%20only%2C%20never%20to%20%60farm%60%2C%20and%20gives%20no%20warning.%20In%20pandas%202%20the%20same%20line%20gave%20a%20*SettingWithCopyWarning*%2C%20and%20whether%20%60farm%60%20changed%20depended%20on%20how%20the%20filter%20was%20written.%20Module%203%20covers%20copy-on-write%3B%20primer%20%5BP2%5D(%2F%3Ffile%3D91_primer_pandas_2_to_3.py)%20lists%20every%20change.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.3.%20Each%20plot's%20share%20of%20its%20farm%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20household%20whose%20land%20is%20mostly%20one%20plot%20farms%20differently%20from%20one%20spread%20over%20four.%20The%20share%20of%20each%20plot%20in%20its%20household's%20land%20is%20the%20measure%2C%20and%20computing%20it%20is%20the%20pattern%20behind%20every%20share%3A%20a%20value%20over%20its%20group's%20total.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%20%E2%86%92%20a%20Series%20(one%20column)%20with%20each%20plot's%20%60area_ha%60%20divided%20by%20its%20household's%20total%20%60area_ha%60%2C%20**one%20value%20per%20plot%2C%20in%20the%20plots'%20order**%3A%0A%0A%20%20%20%20%7C%20hh_id%20%7C%20plot_no%20%7C%20area_ha%20%7C%20share%20of%20farm%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20HH-0001%20%7C%201%20%7C%200.439%20%7C%200.694%20%7C%0A%20%20%20%20%7C%20HH-0001%20%7C%202%20%7C%200.194%20%7C%200.306%20%7C%0A%20%20%20%20%7C%20HH-0002%20%7C%201%20%7C%200.454%20%7C%200.325%20%7C%0A%0A%20%20%20%20A%20household's%20shares%20add%20up%20to%201%3B%20a%20household%20with%20one%20plot%20has%20a%20share%20of%201.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(%22hh_id%22)%5B%22area_ha%22%5D.transform(%22sum%22)%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20share_of_farm(plots%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20plot's%20area%20as%20a%20share%20of%20its%20household's%20total%20area%2C%20one%20value%20per%20plot%2C%20in%20the%20plots'%20order.%22%22%22%0A%20%20%20%20%23%20Exercise%204.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%20household%20total%3A%20group%20by%20hh_id%2C%20take%20area_ha%2C%20transform(%22sum%22)%0A%20%20%20%20%23%20Step%202%3A%20return%20each%20plot's%20area_ha%20divided%20by%20that%20total%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.3%22)%0A%0A%0A%40app.function%0Adef%20as_values(result%2C%20digits%3A%20int%20%3D%204)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22A%20column's%20values%20in%20order%2C%20rounded%2C%20for%20the%20checks%3A%20its%20name%20and%20index%20don't%20count.%22%22%22%0A%20%20%20%20return%20%5BNone%20if%20pd.isna(value)%20else%20round(float(value)%2C%20digits)%20for%20value%20in%20pd.Series(result).tolist()%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.3%22%2C%20share_of_farm%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20plots%22%2C%20(plots%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table%3A%20a%20plot%20in%20square%20meters%2C%20a%20household%20with%20one%20plot%22%2C%20(small_farm()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20plots%20of%20the%20last%20200%20households%22%2C%20(plots%5Bplots%5B%22hh_id%22%5D%20%3E%20%22HH-0990%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20plots%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Divide%20area_ha%20by%20its%20household's%20total%2C%20from%20groupby('hh_id')%5B'area_ha'%5D.transform('sum').%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%20each%20plot's%20share%20beside%20its%20household%20and%20area%0A%20%20%20%20def%20_try_shares()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20shares%20%3D%20share_of_farm(plots)%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%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20plots%5B%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22area_ha%22%5D%5D.assign(share_of_farm%3Dshares).head(8)%0A%0A%20%20%20%20_try_shares()%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%204.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%20Every%20share%20missing%20(%60None%60)%20%7C%20Divided%20by%20%60groupby(...).sum()%60%3A%20one%20value%20per%20household%2C%20indexed%20by%20%60hh_id%60%2C%20so%20nothing%20lines%20up.%20%7C%20%60.transform(%22sum%22)%60%20%7C%0A%20%20%20%20%7C%20Tiny%20shares%2C%20adding%20up%20to%201%20over%20all%20plots%20%7C%20Divided%20by%20the%20survey's%20total%3A%20the%20wrong%20denominator.%20%7C%20%60groupby(%22hh_id%22)%60%20%7C%0A%20%20%20%20%7C%20Shares%20above%201%2C%20or%20a%20square-meter%20plot%20near%201%20%7C%20%60area%60%20used%20instead%20of%20%60area_ha%60.%20%7C%20%60plots%5B%22area_ha%22%5D%60%20%7C%0A%20%20%20%20%7C%20Shares%20that%20add%20up%20to%201%20per%20crop%20or%20province%20%7C%20Grouped%20by%20the%20wrong%20column.%20%7C%20the%20group%20is%20the%20household%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.4.%20Farm%20size%20rank%20within%20the%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22the%20largest%20farms%20of%20each%20province%22%20is%20a%20common%20request%20from%20a%20program%20that%20targets%20them.%20Ranking%20within%20the%20province%2C%20not%20across%20the%20survey%2C%20compares%20a%20farm%20with%20its%20neighbors%3A%20a%20large%20farm%20in%20Ilocos%20Norte%20may%20be%20small%20by%20Bukidnon's%20measure.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20with%20one%20row%20per%20household%20(%60hh_id%60%2C%20%60province%60%2C%20%60farm_ha%60%2C%20built%20in%20the%20cell%20below)%20%E2%86%92%20a%20Series%20with%20each%20household's%20rank%20**within%20its%20province**%2C%201%20for%20the%20largest%20farm.%20Tied%20farms%20share%20the%20best%20rank%20they%20cover%2C%20and%20the%20next%20rank%20is%20skipped%20(%60method%3D%22min%22%60)%3A%0A%0A%20%20%20%20%7C%20province%20%7C%20farm_ha%20%7C%20rank%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20A%20%7C%203.0%20%7C%201%20%7C%0A%20%20%20%20%7C%20A%20%7C%202.0%20%7C%202%20%7C%0A%20%20%20%20%7C%20A%20%7C%202.0%20%7C%202%20%7C%0A%20%20%20%20%7C%20A%20%7C%201.0%20%7C%204%20%7C%0A%20%20%20%20%7C%20B%20%7C%201.5%20%7C%201%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(...)%5B...%5D.rank(ascending%3D...%2C%20method%3D...)%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20One%20row%20per%20household%3A%20its%20province%20and%20the%20total%20area%20of%20its%20plots%0A%20%20%20%20farms%20%3D%20farm.groupby(%5B%22hh_id%22%2C%20%22province%22%5D%2C%20as_index%3DFalse).agg(farm_ha%3D(%22area_ha%22%2C%20%22sum%22))%0A%20%20%20%20farms.head(3)%0A%20%20%20%20return%20(farms%2C)%0A%0A%0A%40app.function%0Adef%20size_rank(farms%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20household's%20farm%20size%20rank%20within%20its%20province%3A%201%20for%20the%20largest%3B%20ties%20share%20the%20best%20rank%20they%20cover.%22%22%22%0A%20%20%20%20%23%20Exercise%204.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20group%20the%20farms%20by%20province%20and%20take%20farm_ha%0A%20%20%20%20%23%20Step%202%3A%20rank%20from%20the%20largest%20(ascending%3DFalse)%2C%20ties%20sharing%20the%20best%20rank%20(method%3D%22min%22)%2C%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.4%22)%0A%0A%0A%40app.function%0Adef%20farms_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Six%20farms%20in%20two%20provinces%2C%20with%20a%20tie%20in%20A%20followed%20by%20a%20smaller%20farm%2C%20used%20by%20the%20check%20of%20Exercise%204.4.%22%22%22%0A%20%20%20%20return%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%2C%20%22H5%22%2C%20%22H6%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22A%22%2C%20%22A%22%2C%20%22B%22%2C%20%22A%22%2C%20%22B%22%2C%20%22A%22%5D%2C%0A%20%20%20%20%20%20%20%20%22farm_ha%22%3A%20%5B2.0%2C%202.0%2C%200.5%2C%201.0%2C%201.5%2C%203.0%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(farms)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.4%22%2C%20size_rank%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22six%20farms%20in%20two%20provinces%2C%20two%20of%20them%20tied%22%2C%20(farms_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20farms%22%2C%20(farms%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20farms%20of%20Bukidnon%20and%20Iloilo%22%2C%20(farms%5Bfarms%5B%22province%22%5D.isin(%5B%22Bukidnon%22%2C%20%22Iloilo%22%5D)%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20farms%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Rank%20within%20each%20province%20(groupby)%2C%20the%20largest%20first%20(ascending%3DFalse)%2C%20ties%20sharing%20the%20best%20rank%20(method%3D'min').%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farms)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20three%20largest%20farms%20of%20each%20province%2C%20by%20your%20ranks%0A%20%20%20%20def%20_try_ranks()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ranks%20%3D%20size_rank(farms)%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%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20ranked%20%3D%20farms.assign(rank%3Dranks)%0A%20%20%20%20%20%20%20%20return%20ranked%5Branked%5B%22rank%22%5D%20%3C%3D%203%5D.sort_values(%5B%22province%22%2C%20%22rank%22%5D)%0A%0A%20%20%20%20_try_ranks()%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%204.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%20Ranks%20in%20the%20hundreds%20%7C%20Ranked%20across%20the%20survey%2C%20not%20within%20the%20province.%20%7C%20%60groupby(%22province%22)%60%20%7C%0A%20%20%20%20%7C%20The%20smallest%20farm%20is%201%20%7C%20The%20default%20order%20is%20from%20the%20smallest.%20%7C%20%60ascending%3DFalse%60%20%7C%0A%20%20%20%20%7C%20%602.5%60%20for%20the%20tied%20farms%20%7C%20The%20default%20method%20averages%20ties.%20%7C%20%60method%3D%22min%22%60%20%7C%0A%20%20%20%20%7C%20The%20farm%20after%20a%20tie%20is%203%2C%20not%204%20%7C%20%60method%3D%22dense%22%60%20does%20not%20skip%20a%20rank.%20%7C%20%60method%3D%22min%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%23%20Exercise%204.5.%20Fieldwork%20progress%2C%20province%20by%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20survey%20manager%20watches%20each%20province's%20interviews%20add%20up%20against%20its%20target%20of%20170.%20A%20running%20total%20for%20the%20whole%20survey%20hides%20a%20province%20that%20falls%20behind.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20households%20table%20%E2%86%92%20one%20row%20per%20province%20and%20interview%20date%20(only%20the%20dates%20with%20interviews)%2C%20with%3A%0A%0A%20%20%20%20%7C%20province%20%7C%20interview_date%20%7C%20interviews%20%7C%20to_date%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20Bukidnon%20%7C%202025-10-01%20%7C%202%20%7C%202%20%7C%0A%20%20%20%20%7C%20Bukidnon%20%7C%202025-10-02%20%7C%203%20%7C%205%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20Cotabato%20%7C%202025-10-01%20%7C%20%E2%80%A6%20%7C%20*starts%20again*%20%7C%0A%0A%20%20%20%20-%20%60interviews%60%3A%20households%20interviewed%20in%20that%20province%20that%20day%3B%0A%20%20%20%20-%20%60to_date%60%3A%20the%20province's%20running%20total%2C%20starting%20again%20in%20each%20province.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(%5B...%5D).size()%60%2C%20%60.reset_index()%60%2C%20and%20a%20grouped%20%60cumsum()%60%2C%20all%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20fieldwork_progress(households%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20province%20and%20interview%20date%3A%20interviews%20that%20day%20and%20to_date%2C%20the%20province's%20running%20total.%22%22%22%0A%20%20%20%20%23%20Exercise%204.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20count%20the%20households%20per%20province%20and%20interview_date%20(groupby%20with%20both%20keys%2C%20.size())%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20name%20the%20count%20%22interviews%22%20and%20reset%20the%20index%0A%20%20%20%20%23%20Step%202%3A%20add%20to_date%3A%20the%20running%20total%20of%20interviews%20within%20each%20province%20(a%20grouped%20cumsum)%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.5%22)%0A%0A%0A%40app.function%0Adef%20interviews_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Six%20interviews%20in%20two%20provinces%2C%20not%20in%20date%20order%2C%20used%20by%20the%20check%20of%20Exercise%204.5.%22%22%22%0A%20%20%20%20return%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%2C%20%22H5%22%2C%20%22H6%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22A%22%2C%20%22B%22%2C%20%22A%22%2C%20%22A%22%2C%20%22B%22%2C%20%22B%22%5D%2C%0A%20%20%20%20%20%20%20%20%22interview_date%22%3A%20pd.to_datetime(%5B%222025-10-02%22%2C%20%222025-10-03%22%2C%20%222025-10-01%22%2C%20%222025-10-02%22%2C%20%222025-10-01%22%2C%20%222025-10-01%22%5D)%2C%0A%20%20%20%20%20%20%20%20%22hh_size%22%3A%20%5B4%2C%205%2C%203%2C%206%2C%202%2C%204%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.5%22%2C%20fieldwork_progress%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22six%20interviews%20in%20two%20provinces%2C%20not%20in%20date%20order%22%2C%20(interviews_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20households%22%2C%20(households%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20households%20interviewed%20in%20December%22%2C%20(households%5Bhouseholds%5B%22interview_date%22%5D%20%3E%3D%20%222025-12-01%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20households%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%5B%22province%22%2C%20%22interview_date%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20per%20province%20and%20date%20with%20size()%2C%20then%20cumsum%20the%20counts%20grouped%20by%20province%2C%20so%20each%20province%20starts%20again%20at%20zero.%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%20progress%20table%2C%20then%20the%20chart%20below%20draws%20it%0A%20%20%20%20try_it(fieldwork_progress%2C%20households)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%204.5%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60to_date%60%20keeps%20growing%20from%20one%20province%20to%20the%20next%20%7C%20%60cumsum()%60%20without%20%60groupby%60%3A%20one%20running%20total%20for%20the%20whole%20table.%20%7C%20%60groupby(%22province%22)%5B%22interviews%22%5D.cumsum()%60%20%7C%0A%20%20%20%20%7C%20%60to_date%60%20equals%20%60interviews%60%20%7C%20Grouped%20by%20province%20**and**%20date%2C%20so%20every%20group%20is%20one%20row.%20%7C%20group%20the%20cumsum%20by%20province%20only%20%7C%0A%20%20%20%20%7C%20%60interviews%60%20is%20the%20sum%20of%20%60hh_size%60%20%7C%20Summed%20a%20column%20instead%20of%20counting%20rows.%20%7C%20%60.size()%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'interviews'*%20%7C%20The%20count%20is%20still%20named%20%600%60%20or%20%60size%60.%20%7C%20%60.rename(%22interviews%22)%60%20before%20%60.reset_index()%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Each%20province's%20running%20total%20(seaborn)%3A%20one%20line%20per%20province%2C%20from%20the%20long%20table%0A%20%20%20%20_progress%20%3D%20attempt(fieldwork_progress%2C%20households)%0A%20%20%20%20mo.stop(_progress%20is%20NOT_YET%2C%20waiting_for(%22Exercise%204.5%22))%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%203.6))%0A%20%20%20%20sns.lineplot(data%3D_progress%2C%20x%3D%22interview_date%22%2C%20y%3D%22to_date%22%2C%20hue%3D%22province%22%2C%20drawstyle%3D%22steps-post%22%2C%20ax%3D_ax)%0A%20%20%20%20_ax.axhline(170%2C%20color%3D%22%23888888%22%2C%20linewidth%3D0.8%2C%20linestyle%3D%22--%22)%0A%20%20%20%20_ax.set(xlabel%3D%22interview%20date%22%2C%20ylabel%3D%22households%20interviewed%20so%20far%22%2C%20title%3D%22Fieldwork%20progress%20by%20province%20(target%3A%20170%20each)%22)%0A%20%20%20%20_ax.xaxis.set_major_locator(mdates.MonthLocator())%0A%20%20%20%20_ax.xaxis.set_major_formatter(mdates.DateFormatter(%22%25b%20%25Y%22))%0A%20%20%20%20sns.move_legend(_ax%2C%20%22upper%20left%22%2C%20bbox_to_anchor%3D(1%2C%201)%2C%20frameon%3DFalse)%0A%20%20%20%20sns.despine(ax%3D_ax)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20seaborn%20draws%20a%20line%20per%20province%20because%20the%20table%20is%20**long**%2C%20one%20row%20per%20province%20and%20day%2C%20and%20%60hue%3D%22province%22%60%20names%20the%20column%20that%20tells%20the%20lines%20apart.%20Section%204%20comes%20back%20to%20why%20that%20shape%20matters.%0A%0A%20%20%20%20%23%23%203%20%C2%B7%20Survey%20weights%3A%20weighted%20means%2C%20totals%20and%20shares%0A%0A%20%20%20%20**The%20problem.**%20The%20survey%20interviewed%20**170%20households%20in%20every%20province**.%20The%20provinces%20are%20not%20the%20same%20size%3A%20Nueva%20Ecija%20has%20far%20more%20farm%20households%20than%20Ilocos%20Norte.%20A%20plain%20mean%20treats%20every%20interviewed%20household%20alike%2C%20so%20Ilocos%20Norte's%20170%20count%20as%20much%20as%20Nueva%20Ecija's%20170%2C%20and%20the%20mean%20describes%20the%20sample%2C%20not%20the%20provinces.%0A%0A%20%20%20%20**The%20tool.**%20Each%20household%20has%20a%20**survey%20weight**%3A%20the%20number%20of%20farm%20households%20in%20its%20province%20that%20it%20stands%20for%20(the%20codebook's%20words).%20A%20household%20with%20a%20weight%20of%201%2C391%20speaks%20for%201%2C391%20farm%20households.%20Weighting%20gives%20each%20household%20the%20say%20of%20the%20households%20it%20represents.%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%20Interviewed%20and%20represented%3A%20the%20weights%20add%20up%20to%20the%20farm%20households%20each%20province%20has%0A%20%20%20%20households.groupby(%22province%22).agg(%0A%20%20%20%20%20%20%20%20interviewed%3D(%22hh_id%22%2C%20%22size%22)%2C%0A%20%20%20%20%20%20%20%20represented%3D(%22weight%22%2C%20%22sum%22)%2C%0A%20%20%20%20%20%20%20%20mean_weight%3D(%22weight%22%2C%20%22mean%22)%2C%0A%20%20%20%20).round(0).sort_values(%22represented%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%20Three%20formulas%20cover%20almost%20every%20use%20of%20a%20weight%20%60w%60%20and%20a%20value%20%60x%60%3A%0A%0A%20%20%20%20%7C%20estimate%20%7C%20formula%20%7C%20in%20pandas%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20how%20many%20the%20sample%20stands%20for%20%7C%20%CE%A3%20w%20%7C%20%60weight.sum()%60%20%7C%0A%20%20%20%20%7C%20a%20**weighted%20total**%20%7C%20%CE%A3%20w%C2%B7x%20%7C%20%60(x%20*%20weight).sum()%60%20%7C%0A%20%20%20%20%7C%20a%20**weighted%20mean**%20%7C%20%CE%A3%20w%C2%B7x%20%2F%20%CE%A3%20w%20%7C%20%60(x%20*%20weight).sum()%20%2F%20weight.sum()%60%20%7C%0A%20%20%20%20%7C%20a%20**weighted%20share**%20of%20a%20group%20%7C%20the%20group's%20%CE%A3%20w%C2%B7x%20%2F%20everyone's%20%CE%A3%20w%C2%B7x%20%7C%20two%20weighted%20totals%2C%20divided%20%7C%0A%0A%20%20%20%20**A%20worked%20example.**%20Two%20households%20grow%20rice%3A%20one%20yields%203%20t%2Fha%20and%20stands%20for%20100%20households%3B%20the%20other%20yields%206%20t%2Fha%20and%20stands%20for%20300.%20The%20plain%20mean%20is%20(3%20%2B%206)%20%2F%202%20%3D%20**4.5**.%20The%20weighted%20mean%20is%20(100%20%C3%97%203%20%2B%20300%20%C3%97%206)%20%2F%20(100%20%2B%20300)%20%3D%202%2C100%20%2F%20400%20%3D%20**5.25**%3A%20three%20quarters%20of%20the%20households%20it%20represents%20get%206%20t%2Fha%2C%20and%20the%20weighted%20mean%20says%20so.%0A%0A%20%20%20%20**Plots%20carry%20their%20household's%20weight.**%20A%20plot%20of%20a%20household%20that%20stands%20for%201%2C391%20households%20stands%20for%201%2C391%20plots.%20That%20is%20why%20%60farm%60%20has%20a%20%60weight%60%20column%3A%20every%20plot%20inherits%20its%20household's.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20Rice%3A%20the%20mean%20yield%20of%20a%20plot%2C%20unweighted%20and%20weighted%0A%20%20%20%20_rice%20%3D%20farm%5Bfarm%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20_unweighted%20%3D%20_rice%5B%22yield_t_ha%22%5D.mean()%0A%20%20%20%20_weighted%20%3D%20(_rice%5B%22yield_t_ha%22%5D%20*%20_rice%5B%22weight%22%5D).sum()%20%2F%20_rice%5B%22weight%22%5D.sum()%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20rice%20plots%20%7C%20mean%20yield%20(t%2Fha)%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20unweighted%3A%20every%20plot%20alike%20%7C%20**%7B_unweighted%3A.2f%7D**%20%7C%0A%20%20%20%20%7C%20weighted%3A%20every%20plot%20as%20many%20plots%20as%20it%20stands%20for%20%7C%20**%7B_weighted%3A.2f%7D**%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Why%20they%20differ.**%20The%20table%20below%20has%20the%20answer.%20Nueva%20Ecija's%20rice%20plots%20yield%20the%20most%2C%20and%20its%20households%20have%20the%20largest%20weights%2C%20so%20it%20counts%20for%20more%20in%20the%20weighted%20mean%20than%20its%20235%20plots%20alone%20give%20it.%20Ilocos%20Norte%20yields%20less%20and%20weighs%20least.%20An%20unweighted%20mean%20gives%20every%20province%20a%20say%20in%20proportion%20to%20its%20**sample**%3B%20the%20weighted%20mean%2C%20in%20proportion%20to%20its%20**farms**.%20When%20the%20provinces%20with%20the%20largest%20weights%20also%20differ%20in%20what%20you%20measure%2C%20the%20two%20disagree%2C%20and%20only%20the%20weighted%20one%20describes%20the%20provinces.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20Rice%20by%20province%3A%20plots%20in%20the%20sample%2C%20their%20mean%20yield%2C%20and%20how%20much%20each%20plot%20weighs%0A%20%20%20%20farm%5Bfarm%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D.groupby(%22province%22).agg(%0A%20%20%20%20%20%20%20%20plots%3D(%22plot_no%22%2C%20%22size%22)%2C%0A%20%20%20%20%20%20%20%20mean_yield%3D(%22yield_t_ha%22%2C%20%22mean%22)%2C%0A%20%20%20%20%20%20%20%20mean_weight%3D(%22weight%22%2C%20%22mean%22)%2C%0A%20%20%20%20).round(2).sort_values(%22mean_yield%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%20A%20weighted%20mean%20for%20each%20group%0A%0A%20%20%20%20%60groupby%60%20has%20no%20weighted%20mean.%20The%20pattern%20is%20two%20sums%20per%20group%3A%20add%20a%20column%20with%20%60w%C2%B7x%60%2C%20sum%20it%20and%20the%20weights%20per%20group%2C%20and%20divide.%20Every%20piece%20is%20a%20plain%2C%20fast%20%60groupby%60%20sum%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20rice%20%3D%20farm%5Bfarm%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D.assign(wx%3Dlambda%20t%3A%20t%5B%22yield_t_ha%22%5D%20*%20t%5B%22weight%22%5D)%0A%20%20%20%20sums%20%3D%20rice.groupby(%22province%22)%5B%5B%22wx%22%2C%20%22weight%22%5D%5D.sum()%0A%20%20%20%20sums%5B%22wx%22%5D%20%2F%20sums%5B%22weight%22%5D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20the%20weighted%20mean%20yield%20of%20each%20province%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20(%60assign(wx%3Dlambda%20t%3A%20...)%60%20adds%20a%20column%20computed%20from%20the%20table%20%60t%60%20it%20is%20given%2C%20the%20table%20being%20built.)%20Pick%20a%20crop%3A%20the%20chart%20puts%20each%20province's%20unweighted%20and%20weighted%20mean%20side%20by%20side%2C%20and%20the%20survey's%20overall%20in%20the%20last%20row.%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%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%22crop%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%2C%20farm)%3A%0A%20%20%20%20%23%20Unweighted%20and%20weighted%20mean%20yield%20per%20province%2C%20and%20for%20all%20provinces%20together%2C%20as%20two%20dots%20per%20row%20(matplotlib)%0A%20%20%20%20_one%20%3D%20farm%5Bfarm%5B%22crop%22%5D%20%3D%3D%20crop_choice.value%5D.assign(wx%3Dlambda%20t%3A%20t%5B%22yield_t_ha%22%5D%20*%20t%5B%22weight%22%5D)%0A%20%20%20%20_sums%20%3D%20_one.groupby(%22province%22).agg(wx%3D(%22wx%22%2C%20%22sum%22)%2C%20weight%3D(%22weight%22%2C%20%22sum%22)%2C%20unweighted%3D(%22yield_t_ha%22%2C%20%22mean%22))%0A%20%20%20%20_sums.loc%5B%22All%20provinces%22%5D%20%3D%20%5B_one%5B%22wx%22%5D.sum()%2C%20_one%5B%22weight%22%5D.sum()%2C%20_one%5B%22yield_t_ha%22%5D.mean()%5D%0A%20%20%20%20_sums%5B%22weighted%22%5D%20%3D%20_sums%5B%22wx%22%5D%20%2F%20_sums%5B%22weight%22%5D%0A%20%20%20%20_rows%20%3D%20list(range(len(_sums)))%5B%3A%3A-1%5D%20%20%23%20the%20first%20province%20at%20the%20top%2C%20%22All%20provinces%22%20at%20the%20bottom%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%203.4))%0A%20%20%20%20_ax.hlines(_rows%2C%20_sums%5B%22unweighted%22%5D%2C%20_sums%5B%22weighted%22%5D%2C%20color%3D%22%23bbbbbb%22%2C%20linewidth%3D2)%0A%20%20%20%20_ax.scatter(_sums%5B%22unweighted%22%5D%2C%20_rows%2C%20color%3D%22%239e9e9e%22%2C%20label%3D%22unweighted%22%2C%20zorder%3D3)%0A%20%20%20%20_ax.scatter(_sums%5B%22weighted%22%5D%2C%20_rows%2C%20color%3D%22%23e4572e%22%2C%20label%3D%22weighted%22%2C%20zorder%3D3)%0A%20%20%20%20_ax.set_yticks(_rows%2C%20_sums.index)%0A%20%20%20%20_ax.set_xlabel(f%22mean%20yield%20of%20a%20%7Bcrop_choice.value.lower()%7D%20plot%20(t%2Fha)%22)%0A%20%20%20%20_ax.set_title(f%22%7Bcrop_choice.value%7D%3A%20unweighted%20and%20weighted%20mean%20yield%22)%0A%20%20%20%20_ax.legend(frameon%3DFalse%2C%20loc%3D%22upper%20left%22%2C%20bbox_to_anchor%3D(1%2C%201))%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Within%20a%20province%20the%20two%20dots%20sit%20close%3A%20its%20households%20have%20similar%20weights.%20The%20gap%20opens%20in%20the%20last%20row%2C%20where%20provinces%20with%20very%20different%20weights%20are%20mixed.%20That%20is%20where%20an%20unweighted%20mean%20misleads.%0A%0A%20%20%20%20%23%23%23%20Missing%20values%20and%20the%20denominator%0A%0A%20%20%20%20%60fertilizer_kg%60%20is%20missing%20for%20194%20plots.%20%60(x%20*%20w).sum()%60%20skips%20them%2C%20as%20every%20pandas%20sum%20does%2C%20but%20%60w.sum()%60%20does%20not%3A%20the%20weights%20of%20plots%20with%20no%20amount%20still%20sit%20in%20the%20denominator%2C%20as%20if%20those%20plots%20had%20used%20no%20fertilizer%20at%20all.%20The%20weighted%20mean%20comes%20out%20too%20low.%20The%20fix%20is%20to%20leave%20a%20missing%20value's%20weight%20out%20too%2C%20by%20keeping%20only%20the%20rows%20where%20%60x%60%20is%20present%20before%20either%20sum.%0A%0A%20%20%20%20**Another%20question%2C%20another%20number.**%20The%20mean%20yield%20of%20a%20plot%20is%20not%20the%20yield%20of%20the%20average%20hectare.%20Total%20production%20over%20total%20area%20(%60production_kg.sum()%20%2F%20area_ha.sum()%60%2C%20each%20weighted)%20weights%20every%20plot%20by%20its%20area%20as%20well%2C%20and%20for%20rice%20gives%204.37%20t%2Fha%20unweighted%20and%204.51%20weighted.%20Neither%20is%20wrong%3B%20a%20report%20says%20which%20one%20it%20gives.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20you%20know%20pandas%202%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20You%20may%20have%20computed%20a%20weighted%20mean%20per%20group%20with%20%60groupby(...).apply(lambda%20g%3A%20np.average(g%5B%22yield_t_ha%22%5D%2C%20weights%3Dg%5B%22weight%22%5D))%60.%20It%20still%20works%2C%20but%20in%20pandas%203%20%60apply%60%20no%20longer%20passes%20the%20grouping%20columns%20to%20your%20function%20(%60include_groups%3DTrue%60%20was%20removed)%2C%20so%20a%20function%20that%20reads%20%60g%5B%22province%22%5D%60%20raises%20*KeyError%3A%20'province'*.%20The%20two-sums%20pattern%20above%20has%20no%20such%20catch%2C%20and%20it%20is%20much%20faster.%20See%20primer%20%5BP2%5D(%2F%3Ffile%3D91_primer_pandas_2_to_3.py).%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.6.%20A%20weighted%20mean%20that%20skips%20missing%20values%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20estimate%20of%20an%20average%20in%20this%20survey%20goes%20through%20a%20weighted%20mean.%20Written%20once%2C%20right%2C%20with%20the%20missing%20values%20handled%2C%20it%20can%20be%20used%20for%20yields%2C%20fertilizer%2C%20household%20size%2C%20anything.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20two%20Series%20of%20the%20same%20length%2C%20%60values%60%20and%20%60weights%60%20%E2%86%92%20one%20number%2C%20the%20weighted%20mean%20%CE%A3%20w%C2%B7x%20%2F%20%CE%A3%20w%2C%20counting%20**only%20the%20rows%20where%20the%20value%20is%20present**%3A%0A%0A%20%20%20%20%7C%20values%20%7C%20weights%20%7C%20weighted%20mean%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%2C%202%2C%203%20%7C%201%2C%201%2C%202%20%7C%20(1%20%2B%202%20%2B%206)%20%2F%204%20%3D%20**2.25**%20%7C%0A%20%20%20%20%7C%2010%2C%20*missing*%2C%2020%20%7C%201%2C%205%2C%203%20%7C%20(10%20%2B%2060)%20%2F%20(1%20%2B%203)%20%3D%20**17.5**%2C%20not%2070%20%2F%209%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60notna()%60%20and%20boolean%20masks%20(module%203)%2C%20%60*%60%2C%20%60.sum()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20weighted_mean(values%3A%20pd.Series%2C%20weights%3A%20pd.Series)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22The%20weighted%20mean%20of%20values%3A%20sum(w%20*%20x)%20%2F%20sum(w)%2C%20over%20the%20rows%20where%20the%20value%20is%20present.%22%22%22%0A%20%20%20%20%23%20Exercise%204.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20mask%20of%20the%20rows%20where%20the%20value%20is%20present%20(values.notna())%0A%20%20%20%20%23%20Step%202%3A%20keep%20only%20those%20rows%20of%20values%20and%20weights%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20sum%20of%20values%20times%20weights%2C%20divided%20by%20the%20sum%20of%20the%20weights%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.6%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(farm)%3A%0A%20%20%20%20_small%20%3D%20small_farm()%0A%20%20%20%20_rice%20%3D%20farm%5Bfarm%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.6%22%2C%20weighted_mean%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22values%201%2C%202%2C%203%20with%20weights%201%2C%201%2C%202%22%2C%20(pd.Series(%5B1.0%2C%202.0%2C%203.0%5D)%2C%20pd.Series(%5B1.0%2C%201.0%2C%202.0%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22values%2010%2C%20missing%2C%2020%20with%20weights%201%2C%205%2C%203%22%2C%20(pd.Series(%5B10.0%2C%20None%2C%2020.0%5D)%2C%20pd.Series(%5B1.0%2C%205.0%2C%203.0%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20table's%20fertilizer%2C%20some%20of%20it%20missing%22%2C%20(_small%5B%22fertilizer_kg%22%5D%2C%20_small%5B%22weight%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20yields%20of%20the%20survey's%20rice%20plots%22%2C%20(_rice%5B%22yield_t_ha%22%5D%2C%20_rice%5B%22weight%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20fertilizer%20of%20all%20the%20survey's%20plots%2C%20194%20of%20them%20missing%22%2C%20(farm%5B%22fertilizer_kg%22%5D%2C%20farm%5B%22weight%22%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20values%20and%20weights%22%2C%20summarize%3Dlambda%20number%3A%20round(float(number)%2C%204)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20only%20the%20rows%20where%20the%20value%20is%20present%2C%20then%20divide%20the%20sum%20of%20value%20times%20weight%20by%20the%20sum%20of%20those%20rows'%20weights.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20weighted%20mean%20yield%20of%20the%20survey's%20rice%20plots%20(it%20should%20be%20close%20to%204.50)%0A%20%20%20%20_rice%20%3D%20farm%5Bfarm%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%5D%0A%20%20%20%20try_it(weighted_mean%2C%20_rice%5B%22yield_t_ha%22%5D%2C%20_rice%5B%22weight%22%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%204.6%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%602.0%60%20for%20the%20first%20case%20%7C%20The%20plain%20mean%3A%20the%20weights%20are%20not%20used.%20%7C%20%60(values%20*%20weights).sum()%20%2F%20weights.sum()%60%20%7C%0A%20%20%20%20%7C%20%609.0%60%20for%20the%20first%20case%20%7C%20The%20weighted%20total%2C%20not%20divided%20by%20the%20weights.%20%7C%20divide%20by%20%60weights.sum()%60%20%7C%0A%20%20%20%20%7C%20%603.0%60%20for%20the%20first%20case%20%7C%20Divided%20by%20the%20number%20of%20rows%20instead%20of%20the%20weights.%20%7C%20%60weights.sum()%60%20%7C%0A%20%20%20%20%7C%20%607.7778%60%20for%20the%20second%20case%20%7C%20The%20weight%20of%20the%20missing%20value%20is%20still%20in%20the%20denominator.%20%7C%20keep%20%60values.notna()%60%20rows%20in%20both%20sums%20%7C%0A%20%20%20%20%7C%20%60nan%60%20%7C%20Every%20value%20was%20dropped%2C%20or%20%60values%60%20and%20%60weights%60%20come%20from%20different%20tables%20and%20don't%20line%20up.%20%7C%20the%20mask%2C%20and%20where%20the%20two%20Series%20come%20from%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.7.%20Crop%20area%20for%20the%20provinces%2C%20estimated%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22how%20many%20hectares%20of%20rice%20do%20the%20seven%20provinces'%20farm%20households%20grow%3F%22%20is%20a%20total%20for%20the%20provinces%2C%20not%20for%20the%20sample.%20The%20sample's%20880%20hectares%20of%20rice%20are%20the%20land%20of%201%2C034%20plots%3B%20weighted%2C%20they%20stand%20for%20the%20rice%20land%20of%20about%20686%2C000%20farm%20households.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60farm%60%20table%20%E2%86%92%20one%20row%20per%20crop%20(plots%20with%20a%20crop%20only)%2C%20with%3A%0A%0A%20%20%20%20%7C%20crop%20%7C%20plots%20%7C%20area_ha%20%7C%20share%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20Banana%20%7C%20136%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20Rice%20%7C%201%2C034%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20-%20%60plots%60%3A%20plots%20in%20the%20sample%3B%0A%20%20%20%20-%20%60area_ha%60%3A%20the%20**weighted%20total**%20area%2C%20%CE%A3%20weight%20%C3%97%20area_ha%3A%20the%20hectares%20the%20provinces'%20farm%20households%20grow%3B%0A%20%20%20%20-%20%60share%60%3A%20the%20crop's%20share%20of%20the%20weighted%20area%20of%20all%20crops%3B%20the%20shares%20add%20up%20to%201.%0A%0A%20%20%20%20**Tools%3A**%20a%20column%20of%20%60area_ha%20%C3%97%20weight%60%2C%20%60groupby%60%20with%20named%20aggregations%2C%20and%20a%20share%20as%20one%20total%20over%20another%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20crop_area_estimates(farm%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20crop%3A%20plots%20in%20the%20sample%2C%20area_ha%20(weighted%20total%20hectares)%20and%20share%20(of%20all%20crops'%20weighted%20area).%22%22%22%0A%20%20%20%20%23%20Exercise%204.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20add%20a%20column%20with%20each%20plot's%20area%20times%20its%20weight%20(assign)%0A%20%20%20%20%23%20Step%202%3A%20group%20by%20crop%20(plots%20with%20no%20crop%20left%20out)%3A%20plots%20(size)%20and%20area_ha%20(sum%20of%20the%20weighted%20column)%0A%20%20%20%20%23%20Step%203%3A%20add%20share%3A%20each%20crop's%20area_ha%20divided%20by%20the%20total%20of%20the%20area_ha%20column%2C%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.7%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(farm)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.7%22%2C%20crop_area_estimates%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20plots%22%2C%20(farm%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table%3A%20households%20of%20different%20weights%2C%20plots%20with%20no%20crop%22%2C%20(small_farm()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20plots%20of%20Nueva%20Ecija%20and%20Ilocos%20Norte%22%2C%20(farm%5Bfarm%5B%22province%22%5D.isin(%5B%22Nueva%20Ecija%22%2C%20%22Ilocos%20Norte%22%5D)%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20plots%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%22crop%22%2C%20digits%3D2)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22area_ha%20is%20the%20sum%20of%20area_ha%20times%20weight%20per%20crop%3B%20share%20is%20each%20crop's%20area_ha%20over%20the%20column's%20total%3B%20plots%20is%20the%20unweighted%20count.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20provinces'%20crop%20land%2C%20estimated%20from%20the%20sample%0A%20%20%20%20try_it(crop_area_estimates%2C%20farm)%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%204.7%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60area_ha%60%20under%201%2C000%20for%20every%20crop%20%7C%20The%20sample's%20hectares%2C%20not%20weighted.%20%7C%20%60area_ha%20*%20weight%60%2C%20then%20sum%20%7C%0A%20%20%20%20%7C%20Shares%20that%20add%20up%20to%20a%20little%20less%20than%201%20%7C%20The%20denominator%20includes%20the%20plots%20with%20no%20crop.%20%7C%20divide%20by%20the%20result's%20own%20%60area_ha.sum()%60%20%7C%0A%20%20%20%20%7C%20A%20row%20for%20the%20missing%20crop%20%7C%20%60dropna%3DFalse%60%3A%20these%20are%20crop%20estimates.%20%7C%20the%20default%20%60groupby(%22crop%22)%60%20%7C%0A%20%20%20%20%7C%20%60plots%60%20in%20the%20hundreds%20of%20thousands%20%7C%20The%20weights%20were%20summed%2C%20not%20the%20plots%20counted.%20%7C%20%60(%22plot_no%22%2C%20%22size%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%204%20%C2%B7%20Tidy%20data%3A%20pivot%2C%20melt%2C%20crosstab%0A%0A%20%20%20%20**The%20problem.**%20The%20report%20wants%20crop%20land%20as%20a%20grid%3A%20provinces%20down%20the%20side%2C%20crops%20across%20the%20top%2C%20one%20number%20in%20each%20cell.%20That%20is%20the%20table%20people%20read%20best.%20It%20is%20not%20the%20table%20pandas%2C%20seaborn%20and%20Altair%20work%20with%20best%2C%20which%20has%20one%20row%20per%20province%20**and**%20crop.%20Same%20facts%2C%20two%20shapes%2C%20and%20you%20will%20move%20between%20them%20all%20the%20time.%0A%0A%20%20%20%20%7C%20shape%20%7C%20rows%20%7C%20columns%20%7C%20best%20for%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**long**%20%7C%20one%20per%20province%20and%20crop%20(28)%20%7C%20%60province%60%2C%20%60crop%60%2C%20%60area_ha%60%20%7C%20%60groupby%60%2C%20joins%2C%20charts%20by%20color%20or%20facet%20%7C%0A%20%20%20%20%7C%20**wide**%20%7C%20one%20per%20province%20(7)%20%7C%20%60province%60%2C%20then%20one%20column%20per%20crop%20%7C%20reading%2C%20a%20report%2C%20a%20heatmap%20%7C%0A%0A%20%20%20%20A%20table%20is%20**tidy**%20when%20each%20variable%20is%20a%20column%2C%20each%20observation%20is%20a%20row%2C%20and%20each%20value%20is%20a%20cell.%20The%20long%20table%20is%20tidy%3A%20*crop*%20is%20a%20variable%2C%20so%20it%20is%20a%20column.%20In%20the%20wide%20one%20the%20crops'%20names%20have%20become%20column%20headings%3B%20it%20reads%20well%2C%20but%20a%20question%20such%20as%20%22which%20crop%20has%20the%20most%20land%3F%22%20now%20has%20to%20look%20across%20columns%20instead%20of%20down%20one.%0A%0A%20%20%20%20%23%23%23%20Long%20to%20wide%3A%20%60pivot_table%60%0A%0A%20%20%20%20%60pivot_table(index%3D...%2C%20columns%3D...%2C%20values%3D...%2C%20aggfunc%3D...)%60%20puts%20one%20column's%20values%20down%20the%20side%2C%20another's%20across%20the%20top%2C%20and%20summarizes%20a%20third%20into%20each%20cell.%20**Its%20default%20%60aggfunc%60%20is%20%60%22mean%22%60**%2C%20so%20for%20totals%20say%20%60aggfunc%3D%22sum%22%60.%20A%20combination%20with%20no%20rows%20gets%20%60NaN%60%3B%20%60fill_value%3D0%60%20writes%200%20there%20instead%2C%20which%20is%20right%20for%20a%20count%20or%20a%20total%20and%20wrong%20for%20a%20mean%20(a%20province%20with%20no%20banana%20has%20no%20banana%20yield%2C%20not%20a%20yield%20of%200).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20The%20mean%20yield%20of%20each%20crop%20in%20each%20province%3A%20provinces%20down%20the%20side%2C%20crops%20across%20the%20top.%20No%20fill_value%3A%20a%20mean%20of%20no%20plots%20is%20missing.%0A%20%20%20%20yield_grid%20%3D%20farm.pivot_table(index%3D%22province%22%2C%20columns%3D%22crop%22%2C%20values%3D%22yield_t_ha%22%2C%20aggfunc%3D%22mean%22).round(2)%0A%20%20%20%20yield_grid%0A%20%20%20%20return%20(yield_grid%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%60pivot%60%20(without%20%60_table%60)%20only%20reshapes%3A%20it%20expects%20**one%20row%20per%20cell**%20and%20refuses%20anything%20else.%20On%20the%20plots%2C%20with%20many%20per%20province%20and%20crop%2C%20it%20stops%20with%20an%20error%20whose%20last%20line%20says%20why%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20pivot%20cannot%20summarize%3A%20many%20plots%20share%20a%20province%20and%20crop%2C%20so%20it%20has%20no%20single%20value%20for%20the%20cell%0A%20%20%20%20_message%20%3D%20%22no%20error%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20farm.pivot(index%3D%22province%22%2C%20columns%3D%22crop%22%2C%20values%3D%22yield_t_ha%22)%0A%20%20%20%20except%20ValueError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_message%20%3D%20f%22ValueError%3A%20%7B_error%7D%22%0A%20%20%20%20mo.callout(mo.md(f%22%60%7B_message%7D%60%5Cn%5Cn*Duplicate%20entries*%20are%20the%20many%20plots%20per%20province%20and%20crop.%20Use%20%60pivot_table%60%20with%20an%20%60aggfunc%60%2C%20or%20%60pivot%60%20on%20a%20table%20that%20already%20has%20one%20row%20per%20cell.%22)%2C%20kind%3D%22warn%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%20Wide%20to%20long%3A%20%60melt%60%0A%0A%20%20%20%20%60melt%60%20turns%20column%20headings%20back%20into%20values.%20%60id_vars%60%20are%20the%20columns%20that%20stay%20as%20they%20are%20(here%20%60province%60)%3B%20every%20other%20column%20becomes%20rows%2C%20its%20heading%20in%20a%20new%20column%20named%20by%20%60var_name%60%20and%20its%20numbers%20in%20one%20named%20by%20%60value_name%60.%20The%20province%20is%20the%20index%20of%20a%20pivot%20table%2C%20so%20%60reset_index()%60%20first%20makes%20it%20a%20column.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(yield_grid)%3A%0A%20%20%20%20%23%20Back%20to%20long%3A%207%20provinces%20x%204%20crops%20%3D%2028%20rows.%20The%205%20missing%20cells%20(no%20banana%20plots)%20come%20back%20as%20missing%20values%3B%20dropna()%20removes%20them.%0A%20%20%20%20yield_long%20%3D%20yield_grid.reset_index().melt(id_vars%3D%22province%22%2C%20var_name%3D%22crop%22%2C%20value_name%3D%22mean_yield%22)%0A%20%20%20%20mo.md(f%22%60yield_grid%60%20has%20**%7Byield_grid.shape%5B0%5D%7D%20rows%20and%20%7Byield_grid.shape%5B1%5D%7D%20crop%20columns**%3B%20melted%2C%20**%7Blen(yield_long)%7D%20rows**%2C%20of%20which%20%7Byield_long%5B'mean_yield'%5D.isna().sum()%7D%20are%20missing%20(no%20plots%20of%20that%20crop%20there).%20%60yield_long.dropna()%60%20keeps%20the%20%7Byield_long%5B'mean_yield'%5D.notna().sum()%7D%20that%20are%20real%2C%20the%20rows%20Exercise%204.2's%20table%20has.%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%20Counting%20two%20columns%20at%20once%3A%20%60crosstab%60%0A%0A%20%20%20%20%60pd.crosstab(rows%2C%20columns)%60%20counts%20the%20rows%20for%20each%20combination%20of%20two%20columns'%20values%3A%20a%20pivot%20table%20of%20counts%2C%20made%20directly%20from%20the%20two%20columns.%20%60normalize%3D%60%20turns%20the%20counts%20into%20shares%2C%20and%20the%20denominator%20is%20the%20choice%3A%0A%0A%20%20%20%20%7C%20%60normalize%3D%60%20%7C%20each%20number%20is%20a%20share%20of%20%7C%20adds%20up%20to%201%20along%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%22index%22%60%20%7C%20its%20**row's**%20total%20%7C%20each%20row%20%7C%0A%20%20%20%20%7C%20%60%22columns%22%60%20%7C%20its%20**column's**%20total%20%7C%20each%20column%20%7C%0A%20%20%20%20%7C%20%60%22all%22%60%20%7C%20the%20grand%20total%20%7C%20the%20whole%20table%20%7C%0A%0A%20%20%20%20%22What%20share%20of%20rice%20plots%20are%20irrigated%3F%22%20divides%20by%20the%20rice%20row%3A%20%60%22index%22%60.%20%22What%20share%20of%20irrigated%20plots%20are%20rice%3F%22%20divides%20by%20the%20*Yes*%20column%3A%20%60%22columns%22%60.%20Same%20counts%2C%20different%20questions.%20Like%20%60groupby%60%2C%20%60crosstab%60%20leaves%20out%20rows%20with%20a%20missing%20crop.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20Plots%20by%20crop%20and%20irrigation%3A%20counts%2C%20then%20shares%20of%20each%20crop's%20plots%20(rows%20add%20up%20to%201)%0A%20%20%20%20mo.hstack(%5B%0A%20%20%20%20%20%20%20%20pd.crosstab(farm%5B%22crop%22%5D%2C%20farm%5B%22irrigated%22%5D)%2C%0A%20%20%20%20%20%20%20%20pd.crosstab(farm%5B%22crop%22%5D%2C%20farm%5B%22irrigated%22%5D%2C%20normalize%3D%22index%22).round(3)%2C%0A%20%20%20%20%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Only%20rice%20is%20irrigated%20in%20this%20survey%2C%20about%2063%25%20of%20its%20plots.%20Those%20are%20shares%20of%20the%20**sample's**%20plots.%20For%20the%20provinces%2C%20weight%20them%3A%20%60values%3D%60%20gives%20a%20column%20to%20summarize%20instead%20of%20counting%2C%20and%20%60aggfunc%3D%22sum%22%60%20adds%20it%20up%2C%20so%20%60pd.crosstab(rows%2C%20columns%2C%20values%3Dfarm%5B%22weight%22%5D%2C%20aggfunc%3D%22sum%22)%60%20counts%20the%20plots%20each%20cell%20**stands%20for**.%20%60normalize%3D%60%20then%20works%20on%20those%20weighted%20counts.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.8.%20Crop%20area%20as%20a%20province-by-crop%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20table%20that%20goes%20in%20the%20report%3A%20the%20estimated%20hectares%20of%20each%20crop%20in%20each%20province%2C%20as%20a%20grid%20a%20reader%20can%20scan.%20The%20chart%20under%20the%20exercise%20draws%20it.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60farm%60%20table%20%E2%86%92%20a%20wide%20table%20with%20**provinces%20as%20the%20index**%20and%20**one%20column%20per%20crop**%2C%20each%20cell%20the%20weighted%20total%20area%20(%CE%A3%20weight%20%C3%97%20area_ha)%20of%20that%20crop%20in%20that%20province%2C%20and%20**0**%20where%20a%20province%20has%20no%20plots%20of%20a%20crop%3A%0A%0A%20%20%20%20%7C%20province%20%7C%20Banana%20%7C%20Corn%20%7C%20Rice%20%7C%20Vegetables%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20Bukidnon%20%7C%2055%2C784%20%7C%20102%2C079%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20Camarines%20Sur%20%7C%200%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20weighted%20column%20as%20in%20Exercise%204.7%2C%20and%20%60pivot_table%60%20with%20%60aggfunc%3D%22sum%22%60%20and%20%60fill_value%3D0%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20area_table(farm%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Weighted%20crop%20area%20in%20hectares%3A%20provinces%20as%20the%20index%2C%20one%20column%20per%20crop%2C%200%20where%20a%20province%20has%20none.%22%22%22%0A%20%20%20%20%23%20Exercise%204.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20add%20a%20column%20with%20each%20plot's%20area%20times%20its%20weight%0A%20%20%20%20%23%20Step%202%3A%20pivot_table%3A%20province%20down%20the%20side%2C%20crop%20across%20the%20top%2C%20that%20column%20summed%2C%200%20where%20empty%3B%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.8%22)%0A%0A%0A%40app.function%0Adef%20as_grid(result%2C%20index%3A%20str%2C%20digits%3A%20int%20%3D%202)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20wide%20result%20in%20one%20shape%20for%20the%20checks%3A%20its%20row%20labels%20as%20the%20index%2C%20rows%20and%20columns%20in%20name%20order%2C%20numbers%20rounded.%22%22%22%0A%20%20%20%20table%20%3D%20result.set_index(index)%20if%20index%20in%20result.columns%20else%20result%0A%20%20%20%20return%20table.sort_index().sort_index(axis%3D1).round(digits)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(farm)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.8%22%2C%20area_table%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20plots%22%2C%20(farm%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table%3A%20Iloilo%20has%20no%20vegetables%2C%20households%20of%20different%20weights%22%2C%20(small_farm()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20plots%20of%20Bukidnon%2C%20Iloilo%20and%20Isabela%22%2C%20(farm%5Bfarm%5B%22province%22%5D.isin(%5B%22Bukidnon%22%2C%20%22Iloilo%22%2C%20%22Isabela%22%5D)%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20plots%22%2C%20summarize%3Dlambda%20table%3A%20as_grid(table%2C%20%22province%22)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Sum%20area_ha%20times%20weight%20with%20pivot_table(index%3D'province'%2C%20columns%3D'crop'%2C%20...%2C%20aggfunc%3D'sum'%2C%20fill_value%3D0).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20grid%20of%20estimated%20crop%20land%0A%20%20%20%20try_it(area_table%2C%20farm)%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%204.8%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%20Cells%20of%20a%20few%20hundred%2C%20none%20much%20above%201%2C000%20%7C%20The%20mean%2C%20%60pivot_table%60's%20default%2C%20not%20the%20sum.%20%7C%20%60aggfunc%3D%22sum%22%60%20%7C%0A%20%20%20%20%7C%20No%20cell%20above%20200%20%7C%20The%20sample's%20hectares%3A%20the%20area%20is%20not%20weighted.%20%7C%20%60area_ha%20*%20weight%60%20%7C%0A%20%20%20%20%7C%20%60None%60%20in%20some%20cells%20%7C%20Empty%20cells%20left%20missing.%20%7C%20%60fill_value%3D0%60%20%7C%0A%20%20%20%20%7C%20Provinces%20across%20the%20top%20%7C%20%60index%60%20and%20%60columns%60%20swapped.%20%7C%20%60index%3D%22province%22%2C%20columns%3D%22crop%22%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20Index%20contains%20duplicate%20entries%2C%20cannot%20reshape*%20%7C%20%60pivot%60%20used%3A%20it%20cannot%20add%20up.%20%7C%20%60pivot_table%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20grid%20as%20a%20heatmap%20(Altair)%0A%0A%20%20%20%20Altair%2C%20like%20seaborn's%20%60hue%60%2C%20wants%20the%20**long**%20shape%3A%20one%20row%20per%20cell%2C%20with%20a%20column%20for%20each%20thing%20it%20encodes%20(%60x%60%2C%20%60y%60%2C%20%60color%60).%20So%20the%20wide%20grid%20is%20melted%20back%20before%20it%20is%20drawn%3A%20the%20round%20trip%20of%20this%20section%2C%20in%20four%20lines.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20_grid%20%3D%20attempt(area_table%2C%20farm)%0A%20%20%20%20mo.stop(_grid%20is%20NOT_YET%2C%20waiting_for(%22Exercise%204.8%22))%0A%20%20%20%20_long%20%3D%20_grid.reset_index().melt(id_vars%3D%22province%22%2C%20var_name%3D%22crop%22%2C%20value_name%3D%22area_ha%22)%20%20%23%20Altair%20wants%20one%20row%20per%20cell%0A%20%20%20%20_base%20%3D%20alt.Chart(_long).encode(%0A%20%20%20%20%20%20%20%20x%3Dalt.X(%22crop%3AN%22%2C%20title%3DNone%2C%20axis%3Dalt.Axis(orient%3D%22top%22%2C%20labelAngle%3D0))%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22province%3AN%22%2C%20title%3DNone)%2C%0A%20%20%20%20)%0A%20%20%20%20_cells%20%3D%20_base.mark_rect().encode(%0A%20%20%20%20%20%20%20%20color%3Dalt.Color(%22area_ha%3AQ%22%2C%20title%3D%22hectares%22%2C%20scale%3Dalt.Scale(scheme%3D%22blues%22))%2C%0A%20%20%20%20%20%20%20%20tooltip%3D%5B%22province%22%2C%20%22crop%22%2C%20alt.Tooltip(%22area_ha%3AQ%22%2C%20format%3D%22%2C.0f%22)%5D%2C%0A%20%20%20%20)%0A%20%20%20%20_labels%20%3D%20_base.mark_text(fontSize%3D11).encode(%0A%20%20%20%20%20%20%20%20text%3Dalt.Text(%22area_ha%3AQ%22%2C%20format%3D%22%2C.0f%22)%2C%0A%20%20%20%20%20%20%20%20color%3Dalt.when(alt.datum.area_ha%20%3E%20150_000).then(alt.value(%22white%22)).otherwise(alt.value(%22black%22))%2C%0A%20%20%20%20)%0A%20%20%20%20(_cells%20%2B%20_labels).properties(width%3D360%2C%20height%3D230%2C%20title%3D%22Estimated%20crop%20land%20by%20province%20(hectares%2C%20weighted)%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.9.%20Irrigated%20plots%2C%20province%20by%20province%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20an%20irrigation%20program%20asks%20where%20a%20crop's%20plots%20still%20depend%20on%20rain.%20The%20answer%20is%20a%20share%20of%20each%20province's%20plots%20of%20that%20crop%2C%20weighted%2C%20so%20it%20describes%20the%20provinces'%20farms%20and%20not%20the%20sample's.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60farm%60%20table%20and%20a%20crop%20name%20%E2%86%92%20a%20table%20with%20**provinces%20as%20the%20index**%20and%20the%20columns%20**%60No%60**%20and%20**%60Yes%60**%20(%60irrigated%60)%2C%20each%20cell%20the%20weighted%20share%20of%20that%20province's%20plots%20of%20the%20crop%3A%20each%20row%20adds%20up%20to%201.%0A%0A%20%20%20%20%7C%20province%20%7C%20No%20%7C%20Yes%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20Bukidnon%20%7C%200.448%20%7C%200.552%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%0A%20%20%20%20Unweighted%2C%20Bukidnon's%20rice%20would%20read%200.484%20and%200.516%3A%20close%2C%20but%20not%20the%20provinces'%20answer.%0A%0A%20%20%20%20**Tools%3A**%20filter%20to%20the%20crop%20(module%203)%2C%20then%20%60pd.crosstab%60%20with%20%60values%3D%60%2C%20%60aggfunc%3D%22sum%22%60%20and%20%60normalize%3D%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20irrigated_share(farm%3A%20pd.DataFrame%2C%20crop%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22For%20one%20crop%3A%20provinces%20as%20the%20index%2C%20No%20and%20Yes%20as%20columns%2C%20each%20row%20the%20weighted%20shares%20of%20its%20plots.%22%22%22%0A%20%20%20%20%23%20Exercise%204.9%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20plots%20of%20this%20crop%0A%20%20%20%20%23%20Step%202%3A%20pd.crosstab%20of%20province%20by%20irrigated%2C%20adding%20up%20the%20weights%20(values%3D%2C%20aggfunc%3D%22sum%22)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20as%20shares%20of%20each%20province's%20row%20(normalize%3D)%3B%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.9%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(farm)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.9%22%2C%20irrigated_share%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20rice%20plots%22%2C%20(farm%2C%20%22Rice%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table's%20rice%20plots%3A%20Iloilo's%20two%20households%20have%20different%20weights%22%2C%20(small_farm()%2C%20%22Rice%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20rice%20plots%20of%20Nueva%20Ecija%2C%20Iloilo%20and%20Cotabato%22%2C%20(farm%5Bfarm%5B%22province%22%5D.isin(%5B%22Nueva%20Ecija%22%2C%20%22Iloilo%22%2C%20%22Cotabato%22%5D)%5D%2C%20%22Rice%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20corn%20plots%22%2C%20(farm%2C%20%22Corn%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20and%20crops%22%2C%20summarize%3Dlambda%20table%3A%20as_grid(table%2C%20%22province%22%2C%20digits%3D4)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20the%20crop's%20plots%3B%20crosstab%20province%20by%20irrigated%20with%20values%3Dweight%2C%20aggfunc%3D'sum'%2C%20normalize%3D'index'.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(farm)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20weighted%20shares%20of%20rice%20plots%20irrigated%20or%20not%2C%20by%20province%0A%20%20%20%20try_it(irrigated_share%2C%20farm%2C%20%22Rice%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%204.9%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%20Bukidnon's%20rice%20reads%200.484%20and%200.516%20%7C%20Plots%20counted%2C%20not%20weighted.%20%7C%20%60values%3Done%5B%22weight%22%5D%2C%20aggfunc%3D%22sum%22%60%20%7C%0A%20%20%20%20%7C%20Columns%2C%20not%20rows%2C%20add%20up%20to%201%20%7C%20The%20wrong%20denominator.%20%7C%20%60normalize%3D%22index%22%60%20%7C%0A%20%20%20%20%7C%20Every%20cell%20small%2C%20the%20whole%20table%20adding%20up%20to%201%20%7C%20%60normalize%3D%22all%22%60.%20%7C%20%60normalize%3D%22index%22%60%20%7C%0A%20%20%20%20%7C%20Rice%20shares%20that%20look%20like%20all%20crops'%20%7C%20The%20crop%20was%20not%20filtered%20first.%20%7C%20%60farm%5Bfarm%5B%22crop%22%5D%20%3D%3D%20crop%5D%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20values%20cannot%20be%20used%20without%20an%20aggfunc*%20%7C%20%60values%3D%60%20without%20%60aggfunc%3D%60.%20%7C%20%60aggfunc%3D%22sum%22%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%205%20%C2%B7%20Joins%20and%20concatenation%3A%20keys%2C%20validate%3D%2C%20indicator%3D%0A%0A%20%20%20%20**The%20problem.**%20The%20survey's%20facts%20are%20in%20separate%20tables%3A%20a%20plot's%20province%20is%20in%20%60households%60%3B%20a%20household's%20municipality%20name%20is%20in%20%60code_list%60%3B%20the%20prices%20are%20in%2047%20monthly%20files.%20Combining%20two%20tables%20is%20easy%20to%20write%20and%20easy%20to%20get%20wrong%20in%20a%20way%20no%20error%20reports%3A%20**rows%20that%20vanish%2C%20or%20rows%20that%20are%20counted%20twice**.%20This%20section%20is%20about%20combining%20tables%20and%20proving%20you%20did%20neither.%0A%0A%20%20%20%20%23%23%23%20%60merge%60%3A%20rows%20matched%20on%20a%20key%0A%0A%20%20%20%20%60left.merge(right%2C%20on%3D%22hh_id%22%2C%20how%3D...)%60%20puts%20side%20by%20side%20the%20rows%20of%20two%20tables%20whose%20**key**%20(%60hh_id%60)%20is%20equal.%20%60how%60%20decides%20what%20happens%20to%20rows%20with%20no%20match%3A%0A%0A%20%20%20%20%7C%20%60how%3D%60%20%7C%20keeps%20%7C%20rows%20with%20no%20match%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%22inner%22%60%20(the%20default)%20%7C%20rows%20whose%20key%20is%20in%20both%20tables%20%7C%20dropped%2C%20from%20both%20sides%20%7C%0A%20%20%20%20%7C%20%60%22left%22%60%20%7C%20every%20row%20of%20the%20left%20table%20%7C%20the%20right%20side's%20columns%20are%20missing%20%7C%0A%20%20%20%20%7C%20%60%22right%22%60%20%7C%20every%20row%20of%20the%20right%20table%20%7C%20the%20left%20side's%20columns%20are%20missing%20%7C%0A%20%20%20%20%7C%20%60%22outer%22%60%20%7C%20every%20row%20of%20both%20%7C%20missing%20on%20the%20side%20with%20no%20match%20%7C%0A%0A%20%20%20%20A%20small%20example%3A%20three%20households%2C%20four%20plots.%20H3%20has%20no%20plot%3B%20H4's%20plot%20has%20no%20household.%20H1%20has%20two%20plots%2C%20so%20it%20appears%20**twice**%20in%20every%20result%3A%20a%20row%20of%20the%20left%20table%20is%20repeated%20once%20for%20each%20match%20it%20finds.%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%20Three%20households%20and%20four%20plots%3A%20H3%20has%20no%20plot%2C%20plot%20H4%20has%20no%20household%2C%20H1%20has%20two%20plots%0A%20%20%20%20_left%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22H1%22%2C%20%22H2%22%2C%20%22H3%22%5D%2C%20%22province%22%3A%20%5B%22Iloilo%22%2C%20%22Iloilo%22%2C%20%22Isabela%22%5D%7D)%0A%20%20%20%20_right%20%3D%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22H1%22%2C%20%22H1%22%2C%20%22H2%22%2C%20%22H4%22%5D%2C%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%201%5D%7D)%0A%20%20%20%20_counts%20%3D%20pd.DataFrame(%7B%22how%22%3A%20%5B%22inner%22%2C%20%22left%22%2C%20%22right%22%2C%20%22outer%22%5D%7D)%0A%20%20%20%20_counts%5B%22rows%22%5D%20%3D%20%5Blen(_left.merge(_right%2C%20on%3D%22hh_id%22%2C%20how%3D_how))%20for%20_how%20in%20_counts%5B%22how%22%5D%5D%0A%20%20%20%20mo.hstack(%5B_left%2C%20_right%2C%20_counts%2C%20_left.merge(_right%2C%20on%3D%22hh_id%22%2C%20how%3D%22outer%22%2C%20indicator%3DTrue)%5D%2C%20justify%3D%22start%22%2C%20gap%3D1.5)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20last%20table%20is%20the%20outer%20join%20with%20%60indicator%3DTrue%60%2C%20which%20adds%20a%20column%20**%60_merge%60**%20saying%20where%20each%20row%20came%20from%3A%20%60both%60%2C%20%60left_only%60%20or%20%60right_only%60.%20Counting%20it%20(%60value_counts()%60)%20is%20the%20quickest%20way%20to%20see%20what%20a%20join%20matched.%0A%0A%20%20%20%20%23%23%23%20One-to-one%2C%20many-to-one%2C%20many-to-many%3A%20%60validate%3D%60%0A%0A%20%20%20%20How%20many%20rows%20a%20join%20gives%20depends%20on%20how%20often%20each%20key%20appears%20on%20each%20side%3A%0A%0A%20%20%20%20%7C%20relationship%20%7C%20the%20key%20is%20unique%20in%20%7C%20example%20%7C%20rows%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20one-to-one%20%7C%20both%20tables%20%7C%20households%20and%20a%20table%20of%20household%20incomes%20%7C%20as%20many%20as%20the%20matches%20%7C%0A%20%20%20%20%7C%20**many-to-one**%20%7C%20the%20right%20table%20only%20%7C%20plots%20(many%20per%20household)%20and%20households%20(one%20each)%20%7C%20the%20left%20table's%20rows%2C%20never%20more%20%7C%0A%20%20%20%20%7C%20many-to-many%20%7C%20neither%20%7C%20plots%20and%20a%20table%20that%20lists%20a%20household%20twice%20%7C%20**multiplied**%3A%20every%20pair%20of%20matches%20%7C%0A%0A%20%20%20%20A%20key%20you%20**expect**%20to%20be%20unique%20sometimes%20isn't.%20The%20households%20table%20as%20it%20arrives%20(%60course.households(clean%3DFalse)%60)%20has%20six%20households%20entered%20twice.%20Join%20the%20plots%20to%20it%2C%20and%20each%20of%20those%20households'%20plots%20is%20repeated%3A%20no%20error%2C%20no%20warning%2C%20just%20more%20rows%20than%20plots%2C%20and%20every%20total%20built%20on%20them%20too%20high.%0A%0A%20%20%20%20%60validate%3D%22many_to_one%22%60%20writes%20your%20expectation%20into%20the%20join%3A%20pandas%20checks%20that%20the%20right%20table's%20key%20is%20unique%20and%20stops%20if%20it%20is%20not.%20Read%20the%20error%20from%20its%20last%20lines%3A%20it%20names%20the%20duplicates.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20households%20as%20they%20arrive%2C%20with%20six%20entered%20twice%0A%20%20%20%20_raw%20%3D%20course.households(clean%3DFalse)%0A%20%20%20%20_joined%20%3D%20plots.merge(_raw%5B%5B%22hh_id%22%2C%20%22province%22%2C%20%22weight%22%5D%5D%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20plots.merge(_raw%5B%5B%22hh_id%22%2C%20%22province%22%2C%20%22weight%22%5D%5D%2C%20on%3D%22hh_id%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20%20%20%20%20_message%20%3D%20%22no%20error%22%0A%20%20%20%20except%20pd.errors.MergeError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_message%20%3D%20f%22MergeError%3A%20%7B_error%7D%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**Without%20%60validate%3D%60%3A**%20%7Blen(plots)%3A%2C%7D%20plots%20in%2C%20**%7Blen(_joined)%3A%2C%7D%20rows%20out**.%20%7Blen(_joined)%20-%20len(plots)%7D%20plots%20are%20now%20counted%20twice%2C%20and%20nothing%20said%20so.%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**With%20%60validate%3D%5C%22many_to_one%5C%22%60%3A**%20the%20join%20refuses.%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%60%60%5Cn%7B_message%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%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%20That%20is%20why%20the%20%60farm%60%20table%20in%20section%201%20was%20built%20with%20%60validate%3D%22many_to_one%22%60%3A%20with%20the%20clean%20households%20it%20passes%2C%20and%20if%20duplicates%20ever%20come%20back%2C%20it%20stops%20instead%20of%20quietly%20inflating%20every%20total%20in%20this%20notebook.%20Write%20%60validate%3D%60%20on%20every%20join%20whose%20relationship%20you%20know%3B%20it%20is%20one%20argument%2C%20and%20it%20turns%20a%20silent%20error%20into%20a%20loud%20one.%0A%0A%20%20%20%20%23%23%23%20Keys%20that%20don't%20match%3A%20%60indicator%3DTrue%60%0A%0A%20%20%20%20A%20key%20must%20be%20**the%20same%20value%2C%20of%20the%20same%20type**%2C%20on%20both%20sides.%20Module%203's%20first%20trap%20shows%20what%20happens%20when%20it%20isn't%3A%20read%20%60psgc%60%20as%20a%20number%20and%20the%20leading%20zero%20is%20lost%2C%20so%20%600102820000%60%20becomes%20%60102820000%60%2C%20and%20the%20code%20list%20(whose%20codes%20are%20text)%20has%20nothing%20to%20match%20it.%20A%20left%20join%20keeps%20those%20households%20but%20fills%20their%20municipality%20with%20missing%20values%3B%20an%20inner%20join%20drops%20them.%20%60indicator%3DTrue%60%20counts%20them.%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%20psgc%20read%20as%20a%20number%20(module%203's%20trap)%2C%20then%20back%20to%20text%3A%20the%20leading%20zero%20is%20gone.%20Each%20household%20keeps%20its%0A%20%20%20%20%23%20own%20code%2C%20only%20without%20the%20zero%2C%20so%20the%20comparison%20is%20row%20for%20row%0A%20%20%20%20_as_number%20%3D%20households%5B%22psgc%22%5D.astype(%22int64%22)%0A%20%20%20%20_broken%20%3D%20households.assign(psgc%3D_as_number.astype(str))%0A%20%20%20%20_check%20%3D%20_broken.merge(code_list%5B%5B%22code%22%2C%20%22name%22%5D%5D%2C%20left_on%3D%22psgc%22%2C%20right_on%3D%22code%22%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60psgc%60%20read%20as%20a%20number%20gives%20%60%7B_as_number.iloc%5B0%5D%7D%60%20for%20%60%7Bhouseholds%5B'psgc'%5D.iloc%5B0%5D%7D%60.%20Joined%20to%20the%20code%20list%3A%22)%2C%0A%20%20%20%20%20%20%20%20_check%5B%22_merge%22%5D.value_counts()%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7B(_check%5B'_merge'%5D%20%3D%3D%20'left_only').sum()%3A%2C%7D%20of%20%7Blen(_check)%3A%2C%7D%20households%20match%20no%20municipality**%3A%20every%20one%20whose%20code%20starts%20with%200.%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%60indicator%3DTrue%60%20works%20in%20the%20other%20direction%20too.%20An%20**outer**%20join%20of%20the%20households%20to%20the%20code%20list%20finds%20the%20municipalities%20**no%20household%20was%20interviewed%20in**%3A%20they%20are%20the%20%60right_only%60%20rows.%0A%0A%20%20%20%20%23%23%23%20Join%20on%20the%20code%2C%20never%20on%20the%20name%0A%0A%20%20%20%20The%20households%20table%20has%20the%20PSGC%20code%20of%20each%20household's%20municipality%20(%60psgc%60)%3B%20the%20code%20list%20has%20the%20code%20(%60code%60)%20and%20the%20name.%20The%20key%20columns%20have%20different%20names%2C%20so%20%60left_on%3D%60%20and%20%60right_on%3D%60%20say%20which%20is%20which.%20Take%20only%20the%20columns%20you%20need%20from%20the%20right%20table%3A%20a%20column%20both%20tables%20have%20(such%20as%20%60province%60)%20comes%20back%20twice%2C%20as%20%60province_x%60%20and%20%60province_y%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Every%20household%20with%20its%20municipality's%20name%20and%20income%20class%2C%20matched%20on%20the%20code%0A%20%20%20%20named%20%3D%20households.merge(%0A%20%20%20%20%20%20%20%20code_list%5B%5B%22code%22%2C%20%22name%22%2C%20%22income_class%22%5D%5D%2C%20left_on%3D%22psgc%22%2C%20right_on%3D%22code%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22%2C%0A%20%20%20%20)%0A%20%20%20%20_outer%20%3D%20households.merge(code_list%5B%5B%22code%22%2C%20%22name%22%5D%5D%2C%20left_on%3D%22psgc%22%2C%20right_on%3D%22code%22%2C%20how%3D%22outer%22%2C%20indicator%3DTrue)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20named%5B%5B%22hh_id%22%2C%20%22province%22%2C%20%22psgc%22%2C%20%22name%22%2C%20%22income_class%22%5D%5D.head(4)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%7Blen(households)%3A%2C%7D%20households%20in%2C%20%7Blen(named)%3A%2C%7D%20out%2C%20every%20one%20named.%20The%20outer%20join's%20%60_merge%60%3A%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B(_outer%5B'_merge'%5D%20%3D%3D%20'both').sum()%3A%2C%7D%20%60both%60%2C%20%7B(_outer%5B'_merge'%5D%20%3D%3D%20'left_only').sum()%7D%20%60left_only%60%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7B(_outer%5B'_merge'%5D%20%3D%3D%20'right_only').sum()%7D%20%60right_only%60**%3A%20municipalities%20in%20the%20code%20list%20where%20no%20household%20was%20interviewed.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(named%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%20Names%20would%20seem%20easier%3A%20they%20are%20what%20people%20read.%20But%20**names%20repeat%20across%20provinces**%3A%20the%20code%20list%20has%20three%20municipalities%20named%20Quezon%20(in%20Isabela%2C%20Nueva%20Ecija%20and%20Bukidnon)%20and%20two%20each%20named%20Burgos%2C%20San%20Isidro%2C%20Cabatuan%20and%20San%20Fernando.%20A%20code%20is%20unique%3B%20a%20name%20is%20not.%20Join%20on%20a%20name%20and%20every%20household%20in%20a%20Quezon%20matches%20all%20three%2C%20and%20is%20counted%20three%20times%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(named)%3A%0A%20%20%20%20%23%20Population%20joined%20by%20name%3A%20each%20household%20in%20a%20repeated%20name%20matches%20every%20municipality%20of%20that%20name%0A%20%20%20%20_by_name%20%3D%20named.merge(code_list%5B%5B%22name%22%2C%20%22population_2024%22%5D%5D%2C%20on%3D%22name%22%2C%20how%3D%22left%22)%0A%20%20%20%20_repeated%20%3D%20code_list%5B%22name%22%5D.value_counts()%0A%20%20%20%20_message%20%3D%20%22no%20error%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20named.merge(code_list%5B%5B%22name%22%2C%20%22population_2024%22%5D%5D%2C%20on%3D%22name%22%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20except%20pd.errors.MergeError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_message%20%3D%20f%22MergeError%3A%20%7B_error%7D%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**Joined%20on%20the%20name%3A**%20%7Blen(named)%3A%2C%7D%20households%20in%2C%20**%7Blen(_by_name)%3A%2C%7D%20rows%20out**.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Blen(_by_name)%20-%20len(named)%7D%20extra%20rows%2C%20from%20the%20households%20of%20the%20%7B(_repeated%20%3E%201).sum()%7D%20names%20that%20repeat%3A%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B'%2C%20'.join(_repeated%5B_repeated%20%3E%201%5D.index)%7D.%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**With%20%60validate%3D%5C%22many_to_one%5C%22%60**%20the%20join%20refuses%2C%20and%20names%20the%20duplicates%3A%5Cn%5Cn%60%60%60%5Cn%7B_message%7D%5Cn%60%60%60%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%20Grouping%20by%20name%20has%20the%20same%20flaw%20without%20the%20extra%20rows%3A%20%60named.groupby(%22name%22)%60%20puts%20Bukidnon's%20Quezon%20and%20Nueva%20Ecija's%20Quezon%20in%20one%20group.%20Group%20and%20join%20on%20the%20code%3B%20bring%20the%20name%20in%20at%20the%20end%2C%20for%20the%20reader.%0A%0A%20%20%20%20%23%23%23%20Stacking%20tables%3A%20%60concat%60%0A%0A%20%20%20%20A%20join%20puts%20tables%20**side%20by%20side**.%20%60pd.concat(%5Bt1%2C%20t2%2C%20t3%5D)%60%20puts%20them%20**one%20under%20another**%2C%20matching%20their%20columns%20**by%20name**.%20The%20farmgate%20prices%20come%20as%20one%20file%20per%20month%2C%20all%20with%20the%20same%20columns%20until%20December%202024.%20%60keys%3D%60%20labels%20the%20rows%20of%20each%20table%20with%20where%20they%20came%20from%2C%20here%20the%20month%2C%20as%20an%20extra%20index%20level%3B%20%60names%3D%60%20names%20the%20levels%2C%20and%20%60reset_index(level%3D%22month%22)%60%20turns%20the%20month%20into%20a%20column.%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%20Three%20monthly%20price%20files%2C%20read%20by%20hand%2C%20stacked%20with%20the%20month%20as%20a%20key%0A%20%20%20%20_months%20%3D%20%5B%222024-10%22%2C%20%222024-11%22%2C%20%222024-12%22%5D%0A%20%20%20%20_tables%20%3D%20%5Bpd.read_csv(PATHS.prices%20%2F%20f%22farmgate_%7B_month%7D.csv%22)%20for%20_month%20in%20_months%5D%0A%20%20%20%20_stacked%20%3D%20pd.concat(_tables%2C%20keys%3D_months%2C%20names%3D%5B%22month%22%2C%20%22row%22%5D).reset_index(level%3D%22month%22).reset_index(drop%3DTrue)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22Three%20files%20of%20%7B'%2C%20'.join(str(len(_t))%20for%20_t%20in%20_tables)%7D%20rows%3B%20stacked%2C%20**%7Blen(_stacked)%7D%20rows**%2C%20each%20with%20its%20month%3A%22)%2C%0A%20%20%20%20%20%20%20%20_stacked.iloc%5B%5B0%2C%201%2C%2028%2C%2029%2C%2056%2C%2057%5D%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**Columns%20match%20by%20name%2C%20not%20by%20position.**%20From%20January%202025%20the%20files%20have%20different%20headings%20(%60Month%60%2C%20%60Commodity%60%2C%20%60Province%20Code%60%2C%20%60Price%20(PHP%2Fkg)%60).%20Stack%20December%202024%20on%20January%202025%20and%20pandas%20finds%20no%20column%20in%20common%3A%20the%20result%20has%20all%20seven%20headings%2C%20each%20filled%20for%20one%20month's%20rows%20and%20missing%20for%20the%20other's.%20No%20error.%20Module%205%20renames%20the%20columns%20first%3B%20%60course.prices()%60%20does%20all%20of%20it%20for%20you%2C%20and%20is%20what%20later%20modules%20use.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20layout%20changes%20in%20January%202025%3A%20concat%20matches%20columns%20by%20name%2C%20so%20nothing%20lines%20up%0A%20%20%20%20_december%20%3D%20pd.read_csv(PATHS.prices%20%2F%20%22farmgate_2024-12.csv%22)%0A%20%20%20%20_january%20%3D%20pd.read_csv(PATHS.prices%20%2F%20%22farmgate_2025-01.csv%22%2C%20dtype%3D%7B%22Province%20Code%22%3A%20str%7D)%0A%20%20%20%20pd.concat(%5B_december%2C%20_january%5D%2C%20keys%3D%5B%222024-12%22%2C%20%222025-01%22%5D).iloc%5B%5B26%2C%2027%2C%2028%2C%2029%5D%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%20you%20know%20pandas%202%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20-%20**A%20failed%20%60validate%3D%60%20names%20the%20duplicates.**%20pandas%202's%20*MergeError*%20said%20only%20that%20the%20keys%20were%20not%20unique%3B%20pandas%203%20lists%20the%20first%20duplicate%20keys%20under%20the%20message%2C%20as%20above.%0A%20%20%20%20-%20**Anti%20joins%20are%20new%3A**%20%60how%3D%22left_anti%22%60%20keeps%20the%20left%20rows%20that%20have%20**no**%20match%20(and%20%60%22right_anti%22%60%20the%20right%20ones)%2C%20what%20%60indicator%3DTrue%60%20and%20a%20filter%20on%20%60left_only%60%20did%20before.%0A%20%20%20%20-%20**%60concat%60%20with%20both%20%60keys%3D%60%20and%20%60ignore_index%3DTrue%60%20now%20raises**%20*ValueError%3A%20Cannot%20set%20ignore_index%3DTrue%20and%20specify%20keys*.%20In%20pandas%202%20the%20keys%20were%20silently%20dropped.%20To%20label%20the%20rows%20and%20number%20them%20afresh%2C%20use%20%60keys%3D%60%2C%20then%20%60reset_index%60.%0A%0A%20%20%20%20All%20of%20them%20in%20one%20place%3A%20primer%20%5BP2%5D(%2F%3Ffile%3D91_primer_pandas_2_to_3.py).%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.10.%20Households%20in%20every%20municipality%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20field%20office%20plans%20next%20year's%20visits%20from%20a%20list%20of%20**every**%20municipality%20with%20how%20many%20households%20were%20interviewed%20there%2C%20the%20ones%20with%20none%20included.%20A%20municipality%20missing%20from%20the%20list%20is%20one%20nobody%20plans%20a%20visit%20to%3B%20one%20counted%20under%20the%20wrong%20Quezon%20sends%20a%20team%20to%20the%20wrong%20province.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20households%20and%20the%20code%20list%20%E2%86%92%20**one%20row%20per%20municipality%20of%20the%20code%20list**%20(all%20of%20them%2C%20none%20repeated)%2C%20with%3A%0A%0A%20%20%20%20%7C%20code%20%7C%20name%20%7C%20province%20%7C%20households%20%7C%20households_represented%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%200102801000%20%7C%20Adams%20%7C%20Ilocos%20Norte%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20*a%20municipality%20with%20no%20household*%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%200%20%7C%200%20%7C%0A%0A%20%20%20%20-%20%60households%60%3A%20households%20interviewed%20there%3B%0A%20%20%20%20-%20%60households_represented%60%3A%20the%20sum%20of%20their%20weights%3B%0A%20%20%20%20-%20**0**%20in%20both%20where%20no%20household%20was%20interviewed.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby%60%20on%20the%20code%2C%20then%20%60merge%60%20with%20%60left_on%3D%60%2F%60right_on%3D%60%2C%20the%20right%20%60how%3D%60%2C%20%60validate%3D%60%2C%20and%20%60fillna(0)%60%20(module%203)%2C%20all%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20municipality_counts(households%3A%20pd.DataFrame%2C%20code_list%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20municipality%20of%20the%20code%20list%3A%20code%2C%20name%2C%20province%2C%20households%20and%20households_represented%20(0%20where%20none).%22%22%22%0A%20%20%20%20%23%20Exercise%204.10%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20per%20psgc%20code%3A%20households%20(size)%20and%20households_represented%20(sum%20of%20weight)%3B%20reset%20the%20index%0A%20%20%20%20%23%20Step%202%3A%20start%20from%20the%20code%20list's%20code%2C%20name%20and%20province%2C%20and%20join%20the%20counts%20to%20it%20on%20code%20%3D%20psgc%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20keeping%20every%20municipality%2C%20with%20validate%3D%22one_to_one%22%0A%20%20%20%20%23%20Step%203%3A%20fill%20the%20two%20counts'%20missing%20values%20with%200%2C%20keep%20the%20five%20columns%2C%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.10%22)%0A%0A%0A%40app.function%0Adef%20towns_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Four%20households%20and%20four%20municipalities%2C%20three%20of%20them%20named%20Quezon%2C%20used%20by%20the%20check%20of%20Exercise%204.10.%22%22%22%0A%20%20%20%20towns%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22code%22%3A%20%5B%220203122000%22%2C%20%220304922000%22%2C%20%220304925000%22%2C%20%221001317000%22%5D%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%5B%22Quezon%22%2C%20%22Quezon%22%2C%20%22San%20Isidro%22%2C%20%22Quezon%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Isabela%22%2C%20%22Nueva%20Ecija%22%2C%20%22Nueva%20Ecija%22%2C%20%22Bukidnon%22%5D%2C%0A%20%20%20%20%20%20%20%20%22income_class%22%3A%20%5B%223rd%22%2C%20%223rd%22%2C%20%224th%22%2C%20%222nd%22%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20people%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%22Nueva%20Ecija%22%2C%20%22Nueva%20Ecija%22%2C%20%22Bukidnon%22%2C%20%22Nueva%20Ecija%22%5D%2C%0A%20%20%20%20%20%20%20%20%22psgc%22%3A%20%5B%220304922000%22%2C%20%220304922000%22%2C%20%221001317000%22%2C%20%220304922000%22%5D%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20%5B10.0%2C%2020.0%2C%205.0%2C%2015.0%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%20people%2C%20towns%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_people%2C%20_towns%20%3D%20towns_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.10%22%2C%20municipality_counts%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20households%2C%20and%20four%20municipalities%2C%20three%20of%20them%20named%20Quezon%20and%20two%20with%20no%20household%22%2C%20(_people%2C%20_towns))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20households%20and%20the%20code%20list%22%2C%20(households%2C%20code_list))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20households%20of%20Isabela%20and%20Nueva%20Ecija%2C%20and%20the%20code%20list%22%2C%20(households%5Bhouseholds%5B%22province%22%5D.isin(%5B%22Isabela%22%2C%20%22Nueva%20Ecija%22%5D)%5D%2C%20code_list))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%22code%22%2C%20digits%3D2)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20by%20psgc%20(not%20by%20name)%2C%20left-join%20the%20counts%20onto%20the%20code%20list%20on%20code%20%3D%20psgc%2C%20and%20fill%20the%20empty%20counts%20with%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%20the%20municipalities%20with%20the%20fewest%20households%20first%3B%20the%20ones%20with%20none%20should%20come%20first%0A%20%20%20%20def%20_try_towns()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20towns%20%3D%20municipality_counts(households%2C%20code_list)%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%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(towns)%7D%20rows**%20(the%20code%20list%20has%20%7Blen(code_list)%7D%20municipalities).%20The%20fewest%20households%20first%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20towns.sort_values(%5B%22households%22%2C%20%22code%22%5D).head(12)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_try_towns()%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%204.10%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%20202%20rows%2C%20not%20212%20%7C%20An%20inner%20join%20dropped%20the%20municipalities%20with%20no%20household.%20%7C%20the%20code%20list%20on%20the%20left%2C%20%60how%3D%22left%22%60%20%7C%0A%20%20%20%20%7C%20More%20rows%20than%20the%20code%20list%20%7C%20Joined%20on%20the%20name%3A%20every%20Quezon%20matched%20every%20Quezon.%20%7C%20%60left_on%3D%22code%22%2C%20right_on%3D%22psgc%22%60%20%7C%0A%20%20%20%20%7C%20A%20Quezon%20with%20households%20from%20two%20provinces%20%7C%20Grouped%20by%20name%20instead%20of%20code.%20%7C%20%60groupby(%22psgc%22)%60%20%7C%0A%20%20%20%20%7C%20%60None%60%20where%20there%20should%20be%200%20%7C%20The%20empty%20counts%20were%20not%20filled.%20%7C%20%60fillna(0)%60%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20Merge%20keys%20are%20not%20unique*%20%7C%20%60validate%3D%60%20caught%20a%20key%20that%20repeats%3A%20often%20the%20households%20themselves%20were%20joined%2C%20not%20their%20counts.%20%7C%20count%20first%2C%20then%20join%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.11.%20Price%20files%20stacked%20into%20one%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20monthly%20files%20are%20how%20prices%20arrive%2C%20and%20a%20trend%20needs%20them%20in%20one%20table%2C%20each%20row%20knowing%20its%20month.%20The%20month%20is%20in%20the%20**file's%20name**%2C%20not%20inside%20the%20file%2C%20so%20it%20must%20be%20added%20as%20the%20files%20are%20stacked%2C%20or%20it%20is%20lost.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20list%20of%20paths%20to%20monthly%20price%20files%20in%20the%20older%20layout%20(%60province%60%2C%20%60commodity%60%2C%20%60price_php_per_kg%60)%2C%20such%20as%20%60PATHS.prices%20%2F%20%22farmgate_2024-10.csv%22%60%20%E2%86%92%20one%20table%20with%20the%20columns%20**%60month%60**%20(text%2C%20such%20as%20%60%222024-10%22%60%2C%20from%20the%20file's%20name)%2C%20%60province%60%2C%20%60commodity%60%2C%20%60price_php_per_kg%60%2C%20and%20rows%20numbered%20from%200%3A%0A%0A%20%20%20%20%7C%20month%20%7C%20province%20%7C%20commodity%20%7C%20price_php_per_kg%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%202024-10%20%7C%20Ilocos%20Norte%20%7C%20Palay%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20**Tools%3A**%20a%20list%20comprehension%20to%20read%20the%20files%20(Python%20Essentials)%2C%20%60path.stem%60%20(the%20file%20name%20without%20%60.csv%60)%20and%20%60.removeprefix(%22farmgate_%22)%60%2C%20and%20%60pd.concat%60%20with%20%60keys%3D%60%20and%20%60names%3D%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20stack_months(paths%3A%20list)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20price%20files%20at%20these%20paths%20in%20one%20table%3A%20month%20(from%20each%20file's%20name)%2C%20province%2C%20commodity%2C%20price_php_per_kg.%22%22%22%0A%20%20%20%20%23%20Exercise%204.11%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20months%2C%20from%20the%20file%20names%3A%20path.stem.removeprefix(%22farmgate_%22)%20for%20each%20path%0A%20%20%20%20%23%20Step%202%3A%20read%20each%20file%20with%20pd.read_csv%2C%20into%20a%20list%20of%20tables%0A%20%20%20%20%23%20Step%203%3A%20pd.concat%20the%20tables%20with%20keys%3Dthe%20months%20and%20names%3D%5B%22month%22%2C%20%22row%22%5D%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20then%20make%20month%20a%20column%20and%20number%20the%20rows%20from%200%20(reset_index%20twice)%2C%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.11%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_files%20%3D%20lambda%20*months%3A%20(%5BPATHS.prices%20%2F%20f%22farmgate_%7Bmonth%7D.csv%22%20for%20month%20in%20months%5D%2C)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.11%22%2C%20stack_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(%22October%20to%20December%202024%22%2C%20_files(%222024-10%22%2C%20%222024-11%22%2C%20%222024-12%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22January%202022%20alone%22%2C%20_files(%222022-01%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22August%20and%20October%202023%2C%20either%20side%20of%20the%20missing%20month%22%2C%20_files(%222023-08%22%2C%20%222023-10%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22lists%20of%20files%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%5B%22month%22%2C%20%22province%22%2C%20%22commodity%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Take%20each%20month%20from%20its%20file's%20name%20(path.stem%20without%20'farmgate_')%2C%20and%20give%20the%20months%20to%20pd.concat%20as%20keys%3D%3B%20then%20make%20month%20a%20column.%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%20last%20quarter%20of%202024%20in%20one%20table%0A%20%20%20%20try_it(stack_months%2C%20%5BPATHS.prices%20%2F%20f%22farmgate_2024-%7B_m%7D.csv%22%20for%20_m%20in%20(%2210%22%2C%20%2211%22%2C%20%2212%22)%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%204.11%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20No%20%60month%60%20column%20%7C%20The%20tables%20were%20stacked%20without%20%60keys%3D%60%2C%20so%20the%20month%20was%20lost.%20%7C%20%60pd.concat(tables%2C%20keys%3Dmonths%2C%20...)%60%20%7C%0A%20%20%20%20%7C%20%60month%60%20reads%20%60farmgate_2024-10%60%20%7C%20The%20prefix%20is%20still%20on.%20%7C%20%60.removeprefix(%22farmgate_%22)%60%20%7C%0A%20%20%20%20%7C%20A%20column%20%60row%60%20or%20%60level_1%60%20%7C%20%60reset_index()%60%20without%20%60level%3D%60%20brought%20the%20files'%20row%20numbers%20in%20as%20a%20column.%20%7C%20%60reset_index(level%3D%22month%22)%60%2C%20then%20%60reset_index(drop%3DTrue)%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20Cannot%20set%20ignore_index%3DTrue%20and%20specify%20keys*%20%7C%20pandas%203%20refuses%20both%20at%20once.%20%7C%20drop%20%60ignore_index%60%2C%20reset%20the%20index%20after%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20first%20argument%20must%20be%20an%20iterable%20of%20pandas%20objects*%20%7C%20%60pd.concat%60%20was%20given%20one%20table%2C%20or%20the%20tables%20not%20in%20a%20list.%20%7C%20%60pd.concat(%5B...%5D)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20%7C%20use%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20summarize%20by%20group%20%7C%20%60groupby(...).agg(name%3D(column%2C%20summary))%60%20%7C%20%60dropna%3DFalse%60%20where%20missing%20keys%20belong%3B%20%60size%60%20for%20rows%2C%20%60count%60%20for%20values%20present%20%7C%0A%20%20%20%20%7C%20put%20a%20group's%20total%20on%20each%20row%20%7C%20%60groupby(...)%5B...%5D.transform(%22sum%22)%60%20%7C%20the%20group%20you%20choose%20is%20the%20denominator%20%7C%0A%20%20%20%20%7C%20rank%20or%20keep%20a%20running%20total%20in%20groups%20%7C%20%60groupby(...)%5B...%5D.rank(...)%60%2C%20%60.cumsum()%60%20%7C%20%60ascending%60%2C%20%60method%60%3B%20sort%20before%20a%20running%20total%20%7C%0A%20%20%20%20%7C%20estimate%20for%20the%20provinces%20%7C%20%CE%A3%20w%C2%B7x%20for%20totals%2C%20%CE%A3%20w%C2%B7x%20%2F%20%CE%A3%20w%20for%20means%20%7C%20a%20missing%20value's%20weight%20leaves%20the%20denominator%20too%20%7C%0A%20%20%20%20%7C%20reshape%20%7C%20%60pivot_table%60%20(long%20to%20wide)%2C%20%60melt%60%20(wide%20to%20long)%2C%20%60crosstab%60%20%7C%20%60aggfunc%60%20(the%20default%20is%20the%20mean)%3B%20%60normalize%3D%60%20picks%20the%20denominator%20%7C%0A%20%20%20%20%7C%20combine%20%7C%20%60merge%60%20on%20a%20code%2C%20%60concat%60%20with%20%60keys%3D%60%20%7C%20%60validate%3D%60%20on%20every%20join%3B%20%60indicator%3DTrue%60%20to%20count%20what%20matched%20%7C%0A%0A%20%20%20%20Next%2C%20module%205%20(%6040_cleaning_and_time.py%60)%20does%20the%20cleaning%20these%20tables%20arrived%20with%3A%20missing%20values%20and%20not-stated%20codes%2C%20types%2C%20dates%2C%20the%20price%20files'%20change%20of%20layout%2C%20and%20duplicates.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%224.5%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
e3dd4ec197f25951fee14985143cade7