import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Debugging%20drill%3A%20a%20table%20that%20lies%22%2C%0A)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20tables%20every%20other%20cell%20uses.%0A%20%20%20%20import%20io%0A%20%20%20%20import%20warnings%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%2C%20the%20survey's%20tables%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20PATHS%2C%20attempt%2C%20check_cases%2C%20try_it%0A%0A%20%20%20%20HOUSEHOLDS_CSV%20%3D%20PATHS.survey%20%2F%20%22households.csv%22%0A%20%20%20%20PLOTS_CSV%20%3D%20PATHS.survey%20%2F%20%22plots.csv%22%0A%0A%20%20%20%20%23%20The%20survey%20read%20with%20the%20right%20types%2C%20as%20module%203%20reads%20it%3A%20every%20trap%20of%20the%20data%20still%20in%20it%0A%20%20%20%20households%20%3D%20course.households(clean%3DFalse)%0A%20%20%20%20plots%20%3D%20course.plots(clean%3DFalse)%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%20Debugging%20drill%3A%20a%20table%20that%20lies%0A%0A%20%20%20%20Six%20functions%20from%20the%20farm%20survey's%20analysis%2C%20each%20with%20**one**%20mistake%20in%20it%2C%20the%20kind%20everyone%20makes%20with%20pandas.%20One%20of%20them%20stops%20with%20an%20error%2C%20and%20one%20gives%20a%20warning.%20The%20other%20four%20run%20without%20a%20word%2C%20and%20hand%20back%20a%20table%20or%20a%20number%20that%20looks%20fine%20and%20is%20wrong%3A%20a%20table%20that%20lies.%20Each%20check%20below%20starts%20**red**%3A%20that%20is%20the%20point.%20For%20each%20function%3A%0A%0A%20%20%20%201.%20**predict**%3A%20read%20the%20task%20and%20the%20function%2C%20and%20write%20down%20what%20you%20expect%20it%20to%20give%3B%0A%20%20%20%202.%20**run**%20it%20with%20its%20**Try%20it**%20cell%2C%20and%20compare%20what%20it%20gives%20with%20what%20you%20predicted%3B%0A%20%20%20%203.%20**trace**%3A%20find%20the%20line%20that%20makes%20the%20difference%2C%20with%20the%20cell%20the%20drill%20gives%20you%20for%20it%3B%0A%20%20%20%204.%20**fix**%20it%2C%20run%20the%20function's%20cell%20again%2C%20and%20watch%20the%20check%20turn%20green%3B%0A%20%20%20%205.%20**say**%20what%20the%20mistake%20was%2C%20in%20one%20sentence%3A%20to%20your%20table%20in%20a%20training%2C%20or%20in%20a%20comment%20above%20your%20fix.%0A%0A%20%20%20%20When%20something%20stops%20with%20an%20error%2C%20read%20the%20error%20from%20its%20**last%20line**%20first%3A%20it%20names%20the%20kind%20of%20error%20and%20what%20went%20wrong%3B%20the%20lines%20above%20it%20say%20where.%20The%20hints%20say%20what%20you%20see%2C%20never%20what%20to%20change.%20Everything%20you%20need%20was%20taught%20in%20modules%201%20to%203%3B%20each%20task%20links%20to%20the%20section.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20A%20table%20that%20lies%3A%20six%20bugs%20to%20find%20and%20fix%0A%0A%20%20%20%20The%20setup%20cell%20at%20the%20top%20has%20read%20the%20survey%20the%20way%20module%203%20does%2C%20every%20trap%20still%20in%20it%3A%0A%0A%20%20%20%20%7C%20name%20%7C%20what%20it%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60households%60%20%7C%20%60households.csv%60%3A%20one%20row%20per%20household%2C%20%60psgc%60%20as%20text%2C%20with%20its%206%20households%20entered%20twice%20and%20its%20not-stated%20codes%20%7C%0A%20%20%20%20%7C%20%60plots%60%20%7C%20%60plots.csv%60%3A%20one%20row%20per%20plot%2C%20production%20as%20numbers%2C%20areas%20in%20hectares%20or%20square%20meters%20as%20the%20file%20gives%20them%20%7C%0A%20%20%20%20%7C%20%60code_list%60%20%7C%20the%20PSGC%20code%20list%20of%20the%20seven%20provinces'%20cities%20and%20municipalities%2C%20its%20codes%20as%20text%20%7C%0A%20%20%20%20%7C%20%60HOUSEHOLDS_CSV%60%2C%20%60PLOTS_CSV%60%20%7C%20where%20the%20two%20files%20are%2C%20for%20the%20functions%20that%20read%20them%20themselves%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D1.1%20%C2%B7%20Codes%20that%20lose%20their%20leading%20zero%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20PSGC%20code%20is%20how%20the%20survey%20joins%20to%20every%20other%20table%20about%20places.%20A%20code%20that%20loses%20a%20digit%20matches%20nothing%2C%20without%20an%20error.%0A%0A%20%20%20%20The%20code%20list%20names%20each%20province%20by%20a%2010-digit%20code%3A%20the%20first%20five%20digits%20of%20its%20towns'%20codes%2C%20then%20five%20zeros.%20Ilocos%20Norte%20is%20%600102800000%60.%20%60with_province_code(path)%60%20should%20read%20the%20households%20file%2C%20keep%20%60psgc%60%20as%20the%2010-digit%20text%20the%20codebook%20describes%2C%20and%20add%20each%20household's%20%60province_code%60%20in%20the%20same%20form.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20%60households.csv%60%20%E2%86%92%20the%20households%20table%20with%20%60psgc%60%20as%20text%20and%20a%20%60province_code%60%20column.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60with_province_code(HOUSEHOLDS_CSV)%5B%22province_code%22%5D.head(3).tolist()%60%20%7C%20%60%5B'0102800000'%2C%20'0102800000'%2C%20'0102800000'%5D%60%20%7C%0A%20%20%20%20%7C%20%60with_province_code(HOUSEHOLDS_CSV)%5B%22province_code%22%5D.isin(code_list%5B%22province_code%22%5D).sum()%60%20%7C%20%601196%60%3A%20every%20household's%20province%20is%20in%20the%20code%20list%20%7C%0A%0A%20%20%20%20Tools%3A%20reading%20with%20the%20right%20types%20(%5Bmodule%203%2C%20section%202%5D(%2F%3Ffile%3D20_dataframes.py)).%0A%0A%20%20%20%20**Predict.**%20The%20first%20household's%20code%20in%20the%20file%20is%20%600102820000%60.%20What%20province%20code%20will%20the%20function%20give%20it%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20with_province_code(path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20households%20file%2C%20with%20psgc%20as%2010-digit%20text%20and%20each%20household's%20province%20code%20beside%20it.%22%22%22%0A%20%20%20%20%23%20Drill%20D1.1%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20table%20%3D%20pd.read_csv(path)%0A%20%20%20%20table%5B%22province_code%22%5D%20%3D%20table%5B%22psgc%22%5D.astype(str).str%5B%3A5%5D%20%2B%20%2200000%22%0A%20%20%20%20return%20table%0A%0A%0A%40app.function%0Adef%20drill_households_csv()%20-%3E%20io.StringIO%3A%0A%20%20%20%20%22%22%22A%20small%20households%20file%2C%20as%20text%2C%20for%20the%20check%20of%20Drill%20D1.1%3A%20one%20code%20that%20starts%20with%200%2C%20one%20that%20doesn't.%22%22%22%0A%20%20%20%20return%20io.StringIO(%0A%20%20%20%20%20%20%20%20%22hh_id%2Cprovince%2Cpsgc%2Cinterview_date%2Chh_size%2Chead_sex%2Chead_age%2Cweight%5Cn%22%0A%20%20%20%20%20%20%20%20%22HH-0201%2CIsabela%2C0203101000%2C14%2F10%2F2025%2C4%2C1%2C51%2C412.30%5Cn%22%0A%20%20%20%20%20%20%20%20%22HH-0901%2CBukidnon%2C1001301000%2C02%2F12%2F2025%2C6%2C2%2C38%2C355.10%5Cn%22%0A%20%20%20%20)%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%22Drill%20D1.1%22%2C%20with_province_code%2C%0A%20%20%20%20%20%20%20%20%5B(%22households.csv%22%2C%20(HOUSEHOLDS_CSV%2C))%2C%20(%22a%20file%20with%20an%20Isabela%20code%20and%20a%20Bukidnon%20code%22%2C%20(drill_households_csv()%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22files%22%2C%20summarize%3Dlambda%20table%3A%20table%5B%5B%22hh_id%22%2C%20%22psgc%22%2C%20%22province_code%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Look%20at%20the%20%60psgc%60%20values%20the%20function%20read%3A%20how%20many%20digits%20do%20they%20have%3F%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%20read%20the%20households%20file%2C%20and%20look%20at%20psgc%20and%20province_code%20in%20the%20first%20rows.%0A%20%20%20%20try_it(with_province_code%2C%20HOUSEHOLDS_CSV)%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**Trace.**%20The%20province%20code%20is%20cut%20from%20%60psgc%60%2C%20so%20look%20at%20%60psgc%60%20as%20%60read_csv%60%20gives%20it%2C%20without%20any%20options%2C%20and%20at%20the%20type%20pandas%20chose%20for%20it%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%20%23%20Trace%3A%20the%20codes%20as%20read_csv%20reads%20them%20by%20itself%2C%20and%20their%20type%0A%20%20%20%20_codes%20%3D%20pd.read_csv(HOUSEHOLDS_CSV)%5B%22psgc%22%5D%0A%20%20%20%20_codes.head(3).tolist()%2C%20_codes.dtype%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%20Nine%20digits%2C%20and%20the%20type%20%60int64%60%3A%20whole%20numbers.%20The%20file%20says%20%600102820000%60%2C%20but%20a%20number%20has%20no%20leading%20zero%20to%20keep.%20Every%20step%20after%20that%20worked%20on%20the%20wrong%20digits.%20Module%203%2C%20section%202%20shows%20how%20to%20tell%20%60read_csv%60%20the%20type%20of%20one%20column.%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%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60psgc%60%20has%209%20digits%20for%20most%20households%20%7C%20The%20column%20is%20still%20read%20as%20numbers.%20%7C%20%60read_csv%60's%20%60dtype%3D%60%20for%20%60psgc%60%20%7C%0A%20%20%20%20%7C%20%60province_code%60%20is%20right%2C%20but%20%60psgc%60%20still%20has%209%20digits%20%7C%20The%20zeros%20were%20put%20back%20in%20the%20province%20code%20only.%20The%20task%20keeps%20%60psgc%60%20itself%20as%2010-digit%20text.%20%7C%20where%20%60psgc%60%20is%20read%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20Can%20only%20use%20.str%20accessor%20with%20string%20values*%20%7C%20%60.astype(str)%60%20was%20removed%20while%20%60psgc%60%20is%20still%20a%20number.%20%7C%20read%20%60psgc%60%20as%20text%20from%20the%20start%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%20Drill%20D1.2%20%C2%B7%20%60and%60%20for%20%60%26%60%2C%20and%20missing%20brackets%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20almost%20every%20analysis%20filters%20rows%20on%20two%20conditions%20at%20once.%20The%20way%20pandas%20combines%20them%20is%20not%20the%20way%20Python's%20%60if%60%20does.%0A%0A%20%20%20%20%60irrigated_rice(plots)%60%20should%20give%20the%20plots%20that%20grow%20rice%20**and**%20are%20irrigated%3A%20the%20rows%20whose%20%60crop%60%20is%20%60%22Rice%22%60%20and%20whose%20%60irrigated%60%20is%20%60%22Yes%22%60.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%20%E2%86%92%20the%20rows%20of%20irrigated%20rice%20plots%2C%20every%20column%20kept.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60len(irrigated_rice(plots))%60%20%7C%20%60651%60%20%7C%0A%0A%20%20%20%20Tools%3A%20filtering%20on%20two%20conditions%20(%5Bmodule%203%2C%20section%203%5D(%2F%3Ffile%3D20_dataframes.py)).%0A%0A%20%20%20%20**Predict.**%20Will%20the%20function%20run%3F%20If%20it%20stops%2C%20what%20will%20the%20last%20line%20of%20the%20error%20say%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20irrigated_rice(plots%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20plots%20that%20grow%20rice%20and%20are%20irrigated.%22%22%22%0A%20%20%20%20%23%20Drill%20D1.2%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20return%20plots%5Bplots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22%20and%20plots%5B%22irrigated%22%5D%20%3D%3D%20%22Yes%22%5D%0A%0A%0A%40app.function%0Adef%20drill_plots()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Four%20small%20plots%2C%20one%20of%20each%20kind%20of%20crop%20and%20water%2C%20for%20the%20checks%20of%20Drills%20D1.2%20to%20D1.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%22HH-0001%22%2C%20%22HH-0001%22%2C%20%22HH-0002%22%2C%20%22HH-0003%22%5D%2C%0A%20%20%20%20%20%20%20%20%22plot_no%22%3A%20%5B1%2C%202%2C%201%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22crop%22%3A%20%5B%22Rice%22%2C%20%22Rice%22%2C%20%22Corn%22%2C%20%22Corn%22%5D%2C%0A%20%20%20%20%20%20%20%20%22area%22%3A%20%5B0.439%2C%201940.0%2C%200.5%2C%205980.0%5D%2C%0A%20%20%20%20%20%20%20%20%22area_unit%22%3A%20%5B%22ha%22%2C%20%22sqm%22%2C%20%22ha%22%2C%20%22sqm%22%5D%2C%0A%20%20%20%20%20%20%20%20%22irrigated%22%3A%20%5B%22No%22%2C%20%22Yes%22%2C%20%22Yes%22%2C%20%22No%22%5D%2C%0A%20%20%20%20%20%20%20%20%22production_kg%22%3A%20%5B1640%2C%20700%2C%201990%2C%202200%5D%2C%0A%20%20%20%20%20%20%20%20%22fertilizer_kg%22%3A%20%5B89.0%2C%20np.nan%2C%200.0%2C%20np.nan%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%22Drill%20D1.2%22%2C%20irrigated_rice%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20survey's%20plots%22%2C%20(plots%2C))%2C%20(%22four%20plots%3A%20rice%20or%20corn%2C%20irrigated%20or%20not%22%2C%20(drill_plots()%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20table%3A%20table%5B%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22crop%22%2C%20%22irrigated%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Read%20the%20error's%20last%20line%3A%20what%20is%20ambiguous%3F%20Once%20that%20error%20is%20gone%2C%20read%20the%20next%20one%20the%20same%20way.%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%20filter%20the%20survey's%20plots%2C%20and%20read%20the%20error's%20last%20line.%0A%20%20%20%20try_it(irrigated_rice%2C%20plots)%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**Trace.**%20The%20error's%20last%20line%20says%20*the%20truth%20value%20of%20a%20Series%20is%20ambiguous*.%20A%20**truth%20value**%20is%20one%20%60True%60%20or%20%60False%60.%20Look%20at%20what%20one%20side%20of%20the%20%60and%60%20is%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%20%23%20Trace%3A%20one%20side%20of%20the%20condition%2C%20by%20itself%0A%20%20%20%20(plots%5B%22crop%22%5D%20%3D%3D%20%22Rice%22).head()%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%20Not%20one%20%60True%60%20or%20%60False%60%2C%20but%20one%20for%20every%20row.%20%60and%60%20needs%20a%20single%20answer%20for%20each%20side%2C%20and%20pandas%20will%20not%20guess%20whether%20a%20column%20of%20them%20means%20%22all%20true%22%20or%20%22any%20true%22.%20Module%203%2C%20section%203%20combines%20two%20such%20columns%20row%20by%20row.%0A%0A%20%20%20%20Then%20expect%20a%20second%20error.%20If%20you%20change%20only%20the%20word%2C%20the%20next%20run%20stops%20again%2C%20with%20a%20%60TypeError%60%20about%20%60'rand_'%60%20that%20names%20a%20%60str%60%3A%20Python%20worked%20out%20%60%22Rice%22%20%26%20plots%5B%22irrigated%22%5D%60%20before%20either%20%60%3D%3D%60.%20Read%20it%20as%20a%20question%20about%20the%20order%20in%20which%20Python%20does%20the%20operations.%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%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*ValueError%3A%20The%20truth%20value%20of%20a%20Series%20is%20ambiguous*%20%7C%20%60and%60%20(or%20%60or%60)%20between%20two%20columns%20of%20True%20and%20False.%20%7C%20%60%26%60%20and%20%60%5C%7C%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20operation%20'rand_'%20not%20supported%20for%20dtype%20'str'*%20%7C%20%60%26%60%20without%20brackets%3A%20Python%20combined%20%60%22Rice%22%60%20with%20a%20column%20before%20comparing.%20%7C%20a%20pair%20of%20brackets%20around%20each%20comparison%20%7C%0A%20%20%20%20%7C%20Too%20many%20rows%2C%20rice%20or%20irrigated%20%7C%20%60%5C%7C%60%20(or)%20where%20the%20task%20asks%20for%20both.%20%7C%20%60%26%60%20%7C%0A%20%20%20%20%7C%20No%20rows%20at%20all%20%7C%20A%20value%20spelled%20unlike%20the%20file%3A%20%60%22rice%22%60%2C%20%60%22yes%22%60.%20%7C%20the%20values%20in%20%60plots%5B%22crop%22%5D%60%20and%20%60plots%5B%22irrigated%22%5D%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Drill%20D1.3%20%C2%B7%20Comparing%20with%20NaN%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20before%20anyone%20reports%20fertilizer%20use%2C%20they%20need%20to%20know%20how%20many%20plots%20have%20no%20fertilizer%20recorded.%20A%20count%20of%20zero%20says%20the%20column%20is%20complete%3B%20if%20that%20is%20false%2C%20every%20average%20built%20on%20it%20is%20too.%0A%0A%20%20%20%20In%20%60plots.csv%60%2C%20a%20plot%20without%20fertilizer%20recorded%20has%20its%20%60fertilizer_kg%60%20left%20blank%2C%20which%20pandas%20reads%20as%20%60NaN%60%2C%20%22not%20a%20number%22.%20%60count_no_fertilizer(plots)%60%20should%20count%20those%20plots.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%20%E2%86%92%20how%20many%20plots%20have%20no%20%60fertilizer_kg%60%2C%20as%20a%20whole%20number.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60count_no_fertilizer(plots)%60%20%7C%20%60195%60%20%7C%0A%0A%20%20%20%20Tools%3A%20NaN%20(%5Bmodule%202%2C%20section%204%5D(%2F%3Ffile%3D10_arrays.py)).%0A%0A%20%20%20%20**Predict.**%20The%20function%20compares%20each%20value%20with%20%60np.nan%60.%20What%20count%20will%20it%20give%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20count_no_fertilizer(plots%3A%20pd.DataFrame)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22How%20many%20plots%20have%20no%20fertilizer%20recorded.%22%22%22%0A%20%20%20%20%23%20Drill%20D1.3%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20return%20int((plots%5B%22fertilizer_kg%22%5D%20%3D%3D%20np.nan).sum())%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%22Drill%20D1.3%22%2C%20count_no_fertilizer%2C%0A%20%20%20%20%20%20%20%20%5B(%22the%20survey's%20plots%22%2C%20(plots%2C))%2C%20(%22four%20plots%2C%20two%20blanks%20and%20one%200.0%22%2C%20(drill_plots()%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22plots%20with%20fertilizer%20everywhere%22%2C%20(drill_plots().fillna(%7B%22fertilizer_kg%22%3A%2025.0%7D)%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20hint%3D%22A%20count%20of%200%20from%20a%20column%20with%20blanks%3A%20can%20the%20comparison%20ever%20be%20true%3F%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%20count%20the%20survey's%20plots%20with%20no%20fertilizer%20recorded.%0A%20%20%20%20try_it(count_no_fertilizer%2C%20plots)%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**Trace.**%20No%20error%2C%20and%20a%20count%20of%200.%20Yet%20the%20first%20rows%20of%20the%20file%20already%20have%20a%20blank.%20Ask%20Python%20the%20question%20the%20function%20asks%2C%20about%20one%20value%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%20%23%20Trace%3A%20is%20a%20missing%20value%20equal%20to%20NaN%3F%0A%20%20%20%20_first_blank%20%3D%20plots%5B%22fertilizer_kg%22%5D.iloc%5B1%5D%20%20%23%20HH-0001's%20plot%202%3A%20blank%20in%20the%20file%0A%20%20%20%20_first_blank%2C%20_first_blank%20%3D%3D%20np.nan%2C%20np.nan%20%3D%3D%20np.nan%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%20value%20is%20%60nan%60%2C%20and%20still%20%60nan%20%3D%3D%20np.nan%60%20is%20%60False%60%3A%20NaN%20is%20never%20equal%20to%20anything.%20A%20comparison%20with%20%60%3D%3D%60%20cannot%20find%20it.%20Module%202%2C%20section%204%20has%20the%20test%20that%20can.%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%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%600%60%20%7C%20A%20comparison%20with%20%60np.nan%60%2C%20with%20%60%3D%3D%60%20or%20%60is%60%3A%20never%20true%20row%20by%20row.%20%7C%20%60.isna()%60%20%7C%0A%20%20%20%20%7C%20One%20more%20than%20expected%20in%20the%20small%20table%20%7C%20The%20zero%20was%20counted%20too%3A%20%60fillna(0)%20%3D%3D%200%60%2C%20or%20%60%3D%3D%200%60.%20A%20plot%20given%200%20kg%20has%20fertilizer%20recorded.%20%7C%20count%20missing%20values%20only%20%7C%0A%20%20%20%20%7C%20The%20number%20of%20plots%20*with*%20fertilizer%20%7C%20%60.notna()%60%20where%20the%20task%20asks%20for%20the%20missing%20ones.%20%7C%20%60.isna()%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%20Drill%20D1.4%20%C2%B7%20An%20assignment%20that%20doesn't%20stick%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20sum%20of%20areas%20means%20something%20only%20if%20every%20area%20is%20in%20the%20same%20unit.%20This%20table%20will%20then%20say%20every%20plot%20is%20in%20hectares%3B%20if%20the%20numbers%20were%20not%20converted%2C%20it%20says%20something%20false%20about%20151%20plots.%0A%0A%20%20%20%20%60to_hectares(plots)%60%20should%20give%20a%20copy%20of%20the%20plots%20with%20every%20area%20in%20hectares%3A%20an%20area%20in%20square%20meters%20divided%20by%2010%2C000%2C%20and%20%60area_unit%60%20set%20to%20%60%22ha%22%60%20on%20every%20row.%20The%20table%20it%20is%20given%20stays%20as%20it%20was.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20plots%20table%20%E2%86%92%20a%20copy%2C%20every%20%60area%60%20in%20hectares%2C%20every%20%60area_unit%60%20%60%22ha%22%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60to_hectares(plots).loc%5B1%2C%20%22area%22%5D%60%20%7C%20%600.194%60%3A%20HH-0001's%20plot%202%2C%201%2C940%20square%20meters%20%7C%0A%20%20%20%20%7C%20%60to_hectares(plots)%5B%22area_unit%22%5D.unique().tolist()%60%20%7C%20%60%5B'ha'%5D%60%20%7C%0A%20%20%20%20%7C%20%60to_hectares(plots)%5B%22area%22%5D.max()%60%20%7C%20%604.008%60%20%7C%0A%0A%20%20%20%20Tools%3A%20changing%20values%20under%20copy-on-write%20(%5Bmodule%203%2C%20section%204%5D(%2F%3Ffile%3D20_dataframes.py)).%0A%0A%20%20%20%20**Predict.**%20HH-0001's%20second%20plot%20is%201%2C940%20square%20meters.%20After%20the%20function%2C%20what%20will%20its%20%60area%60%20and%20its%20%60area_unit%60%20be%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20to_hectares(plots%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20copy%20of%20the%20plots%20with%20every%20area%20in%20hectares.%22%22%22%0A%20%20%20%20table%20%3D%20plots.copy()%0A%20%20%20%20sqm%20%3D%20table%5B%22area_unit%22%5D%20%3D%3D%20%22sqm%22%0A%20%20%20%20%23%20Drill%20D1.4%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20table%5B%22area%22%5D%5Bsqm%5D%20%3D%20table%5B%22area%22%5D%5Bsqm%5D%20%2F%2010_000%0A%20%20%20%20table%5B%22area_unit%22%5D%20%3D%20%22ha%22%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%22Drill%20D1.4%22%2C%20to_hectares%2C%0A%20%20%20%20%20%20%20%20%5B(%22four%20plots%2C%20two%20in%20square%20meters%22%2C%20(drill_plots()%2C))%2C%20(%22the%20survey's%20plots%22%2C%20(course.plots(clean%3DFalse)%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20table%3A%20table%5B%5B%22hh_id%22%2C%20%22plot_no%22%2C%20%22area%22%2C%20%22area_unit%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Look%20at%20a%20plot%20that%20was%20in%20square%20meters%3A%20did%20its%20area%20change%3F%20Then%20look%20for%20a%20warning%20when%20the%20function%20runs.%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%20convert%20the%20survey's%20plots%2C%20and%20look%20at%20HH-0001's%20plot%202%2C%20the%20second%20row.%0A%20%20%20%20try_it(to_hectares%2C%20plots)%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**Trace.**%20HH-0001's%20plot%202%20still%20says%201%2C940%2C%20now%20in%20%60%22ha%22%60%3A%20a%20plot%20of%201%2C940%20hectares.%20No%20error%20stopped%20the%20function%2C%20but%20pandas%20did%20say%20something.%20A%20**warning**%20is%20a%20message%20that%20does%20not%20stop%20the%20code%3A%20the%20cell%20still%20shows%20its%20result%2C%20and%20the%20warning%20is%20printed%20apart%20from%20it%2C%20so%20it%20is%20easy%20to%20miss.%20The%20cell%20below%20runs%20the%20function%20again%20and%20catches%20every%20warning%20it%20gives%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%20%23%20Trace%3A%20run%20the%20function%20on%20the%20four%20small%20plots%2C%20and%20list%20the%20warnings%20pandas%20gives%20while%20it%20runs%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20_caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%20%20%23%20show%20every%20warning%2C%20even%20one%20already%20shown%0A%20%20%20%20%20%20%20%20attempt(to_hectares%2C%20drill_plots())%0A%20%20%20%20%5Bf%22%7Btype(w.message).__name__%7D%3A%20%7Bstr(w.message).splitlines()%5B0%5D%7D%22%20for%20w%20in%20_caught%5D%20or%20%22No%20warnings.%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%20Read%20the%20warning's%20name%20and%20first%20sentence%3A%20a%20value%20was%20set%20**on%20a%20copy**%2C%20through%20**chained%20assignment**%2C%20two%20pairs%20of%20square%20brackets%20one%20after%20the%20other.%20%60table%5B%22area%22%5D%60%20takes%20the%20column%20out%20of%20the%20table%2C%20and%20under%20copy-on-write%20what%20you%20take%20out%20behaves%20as%20a%20copy%3B%20%60%5Bsqm%5D%20%3D%20...%60%20then%20changes%20that%20copy.%20Module%203%2C%20section%204%20shows%20the%20one-step%20way%20to%20change%20some%20rows%20of%20a%20column.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.callout(mo.md(r%22%22%22%0A%20%20%20%20**If%20you%20know%20pandas%202.**%20In%20pandas%202%20this%20same%20line%20usually%20did%20change%20the%20table%2C%20with%20a%20%60SettingWithCopyWarning%60%20and%2C%20from%20pandas%202.2%2C%20a%20%60FutureWarning%60%20saying%20the%20behavior%20would%20change%20in%20pandas%203.0.%20In%20pandas%203%2C%20copy-on-write%20is%20always%20on%2C%20so%20a%20chained%20assignment%20**never**%20changes%20the%20table%3A%20the%20line%20only%20warns%2C%20with%20%60ChainedAssignmentError%60.%20Code%20that%20worked%20for%20years%20can%20now%20give%20back%20its%20input%20unchanged.%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22info%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%20Drill%20D1.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%20Areas%20of%20thousands%20of%20%60%22ha%22%60%20%7C%20The%20new%20values%20went%20into%20a%20copy%3A%20two%20pairs%20of%20brackets%20in%20a%20row%2C%20%60table%5B%22area%22%5D%5Bsqm%5D%20%3D%20...%60.%20%7C%20%60table.loc%5Brows%2C%20column%5D%20%3D%20...%60%20%7C%0A%20%20%20%20%7C%20Every%20area%20divided%20by%2010%2C000%2C%20hectares%20too%20%7C%20The%20division%20is%20on%20the%20whole%20column%2C%20not%20the%20square-meter%20rows.%20%7C%20%60sqm%60%20on%20both%20sides%20%7C%0A%20%20%20%20%7C%20Still%20thousands%20of%20%60%22ha%22%60%2C%20with%20%60.loc%60%20in%20the%20line%20%7C%20%60.loc%60%20picks%20the%20rows%2C%20then%20a%20second%20pair%20of%20brackets%20the%20column%3A%20%60table.loc%5Bsqm%5D%5B%22area%22%5D%20%3D%20...%60%20is%20still%20a%20chained%20assignment.%20%7C%20the%20rows%20and%20the%20column%20inside%20one%20%60.loc%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%23%20Drill%20D1.5%20%C2%B7%20Numbers%20read%20as%20text%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20sorting%2C%20summing%20and%20averaging%20only%20work%20on%20numbers.%20A%20column%20of%20text%20sorts%20and%20adds%20as%20text%2C%20without%20an%20error.%0A%0A%20%20%20%20%60plots.csv%60%20writes%20large%20harvests%20with%20a%20thousands%20separator%3A%20%60%221%2C640%22%60.%20%60largest_harvests(path%2C%20n)%60%20should%20read%20the%20plots%20file%20and%20give%20the%20%60n%60%20largest%20harvests%2C%20in%20kilograms%2C%20largest%20first%2C%20as%20a%20list%20of%20whole%20numbers.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20path%20of%20%60plots.csv%60%20and%20a%20count%20%E2%86%92%20a%20list%20of%20the%20%60n%60%20largest%20values%20of%20%60production_kg%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60largest_harvests(PLOTS_CSV%2C%203)%60%20%7C%20%60%5B84190%2C%2070780%2C%2059090%5D%60%20%7C%0A%0A%20%20%20%20Tools%3A%20reading%20with%20the%20right%20types%20(%5Bmodule%203%2C%20section%202%5D(%2F%3Ffile%3D20_dataframes.py)).%0A%0A%20%20%20%20**Predict.**%20The%20file's%20first%20plot%20harvested%20%60%221%2C640%22%60%20kg.%20Will%20the%20first%20harvest%20the%20function%20gives%20be%20bigger%20or%20smaller%20than%20that%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20largest_harvests(path%2C%20n%3A%20int)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22The%20n%20largest%20harvests%20in%20the%20plots%20file%2C%20in%20kilograms%2C%20largest%20first.%22%22%22%0A%20%20%20%20%23%20Drill%20D1.5%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20table%20%3D%20pd.read_csv(path)%0A%20%20%20%20return%20table%5B%22production_kg%22%5D.sort_values(ascending%3DFalse).head(n).tolist()%0A%0A%0A%40app.function%0Adef%20drill_plots_csv()%20-%3E%20io.StringIO%3A%0A%20%20%20%20%22%22%22A%20small%20plots%20file%2C%20as%20text%2C%20with%20harvests%20written%20with%20and%20without%20a%20thousands%20separator.%22%22%22%0A%20%20%20%20return%20io.StringIO(%0A%20%20%20%20%20%20%20%20%22hh_id%2Cplot_no%2Ccrop%2Carea%2Carea_unit%2Cirrigated%2Cproduction_kg%2Cfertilizer_kg%5Cn%22%0A%20%20%20%20%20%20%20%20'HH-0001%2C1%2CRice%2C0.439%2Cha%2CNo%2C%221%2C640%22%2C89%5Cn'%0A%20%20%20%20%20%20%20%20%22HH-0001%2C2%2CRice%2C1940%2Csqm%2CYes%2C700%2C%5Cn%22%0A%20%20%20%20%20%20%20%20'HH-0002%2C1%2CBanana%2C2.1%2Cha%2CNo%2C%2212%2C500%22%2C40.5%5Cn'%0A%20%20%20%20)%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%22Drill%20D1.5%22%2C%20largest_harvests%2C%0A%20%20%20%20%20%20%20%20%5B(%22plots.csv%2C%20the%203%20largest%22%2C%20(PLOTS_CSV%2C%203))%2C%20(%22plots.csv%2C%20the%20largest%22%2C%20(PLOTS_CSV%2C%201))%2C%0A%20%20%20%20%20%20%20%20%20(%22a%20file%20with%20harvests%20of%201%2C640%2C%20700%20and%2012%2C500%20kg%22%2C%20(drill_plots_csv()%2C%203))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22calls%22%2C%20hint%3D%22Look%20at%20what%20comes%20back%3A%20numbers%2C%20or%20texts%20in%20quotes%3F%20In%20what%20order%3F%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%203%20largest%20harvests%20in%20the%20plots%20file.%0A%20%20%20%20try_it(largest_harvests%2C%20PLOTS_CSV%2C%203)%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**Trace.**%20The%20answer%20is%20in%20quotes%3A%20texts%2C%20not%20numbers.%20Look%20at%20the%20column's%20type%20as%20%60read_csv%60%20reads%20it%20by%20itself%2C%20and%20at%20what%20adding%20three%20of%20its%20values%20gives%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%20%23%20Trace%3A%20the%20column's%20type%2C%20and%20the%20sum%20of%20its%20first%20three%20values%0A%20%20%20%20_production%20%3D%20pd.read_csv(PLOTS_CSV)%5B%22production_kg%22%5D%0A%20%20%20%20_production.dtype%2C%20_production.head(3).tolist()%2C%20_production.head(3).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%20type%20is%20%60str%60%2C%20text.%20Adding%20texts%20joins%20them%3A%20%60%221%2C640%22%20%2B%20%22700%22%20%2B%20%221%2C990%22%60%20is%20%60%221%2C6407001%2C990%22%60.%20Sorting%20texts%20goes%20letter%20by%20letter%2C%20so%20every%20harvest%20that%20starts%20with%20a%209%20comes%20first.%20The%20comma%20made%20%60read_csv%60%20give%20up%20on%20numbers.%20Module%203%2C%20section%202%20shows%20how%20to%20tell%20it%20what%20the%20comma%20means.%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%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.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%60%5B'990'%2C%20'990'%2C%20'990'%5D%60%20%7C%20The%20column%20is%20text%2C%20sorted%20letter%20by%20letter.%20%7C%20%60read_csv%60's%20%60thousands%3D%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20invalid%20literal%20for%20int()*%20%7C%20%60.astype(int)%60%20on%20texts%20that%20still%20have%20their%20commas.%20%7C%20remove%20the%20commas%20first%2C%20or%20read%20them%20right%20%7C%0A%20%20%20%20%7C%20The%20smallest%20harvests%20%7C%20%60ascending%3DFalse%60%20was%20lost.%20%7C%20%60sort_values%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%20Drill%20D1.6%20%C2%B7%20Rows%20that%20no%20longer%20line%20up%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20report%20shows%20labels%2C%20not%20codes.%20A%20label%20put%20on%20the%20wrong%20household%20is%20worse%20than%20no%20label%3A%20nothing%20about%20it%20looks%20wrong.%0A%0A%20%20%20%20%60head_sex%60%20is%20a%20code%3A%20%601%60%20male%2C%20%602%60%20female%2C%20%609%60%20not%20stated.%20%60with_sex_label(households)%60%20should%20give%20the%20households%20it%20is%20given%20with%20a%20new%20column%2C%20%60head_sex_label%60%2C%20holding%20%60%22male%22%60%2C%20%60%22female%22%60%20or%20%60%22not%20stated%22%60%2C%20each%20on%20its%20own%20household's%20row.%20It%20is%20used%20on%20any%20part%20of%20the%20table%3A%20all%20the%20households%2C%20one%20province's%2C%20or%20the%20households%20in%20another%20order.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20households%20table%2C%20or%20part%20of%20one%20%E2%86%92%20the%20same%20rows%2C%20with%20%60head_sex_label%60%20beside%20%60head_sex%60.%0A%0A%20%20%20%20%7C%20call%20%7C%20should%20give%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60with_sex_label(households)%5B%22head_sex_label%22%5D.value_counts().to_dict()%60%20%7C%20%60%7B'male'%3A%20927%2C%20'female'%3A%20245%2C%20'not%20stated'%3A%2024%7D%60%20%7C%0A%20%20%20%20%7C%20%60with_sex_label(households%5Bhouseholds%5B%22province%22%5D%20%3D%3D%20%22Isabela%22%5D)%5B%22head_sex_label%22%5D.isna().sum()%60%20%7C%20%600%60%3A%20every%20household%20labeled%20%7C%0A%0A%20%20%20%20Tools%3A%20the%20index%20and%20alignment%20(%5Bmodule%203%2C%20section%201%5D(%2F%3Ffile%3D20_dataframes.py)).%0A%0A%20%20%20%20**Predict.**%20Run%20**Try%20it**%20below%2C%20on%20all%20the%20households.%20Does%20each%20label%20match%20its%20code%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20with_sex_label(households%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20households%2C%20each%20with%20its%20head's%20sex%20as%20a%20label%20beside%20the%20code.%22%22%22%0A%20%20%20%20%23%20Drill%20D1.6%3A%20the%20task%20is%20in%20the%20text%20above%20this%20cell.%20Find%20the%20mistake%20and%20fix%20it.%0A%20%20%20%20labels%20%3D%20pd.Series(%5B%22male%22%20if%20code%20%3D%3D%201%20else%20%22female%22%20if%20code%20%3D%3D%202%20else%20%22not%20stated%22%20for%20code%20in%20households%5B%22head_sex%22%5D%5D)%0A%20%20%20%20return%20households.assign(head_sex_label%3Dlabels)%0A%0A%0A%40app.function%0Adef%20drill_heads()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Three%20households%20whose%20index%20labels%20are%20not%200%2C%201%2C%202%20in%20order%2C%20as%20a%20table's%20rows%20are%20after%20sorting.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22hh_id%22%3A%20%5B%22HH-0003%22%2C%20%22HH-0001%22%2C%20%22HH-0002%22%5D%2C%20%22head_sex%22%3A%20%5B9%2C%201%2C%202%5D%7D%2C%20index%3D%5B2%2C%200%2C%201%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%22Drill%20D1.6%22%2C%20with_sex_label%2C%0A%20%20%20%20%20%20%20%20%5B(%22all%20the%20households%22%2C%20(households%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22Isabela's%20households%22%2C%20(households%5Bhouseholds%5B%22province%22%5D%20%3D%3D%20%22Isabela%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22the%20largest%20households%20first%22%2C%20(households.sort_values(%22hh_size%22%2C%20ascending%3DFalse%2C%20kind%3D%22stable%22)%2C))%2C%0A%20%20%20%20%20%20%20%20%20(%22three%20households%20with%20the%20index%202%2C%200%2C%201%22%2C%20(drill_heads()%2C))%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20table%3A%20table%5B%5B%22hh_id%22%2C%20%22head_sex%22%2C%20%22head_sex_label%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20check%20names%20the%20first%20part%20of%20the%20table%20that%20goes%20wrong.%20Look%20at%20that%20part's%20labels%20beside%20its%20codes%2C%20and%20at%20its%20index.%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%20label%20all%20the%20households%2C%20and%20compare%20each%20label%20with%20its%20code.%0A%20%20%20%20try_it(with_sex_label%2C%20households)%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**It%20looks%20right.**%20On%20all%201%2C196%20households%20every%20label%20matches%20its%20code%3A%20no%20error%2C%20nothing%20to%20trace.%20Its%20check%20is%20red%20all%20the%20same.%20Pick%20the%20part%20of%20the%20table%20the%20check%20names%2C%20and%20look%20at%20the%20labels%20the%20function%20gives%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20part%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%5B%22all%20the%20households%22%2C%20%22Isabela's%20households%22%2C%20%22the%20largest%20households%20first%22%5D%2C%0A%20%20%20%20%20%20%20%20value%3D%22Isabela's%20households%22%2C%20label%3D%22Which%20households%3F%22%2C%0A%20%20%20%20)%0A%20%20%20%20part%0A%20%20%20%20return%20(part%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(part)%3A%0A%20%20%20%20_rows%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22all%20the%20households%22%3A%20households%2C%0A%20%20%20%20%20%20%20%20%22Isabela's%20households%22%3A%20households%5Bhouseholds%5B%22province%22%5D%20%3D%3D%20%22Isabela%22%5D%2C%0A%20%20%20%20%20%20%20%20%22the%20largest%20households%20first%22%3A%20households.sort_values(%22hh_size%22%2C%20ascending%3DFalse%2C%20kind%3D%22stable%22)%2C%0A%20%20%20%20%7D%5Bpart.value%5D%0A%20%20%20%20_labeled%20%3D%20attempt(with_sex_label%2C%20_rows)%0A%20%20%20%20_expected%20%3D%20_rows%5B%22head_sex%22%5D.map(%7B1%3A%20%22male%22%2C%202%3A%20%22female%22%2C%209%3A%20%22not%20stated%22%7D)%0A%20%20%20%20_wrong%20%3D%20(_labeled%5B%22head_sex_label%22%5D%20!%3D%20_expected)%20%26%20_labeled%5B%22head_sex_label%22%5D.notna()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Bpart.value%7D**%3A%20%7Blen(_rows)%3A%2C%7D%20rows.%20Labels%20missing%20(%60NaN%60)%3A%20**%7B_labeled%5B'head_sex_label'%5D.isna().sum()%3A%2C%7D**.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22Labels%20that%20don't%20match%20their%20code%3A%20**%7B_wrong.sum()%3A%2C%7D**.%20The%20first%20rows%2C%20with%20their%20index%20on%20the%20left%3A%22)%2C%0A%20%20%20%20%20%20%20%20_labeled%5B%5B%22hh_id%22%2C%20%22head_sex%22%2C%20%22head_sex_label%22%5D%5D.head(8)%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**Trace.**%20Look%20at%20the%20index%20on%20the%20left%20of%20each%20table%2C%20then%20at%20the%20index%20of%20the%20%60labels%60%20Series%20the%20function%20makes%3A%20%60pd.Series(%5B...%5D)%60%20numbers%20its%20values%200%2C%201%2C%202%2C%20%E2%80%A6%20.%20%60assign%60%20puts%20a%20Series%20into%20a%20table%20by%20matching%20**index%20labels**%2C%20as%20module%203%2C%20section%201%20showed%20for%20arithmetic.%20Isabela's%20rows%20are%20labeled%20170%20to%20339%2C%20so%20the%20labels%20numbered%200%20to%20169%20find%20no%20row%2C%20and%20every%20row%20gets%20%60NaN%60.%20Sorted%20rows%20have%20every%20label%2C%20in%20another%20order%2C%20so%20each%20household%20gets%20the%20label%20made%20for%20another%20one.%20The%20whole%20table%20is%20the%20one%20case%20where%20the%20two%20indexes%20agree%2C%20by%20luck.%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%20the%20check%20is%20red%3A%20common%20causes%20for%20Drill%20D1.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%60NaN%60%20labels%20on%20one%20province's%20rows%20%7C%20The%20new%20Series%20is%20numbered%200%2C%201%2C%202%2C%20%E2%80%A6%2C%20and%20those%20numbers%20are%20not%20the%20rows'%20index%20labels.%20%7C%20%60pd.Series(...%2C%20index%3D...)%60%2C%20or%20%60.map%60%20on%20the%20column%20%7C%0A%20%20%20%20%7C%20Labels%20on%20the%20wrong%20households%20after%20sorting%20%7C%20The%20same%3A%20the%20values%20line%20up%20by%20position%2C%20but%20pandas%20lines%20them%20up%20by%20index%20label.%20%7C%20the%20index%20of%20the%20Series%20you%20assign%20%7C%0A%20%20%20%20%7C%20%60.reset_index(drop%3DTrue)%60%20makes%20the%20labels%20right%20but%20the%20check%20is%20still%20red%20%7C%20The%20table's%20own%20index%20was%20changed.%20The%20task%20keeps%20the%20rows%20as%20given.%20%7C%20give%20the%20labels%20the%20table's%20index%20instead%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.callout(mo.md(r%22%22%22%0A%20%20%20%20**What%20the%20six%20mistakes%20have%20in%20common.**%20Only%20one%20stopped%20with%20an%20error%2C%20and%20its%20last%20line%20said%20what%20was%20wrong.%20One%20more%20gave%20a%20warning%2C%20which%20nothing%20forced%20anyone%20to%20read%2C%20and%20handed%20back%20square%20meters%20labeled%20hectares.%20The%20other%20four%20ran%20without%20a%20word%20and%20gave%20back%20a%20table%20or%20a%20number%20that%20looked%20like%20an%20answer%3A%20codes%20missing%20a%20digit%2C%20a%20count%20of%20zero%2C%20harvests%20sorted%20as%20text%2C%20labels%20on%20the%20wrong%20households.%20Two%20came%20from%20the%20way%20a%20file%20was%20read%2C%20before%20any%20analysis%20began%3A%20that%20is%20why%20module%203%20reads%20every%20column%20with%20the%20type%20it%20needs%20and%20checks%20%60dtypes%60%20straight%20after.%20The%20last%20was%20right%20on%20the%20whole%20table%20and%20wrong%20on%20every%20part%20of%20it%2C%20which%20only%20a%20check%20on%20more%20than%20one%20case%20could%20show.%0A%0A%20%20%20%20Next%3A%20module%204%2C%20**Summarize%20and%20combine**%2C%20in%20%5B30_summarize_and_combine.py%5D(%2F%3Ffile%3D30_summarize_and_combine.py).%0A%20%20%20%20%22%22%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
dbf8e68aa627062973c8ba92d69160d4