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%22Primers%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20The%20explainers'%20drawings%20come%20from%20explain.py%2C%20shared%20with%20the%20lesson%20notebooks.%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20import%20explain%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%20Primers%0A%0A%20%20%20%20Short%20explanations%20of%20the%20ideas%20the%20course%20uses%2C%20each%20with%20an%20explainer%20to%20play%20with.%20The%20lesson%20notebooks%20show%20a%20short%20version%20where%20you%20first%20need%20an%20idea%2C%20and%20link%20here%20for%20the%20full%20one.%20Come%20back%20to%20any%20of%20them%20at%20any%20time%3B%20nothing%20here%20is%20graded.%0A%0A%20%20%20%20%7C%20%23%20%7C%20Primer%20%7C%20You%20first%20need%20it%20in%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%7C%20Doing%20something%20to%20every%20value%3A%20%60.map%60%20and%20%60lambda%60%20%7C%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%2C%20%C2%A71%20%7C%0A%20%20%20%20%7C%202%20%7C%20Keeping%20rows%3A%20conditions%20%7C%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%2C%20Exercise%203.1%20%7C%0A%20%20%20%20%7C%203%20%7C%20Reading%20a%20chain%20of%20methods%20%7C%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%2C%20Exercise%203.2%20%7C%0A%20%20%20%20%7C%204%20%7C%20SQL%20in%20five%20minutes%20%7C%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%2C%20%C2%A72%20(*Look%20before%20you%20move%20on*)%20%7C%0A%20%20%20%20%7C%205%20%7C%20Grouping%3A%20one%20row%20per%20group%20%7C%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%2C%20Exercise%203.3%20%7C%0A%20%20%20%20%7C%206%20%7C%20Joins%3A%20left%2C%20inner%2C%20and%20the%20anti-join%20%7C%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%2C%20%C2%A73%20%7C%0A%20%20%20%20%7C%207%20%7C%20What%20a%20survey%20weight%20is%20%7C%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%2C%20%C2%A74%20%7C%0A%20%20%20%20%7C%208%20%7C%20JSON%3A%20dictionaries%20and%20lists%20inside%20each%20other%20%7C%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%2C%20Exercise%204.3%20%7C%0A%20%20%20%20%7C%209%20%7C%20Percent%20difference%20and%20tolerance%20%7C%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%2C%20Exercise%204.4%20%7C%0A%20%20%20%20%7C%2010%20%7C%20Reading%20an%20error%20%7C%20every%20notebook%20%7C%0A%20%20%20%20%7C%2011%20%7C%20Rules%20with%20pandera%20%7C%20%5B20_check_round.py%5D(%2F%3Ffile%3D20_check_round.py)%2C%20%C2%A72%20%7C%0A%20%20%20%20%7C%2012%20%7C%20Database%20constraints%20%7C%20%5B22_store_round.py%5D(%2F%3Ffile%3D22_store_round.py)%2C%20%C2%A71%20%7C%0A%20%20%20%20%7C%2013%20%7C%20Transactions%20%7C%20%5B22_store_round.py%5D(%2F%3Ffile%3D22_store_round.py)%2C%20%C2%A73%20%7C%0A%20%20%20%20%7C%2014%20%7C%20Crosswalks%20between%20editions%20%7C%20%5B30_new_edition.py%5D(%2F%3Ffile%3D30_new_edition.py)%2C%20%C2%A73%20%7C%0A%20%20%20%20%7C%2015%20%7C%20Small-cell%20suppression%20%7C%20%5B31_share_round.py%5D(%2F%3Ffile%3D31_share_round.py)%2C%20%C2%A73%20%7C%0A%20%20%20%20%7C%2016%20%7C%20File%20formats%3A%20what%20survives%20the%20round%20trip%20%7C%20%5B31_share_round.py%5D(%2F%3Ffile%3D31_share_round.py)%2C%20%C2%A71%20and%20%C2%A75%20%7C%0A%20%20%20%20%7C%2017%20%7C%20Datasette%20addresses%20and%20APIs%20%7C%20%5B32_datasette.py%5D(%2F%3Ffile%3D32_datasette.py)%2C%20%C2%A74%20and%20%C2%A75%20%7C%0A%0A%20%20%20%20The%20basics%20(values%2C%20variables%2C%20comments%2C%20functions%2C%20%60if%60%2C%20loops%2C%20slices)%20are%20in%20%5B00_start.py%5D(%2F%3Ffile%3D00_start.py)%2C%20sections%203%20and%204.%20Words%20you%20do%20not%20know%3A%20%5B91_glossary.py%5D(%2F%3Ffile%3D91_glossary.py).%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%20Doing%20something%20to%20every%20value%3A%20%60.map%60%20and%20%60lambda%60%0A%0A%20%20%20%20A%20column%20of%20a%20table%20(a%20**Series**)%20holds%20many%20values.%20Often%20you%20want%20to%20do%20the%20same%20thing%20to%20each%20one%3A%20pad%20every%20code%2C%20remove%20the%20spaces%20around%20every%20name.%20%60.map(f)%60%20does%20exactly%20that%3A%20it%20calls%20the%20function%20%60f%60%20once%20for%20**each**%20value%2C%20and%20collects%20the%20answers%20into%20a%20**new**%20column%20of%20the%20same%20length.%0A%0A%20%20%20%20The%20function%20is%20often%20written%20right%20there%2C%20as%20a%20**lambda**%3A%20a%20small%20function%20without%20a%20name.%20%60lambda%20v%3A%20v.zfill(10)%60%20means%20*%22take%20a%20value%2C%20call%20it%20%60v%60%2C%20and%20give%20back%20%60v.zfill(10)%60%22*.%20It%20is%20the%20same%20as%20writing%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20def%20pad(v)%3A%0A%20%20%20%20%20%20%20%20return%20v.zfill(10)%0A%0A%20%20%20%20codes.map(pad)%20%20%20%20%20%23%20the%20same%20as%20codes.map(lambda%20v%3A%20v.zfill(10))%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Pick%20what%20to%20do%20to%20every%20value%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%20map_choice%20%3D%20mo.ui.dropdown(options%3Dlist(explain.MAP_CHOICES)%2C%20value%3D%22pad%20to%2010%20characters%22%2C%20label%3D%22For%20every%20value%3A%22)%0A%20%20%20%20map_choice%0A%20%20%20%20return%20(map_choice%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(map_choice)%3A%0A%20%20%20%20explain.map_demo(map_choice.value)%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**Look%20at%20the%20row%20%60'%200200207000%20'%60**%3A%20padding%20it%20does%20nothing%20useful%2C%20because%20its%20spaces%20already%20make%20it%2012%20characters%20long.%20Remove%20the%20spaces%20first%2C%20then%20pad.%20The%20order%20of%20steps%20matters%2C%20which%20is%20why%20Exercise%201.1%20lists%20them%20in%20order.%0A%0A%20%20%20%20**In%20SQL**%20a%20function%20in%20a%20query%20applies%20to%20every%20row%20by%20itself%3A%20%60SELECT%20lpad(code%2C%2010%2C%20'0')%20FROM%20codes%60.%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Forgetting%20to%20keep%20the%20result.*%20%60codes.map(...)%60%20gives%20back%20a%20**new**%20column%3B%20%60codes%60%20itself%20does%20not%20change.%20Write%20%60codes%20%3D%20codes.map(...)%60%2C%20or%20%60table%5B%22code%22%5D%20%3D%20table%5B%22code%22%5D.map(...)%60.%0A%20%20%20%20-%20*Calling%20the%20function%20instead%20of%20handing%20it%20over.*%20%60codes.map(pad)%60%20hands%20%60pad%60%20to%20%60.map%60%2C%20which%20calls%20it%20for%20each%20value%3B%20%60codes.map(pad())%60%20calls%20it%20once%2C%20with%20nothing%2C%20and%20fails.%0A%20%20%20%20-%20*A%20lambda%20that%20uses%20the%20wrong%20name.*%20In%20%60lambda%20v%3A%20code.zfill(10)%60%2C%20%60code%60%20is%20not%20the%20value%3A%20the%20value%20is%20%60v%60.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%3A%20after%20Exercise%201.1%2C%20in%20%60read_round%60%2C%20and%20in%20Exercise%203.1%20(step%204).%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%202%20%C2%B7%20Keeping%20rows%3A%20conditions%0A%0A%20%20%20%20A%20**condition**%20asks%20a%20yes%2Fno%20question%20of%20every%20row%20at%20once%2C%20and%20gives%20one%20%60True%60%20or%20%60False%60%20per%20row.%20Putting%20it%20in%20square%20brackets%2C%20%60table%5Bcondition%5D%60%2C%20keeps%20the%20rows%20where%20the%20answer%20is%20%60True%60.%0A%0A%20%20%20%20-%20Compare%20with%20%60%3D%3D%60%20(equal)%2C%20%60!%3D%60%20(not%20equal)%2C%20%60%3C%60%2C%20%60%3C%3D%60%2C%20%60%3E%60%2C%20%60%3E%3D%60.%20One%20%60%3D%60%20gives%20a%20name%20to%20a%20value%3B%20two%20%60%3D%3D%60%20compare.%0A%20%20%20%20-%20Text%20columns%20have%20text%20methods%20under%20%60.str%60%3A%20%60.str.startswith(%2201%22)%60%2C%20%60.str.len()%60%2C%20%60.str.fullmatch(r%22%5Cd%2B%22)%60.%20%60%5Cd%2B%60%20is%20a%20**pattern**%20meaning%20*%22one%20or%20more%20digits%2C%20and%20nothing%20else%22*.%0A%20%20%20%20-%20A%20missing%20cell%20gives%20a%20missing%20answer%2C%20not%20%60False%60.%20%60.fillna(False)%60%20turns%20those%20into%20%60False%60%2C%20so%20the%20row%20is%20dropped.%0A%20%20%20%20-%20Combine%20conditions%20with%20%60%26%60%20(and)%2C%20%60%7C%60%20(or)%2C%20and%20%60~%60%20(not).%20Put%20each%20condition%20in%20its%20own%20brackets%3A%20%60(a%20%3D%3D%201)%20%26%20(b%20%3D%3D%202)%60.%0A%0A%20%20%20%20A%20few%20rows%20of%20the%20code%20list%20workbook%2C%20as%20they%20come%20out%20of%20Excel%20(with%20the%20empty%20row%20and%20the%20note%20under%20the%20table).%20Pick%20a%20condition%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%20filter_choice%20%3D%20mo.ui.dropdown(options%3Dlist(explain.FILTER_CHOICES)%2C%20value%3D%22the%20code%20is%20made%20of%20digits%22%2C%20label%3D%22Keep%20the%20rows%20where%3A%22)%0A%20%20%20%20filter_choice%0A%20%20%20%20return%20(filter_choice%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(filter_choice)%3A%0A%20%20%20%20explain.filter_demo(filter_choice.value)%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**In%20SQL**%20the%20condition%20goes%20in%20%60WHERE%60%3A%20%60SELECT%20*%20FROM%20codes%20WHERE%20level%20%3D%20'City'%60.%20SQL%20uses%20one%20%60%3D%60%20to%20compare%2C%20%60AND%60%2C%20%60OR%60%20and%20%60NOT%60%20as%20words%2C%20and%20single%20quotes%20around%20text.%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*%60and%60%2F%60or%60%20instead%20of%20%60%26%60%2F%60%7C%60.*%20Python's%20words%20work%20on%20one%20yes%2Fno%2C%20not%20on%20a%20whole%20column%2C%20and%20pandas%20says%3A%20*%22The%20truth%20value%20of%20a%20Series%20is%20ambiguous%22*.%20Use%20%60%26%60%20and%20%60%7C%60.%0A%20%20%20%20-%20*No%20brackets%20around%20each%20part.*%20%60table%5B%22level%22%5D%20%3D%3D%20%22City%22%20%26%20...%60%20is%20read%20in%20the%20wrong%20order.%20Write%20%60(table%5B%22level%22%5D%20%3D%3D%20%22City%22)%20%26%20(...)%60.%0A%20%20%20%20-%20*%60%3D%60%20instead%20of%20%60%3D%3D%60*%20inside%20a%20condition%3A%20Python%20stops%20with%20a%20%60SyntaxError%60.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%3A%20Exercise%203.1%20(step%203)%20and%20Exercise%203.3%20(step%201).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20Reading%20a%20chain%20of%20methods%0A%0A%20%20%20%20pandas%20methods%20give%20back%20a%20**new**%20table.%20So%20you%20can%20call%20the%20next%20method%20on%20what%20the%20previous%20one%20gave%20back%2C%20and%20write%20several%20steps%20as%20one%20**chain**%2C%20one%20step%20per%20line%20inside%20round%20brackets.%20Read%20it%20from%20top%20to%20bottom%3A%20each%20line%20works%20on%20the%20table%20the%20line%20above%20produced.%0A%0A%20%20%20%20Move%20the%20slider%20to%20run%20the%20chain%20one%20step%20at%20a%20time%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%20chain_step%20%3D%20mo.ui.slider(0%2C%203%2C%20value%3D0%2C%20label%3D%22steps%20run%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20chain_step%0A%20%20%20%20return%20(chain_step%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(chain_step)%3A%0A%20%20%20%20explain.chain_demo(chain_step.value)%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%20same%20steps%20can%20be%20written%20one%20at%20a%20time%2C%20each%20with%20its%20own%20name.%20It%20is%20longer%2C%20but%20you%20can%20look%20at%20every%20step%2C%20which%20is%20the%20best%20way%20to%20find%20a%20mistake%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20step1%20%3D%20code_list.drop_duplicates(subset%3D%22code%22%2C%20keep%3D%22first%22)%0A%20%20%20%20step2%20%3D%20step1%5B%5B%22code%22%2C%20%22name%22%2C%20%22level%22%5D%5D%0A%20%20%20%20keyed%20%3D%20step2.rename(columns%3D%7B%22code%22%3A%20%22area_code%22%2C%20%22name%22%3A%20%22area_name%22%2C%20%22level%22%3A%20%22area_level%22%7D)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Expecting%20a%20method%20to%20change%20the%20table.*%20%60code_list.rename(...)%60%20gives%20back%20a%20renamed%20**copy**%3B%20%60code_list%60%20keeps%20its%20old%20names.%20Keep%20the%20result%20in%20a%20name.%0A%20%20%20%20-%20*A%20step%20in%20the%20wrong%20order.*%20%60%5B%5B%22code%22%2C%20%22name%22%2C%20%22level%22%5D%5D%60%20after%20%60.rename(...)%60%20fails%20with%20a%20%60KeyError%60%2C%20because%20the%20columns%20no%20longer%20have%20those%20names.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%3A%20Exercises%203.2%20and%204.1.%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%204%20%C2%B7%20SQL%20in%20five%20minutes%0A%0A%20%20%20%20**SQL**%20is%20the%20language%20of%20databases.%20A%20query%20says%20*what*%20you%20want%2C%20and%20the%20database%20works%20out%20how%3A%0A%0A%20%20%20%20%60%60%60sql%0A%20%20%20%20SELECT%20columns%20%20%20%20%20%20%20%20%20%20%20%20--%20which%20columns%2C%20or%20calculations%20on%20them%0A%20%20%20%20FROM%20table%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20--%20from%20which%20table%0A%20%20%20%20WHERE%20condition%20%20%20%20%20%20%20%20%20%20%20--%20only%20the%20rows%20where%20this%20is%20true%20(optional)%0A%20%20%20%20GROUP%20BY%20column%20%20%20%20%20%20%20%20%20%20%20--%20one%20row%20per%20group%20(optional%3B%20see%20primer%205)%0A%20%20%20%20ORDER%20BY%20column%20%20%20%20%20%20%20%20%20%20%20--%20sorted%20(optional)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20-%20%60AS%60%20gives%20a%20result%20a%20name%3A%20%60count(*)%20AS%20persons%60.%0A%20%20%20%20-%20%60count(*)%60%20counts%20rows%3B%20%60sum(weight)%60%20adds%20up%3B%20%60round(x)%60%20rounds.%0A%20%20%20%20-%20Text%20goes%20in%20**single**%20quotes%3A%20%60WHERE%20region%20%3D%20'01'%60.%20%60--%60%20starts%20a%20comment.%0A%0A%20%20%20%20In%20this%20course%20SQL%20runs%20in%20**DuckDB**%2C%20inside%20Python.%20A%20**SQL%20cell**%20in%20marimo%20can%20query%20any%20table%20in%20the%20notebook%20by%20its%20name.%20In%20a%20function%2C%20%60con.sql(%22SELECT%20...%22).df()%60%20runs%20a%20query%20and%20gives%20back%20a%20pandas%20table.%0A%0A%20%20%20%20Build%20a%20query%20on%20a%20small%20table%20of%20people%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%20sql_columns%20%3D%20mo.ui.multiselect(options%3D%5B%22hhid%22%2C%20%22region%22%2C%20%22age%22%2C%20%22weight%22%5D%2C%20value%3D%5B%22hhid%22%2C%20%22age%22%5D%2C%20label%3D%22SELECT%22)%0A%20%20%20%20sql_adults%20%3D%20mo.ui.checkbox(label%3D%22WHERE%20age%20%3E%3D%2015%22%2C%20value%3DFalse)%0A%20%20%20%20sql_group%20%3D%20mo.ui.checkbox(label%3D%22GROUP%20BY%20region%20(counts%20and%20sums)%22%2C%20value%3DFalse)%0A%20%20%20%20mo.hstack(%5Bsql_columns%2C%20sql_adults%2C%20sql_group%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20sql_adults%2C%20sql_columns%2C%20sql_group%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(sql_adults%2C%20sql_columns%2C%20sql_group)%3A%0A%20%20%20%20explain.sql_demo(sql_columns.value%2C%20sql_adults.value%2C%20sql_group.value)%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%20same%20ideas%20in%20pandas%20and%20in%20SQL**%0A%0A%20%20%20%20%7C%20to%E2%80%A6%20%7C%20pandas%20%7C%20SQL%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20keep%20some%20columns%20%7C%20%60t%5B%5B%22a%22%2C%20%22b%22%5D%5D%60%20%7C%20%60SELECT%20a%2C%20b%20FROM%20t%60%20%7C%0A%20%20%20%20%7C%20keep%20some%20rows%20%7C%20%60t%5Bt%5B%22age%22%5D%20%3E%3D%2015%5D%60%20%7C%20%60...%20WHERE%20age%20%3E%3D%2015%60%20%7C%0A%20%20%20%20%7C%20rename%20%7C%20%60t.rename(columns%3D%7B%22a%22%3A%20%22x%22%7D)%60%20%7C%20%60SELECT%20a%20AS%20x%60%20%7C%0A%20%20%20%20%7C%20one%20row%20per%20group%20%7C%20%60t.groupby(%22g%22).agg(n%3D(%22a%22%2C%20%22size%22))%60%20%7C%20%60SELECT%20g%2C%20count(*)%20AS%20n%20...%20GROUP%20BY%20g%60%20%7C%0A%20%20%20%20%7C%20sort%20%7C%20%60t.sort_values(%22a%22)%60%20%7C%20%60...%20ORDER%20BY%20a%60%20%7C%0A%20%20%20%20%7C%20join%20%7C%20%60t.merge(u%2C%20on%3D%22k%22%2C%20how%3D%22left%22)%60%20%7C%20%60...%20FROM%20t%20LEFT%20JOIN%20u%20ON%20t.k%20%3D%20u.k%60%20%7C%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Double%20quotes%20around%20text.*%20In%20SQL%2C%20%60%22region%22%60%20names%20a%20**column**%3B%20%60'01'%60%20is%20a%20text%20value.%0A%20%20%20%20-%20*%60%3D%3D%60%20instead%20of%20%60%3D%60.*%20SQL%20compares%20with%20one%20%60%3D%60.%0A%20%20%20%20-%20*Comparing%20text%20with%20a%20number.*%20DuckDB%20refuses%20%60'01'%20%3D%201%60%3B%20turn%20one%20into%20the%20other%20first%20(%60CAST(region_code%20AS%20VARCHAR)%60%2C%20then%20%60lpad(...%2C%202%2C%20'0')%60).%0A%20%20%20%20-%20*Another%20column%20next%20to%20%60count(*)%60%20without%20%60GROUP%20BY%60.*%20Every%20column%20in%20%60SELECT%60%20must%20either%20be%20in%20%60GROUP%20BY%60%20or%20be%20inside%20a%20calculation%20such%20as%20%60count%60%20or%20%60sum%60.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%3A%20%60SUMMARIZE%60%20in%20%C2%A72%2C%20the%20anti-join%20in%20%C2%A73%2C%20and%20Exercise%203.4.%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%205%20%C2%B7%20Grouping%3A%20one%20row%20per%20group%0A%0A%20%20%20%20*How%20many%20people%20per%20area%20code%3F%20How%20many%20households%3F%20What%20is%20the%20weighted%20total%20per%20region%3F*%20All%20of%20these%20are%20**split%2C%20apply%2C%20combine**%3A%0A%0A%20%20%20%201.%20**split**%20the%20rows%20into%20groups%20that%20share%20a%20value%20(the%20same%20area%20code)%3B%0A%20%20%20%202.%20**apply**%20a%20calculation%20inside%20each%20group%20(count%20the%20rows%2C%20count%20different%20households%2C%20add%20up%20the%20weights)%3B%0A%20%20%20%203.%20**combine**%20the%20answers%20into%20a%20new%20table%20with%20**one%20row%20per%20group**.%0A%0A%20%20%20%20In%20pandas%3A%20%60table.groupby(%22area_code%22).agg(persons%3D(%22hhid%22%2C%20%22size%22))%60.%20Inside%20%60.agg(...)%60%2C%20each%20result%20is%20written%20as%20%60new_name%3D(%22column%22%2C%20%22function%22)%60.%20Pick%20a%20grouping%20and%20a%20calculation%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%20group_by%20%3D%20mo.ui.dropdown(options%3D%5B%22area_code%22%2C%20%22hhid%22%5D%2C%20value%3D%22area_code%22%2C%20label%3D%22group%20by%3A%22)%0A%20%20%20%20group_agg%20%3D%20mo.ui.dropdown(options%3Dlist(explain.GROUP_AGGS)%2C%20value%3D%22count%20rows%20(size)%22%2C%20label%3D%22calculate%3A%22)%0A%20%20%20%20mo.hstack(%5Bgroup_by%2C%20group_agg%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20group_agg%2C%20group_by%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(group_agg%2C%20group_by)%3A%0A%20%20%20%20explain.group_demo(group_by.value%2C%20group_agg.value)%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**%60size%60%2C%20%60count%60%20and%20%60nunique%60.**%20%60size%60%20counts%20the%20rows%20in%20a%20group.%20%60count%60%20counts%20the%20rows%20where%20the%20column%20is%20not%20missing.%20%60nunique%60%20counts%20the%20**different**%20values%3A%20four%20rows%20from%20two%20households%20give%20a%20%60size%60%20of%204%20and%20an%20%60nunique%60%20of%20%60hhid%60%20of%202.%0A%0A%20%20%20%20**In%20SQL%3A**%20%60SELECT%20area_code%2C%20count(*)%20AS%20persons%2C%20count(DISTINCT%20hhid)%20AS%20households%20FROM%20t%20GROUP%20BY%20area_code%60.%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Forgetting%20%60.reset_index()%60.*%20After%20%60groupby%60%2C%20the%20group%20column%20becomes%20the%20table's%20**index**%20(its%20row%20labels)%20instead%20of%20a%20normal%20column.%20%60.reset_index()%60%2C%20or%20%60groupby(...%2C%20as_index%3DFalse)%60%2C%20makes%20it%20a%20column%20again.%0A%20%20%20%20-%20*Counting%20rows%20when%20you%20meant%20households.*%20Use%20%60nunique%60%20on%20%60hhid%60%20to%20count%20households.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%3A%20Exercises%203.3%20and%204.4.%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%206%20%C2%B7%20Joins%3A%20left%2C%20inner%2C%20and%20the%20anti-join%0A%0A%20%20%20%20A%20**join**%20puts%20two%20tables%20side%20by%20side%2C%20matching%20rows%20on%20a%20shared%20column%2C%20the%20**key**.%0A%0A%20%20%20%20-%20A%20**left**%20join%20keeps%20**every**%20row%20of%20the%20left%20table.%20Rows%20that%20find%20no%20match%20get%20empty%20values.%0A%20%20%20%20-%20An%20**inner**%20join%20keeps%20only%20rows%20that%20found%20a%20match%3A%20rows%20without%20one%20**disappear**%2C%20silently.%0A%20%20%20%20-%20An%20**anti-join**%20keeps%20only%20the%20left%20rows%20that%20found%20**no**%20match%3A%20the%20problems%20to%20report.%0A%20%20%20%20-%20If%20the%20key%20appears%20**twice**%20on%20the%20right%2C%20each%20matching%20left%20row%20is%20**copied**.%20%60validate%3D%22many_to_one%22%60%20makes%20pandas%20refuse%20instead.%0A%0A%20%20%20%20Switch%20between%20the%20kinds%20of%20join%2C%20and%20the%20duplicate%20and%20the%20check%20on%20and%20off%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%20join_how%20%3D%20mo.ui.radio(options%3D%7B%22left%20join%22%3A%20%22left%22%2C%20%22inner%20join%22%3A%20%22inner%22%2C%20%22anti-join%22%3A%20%22anti%22%7D%2C%20value%3D%22left%20join%22%2C%20label%3D%22kind%20of%20join%3A%22%2C%20inline%3DTrue)%0A%20%20%20%20join_dupe%20%3D%20mo.ui.checkbox(label%3D%22the%20code%20list%20lists%200100105000%20twice%22%2C%20value%3DTrue)%0A%20%20%20%20join_check%20%3D%20mo.ui.checkbox(label%3D'validate%3D%22many_to_one%22'%2C%20value%3DFalse)%0A%20%20%20%20mo.vstack(%5Bjoin_how%2C%20mo.hstack(%5Bjoin_dupe%2C%20join_check%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%5D)%0A%20%20%20%20return%20join_check%2C%20join_dupe%2C%20join_how%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(join_check%2C%20join_dupe%2C%20join_how)%3A%0A%20%20%20%20explain.join_demo(join_how.value%2C%20join_dupe.value%2C%20join_check.value)%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%20rule%3A**%20after%20every%20join%2C%20compare%20the%20number%20of%20rows%20before%20and%20after.%20More%20rows%20means%20a%20key%20was%20not%20unique%3B%20fewer%20(with%20a%20left%20join)%20cannot%20happen%3B%20fewer%20with%20an%20inner%20join%20means%20records%20were%20dropped.%0A%0A%20%20%20%20**In%20SQL%3A**%20%60SELECT%20*%20FROM%20people%20AS%20p%20LEFT%20JOIN%20codes%20AS%20c%20ON%20p.area_code%20%3D%20c.area_code%60%3B%20the%20anti-join%20adds%20%60WHERE%20c.area_code%20IS%20NULL%60.%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*A%20duplicate%20key%20on%20the%20right.*%20It%20copies%20rows%20without%20any%20error.%20Make%20the%20key%20unique%20first%2C%20and%20use%20%60validate%3D%60.%0A%20%20%20%20-%20*An%20inner%20join%20where%20you%20needed%20a%20left%20join.*%20Records%20with%20unknown%20codes%20vanish%2C%20and%20nobody%20is%20told.%0A%20%20%20%20-%20*Joining%20text%20to%20a%20number*%20(%60'01'%60%20and%20%601%60)%3A%20nothing%20matches%2C%20or%20the%20database%20refuses.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%3A%20Exercises%203.2%2C%203.3%20and%203.4%2C%20and%20the%20comparison%20in%20%C2%A74.%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%207%20%C2%B7%20What%20a%20survey%20weight%20is%0A%0A%20%20%20%20A%20survey%20does%20not%20ask%20everyone.%20It%20asks%20a%20**sample**%2C%20and%20gives%20each%20respondent%20a%20**weight**%3A%20how%20many%20people%20in%20the%20whole%20population%20that%20respondent%20stands%20for.%20A%20weight%20of%20600%20means%20*%22this%20person%20speaks%20for%20600%20people%20like%20them%22*.%0A%0A%20%20%20%20Adding%20up%20the%20weights%20gives%20a%20**weighted%20count**%3A%20an%20estimate%20of%20how%20many%20people%20there%20are.%20Weights%20differ%20between%20respondents%20because%20some%20kinds%20of%20people%20were%20less%20likely%20to%20be%20asked%2C%20or%20less%20likely%20to%20answer%2C%20and%20the%20weights%20make%20up%20for%20it.%20Move%20the%20weights%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20weight_a%20%3D%20mo.ui.slider(100%2C%202000%2C%20step%3D50%2C%20value%3D600%2C%20label%3D%22Ana's%20weight%22%2C%20show_value%3DTrue)%0A%20%20%20%20weight_b%20%3D%20mo.ui.slider(100%2C%202000%2C%20step%3D50%2C%20value%3D450%2C%20label%3D%22Ben's%20weight%22%2C%20show_value%3DTrue)%0A%20%20%20%20weight_c%20%3D%20mo.ui.slider(100%2C%202000%2C%20step%3D50%2C%20value%3D1200%2C%20label%3D%22Cora's%20weight%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.vstack(%5Bweight_a%2C%20weight_b%2C%20weight_c%5D)%0A%20%20%20%20return%20weight_a%2C%20weight_b%2C%20weight_c%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(weight_a%2C%20weight_b%2C%20weight_c)%3A%0A%20%20%20%20explain.weights_demo(%5Bweight_a.value%2C%20weight_b.value%2C%20weight_c.value%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**In%20pandas%3A**%20%60round_df%5B%22weight%22%5D.sum()%60%20is%20the%20weighted%20count%20of%20the%20whole%20round%3B%20%60round_df.groupby(%22region%22)%5B%22weight%22%5D.sum()%60%20gives%20one%20per%20region.%0A%0A%20%20%20%20**Never%20change%20the%20weights%20to%20make%20a%20total%20match%20a%20published%20figure.**%20Weights%20come%20from%20the%20survey's%20design.%20A%20region%20that%20is%20far%20from%20the%20published%20figure%20is%20something%20to%20**report**%20to%20whoever%20produces%20the%20weights.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%3A%20%C2%A74%20and%20Exercise%204.4.%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%208%20%C2%B7%20JSON%3A%20dictionaries%20and%20lists%20inside%20each%20other%0A%0A%20%20%20%20**JSON**%20is%20the%20text%20format%20most%20web%20APIs%20answer%20in.%20It%20is%20made%20of%20the%20same%20pieces%20as%20Python%3A%0A%0A%20%20%20%20%7C%20JSON%20%7C%20Python%20%7C%20written%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20object%20%7C%20dictionary%20%7C%20%60%7B%22name%22%3A%20value%2C%20...%7D%60%20%7C%0A%20%20%20%20%7C%20array%20%7C%20list%20%7C%20%60%5Bvalue%2C%20value%2C%20...%5D%60%20%7C%0A%20%20%20%20%7C%20string%20%7C%20text%20%7C%20%60%2201%22%60%20%7C%0A%20%20%20%20%7C%20number%20%7C%20%60int%60%20or%20%60float%60%20%7C%20%602019%60%20%7C%0A%20%20%20%20%7C%20%60true%60%20%2F%20%60false%60%20%7C%20%60True%60%20%2F%20%60False%60%20%7C%20%7C%0A%20%20%20%20%7C%20%60null%60%20%7C%20%60None%60%20%7C%20%7C%0A%0A%20%20%20%20%60json.loads(text)%60%20turns%20JSON%20text%20into%20Python%20values.%20Then%20you%20go%20**inside**%20step%20by%20step%3A%20%60%5B%22name%22%5D%60%20for%20a%20dictionary%2C%20%60%5Bposition%5D%60%20for%20a%20list%20(counting%20from%200).%20Pick%20a%20path%20into%20one%20API%20page%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%20json_path%20%3D%20mo.ui.dropdown(options%3Dlist(explain.JSON_PATHS)%2C%20value%3D'page%5B%22data%22%5D%5B0%5D'%2C%20label%3D%22Look%20at%3A%22)%0A%20%20%20%20json_path%0A%20%20%20%20return%20(json_path%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(json_path)%3A%0A%20%20%20%20explain.json_demo(json_path.value)%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**With%20pydantic**%2C%20a%20checked%20page%20is%20an%20object%20instead%20of%20a%20dictionary%2C%20and%20you%20go%20inside%20with%20a%20dot%3A%20%60page.data%5B0%5D.key%5B0%5D%60%20instead%20of%20%60page%5B%22data%22%5D%5B0%5D%5B%22key%22%5D%5B0%5D%60.%20In%20exchange%2C%20every%20record%20has%20been%20checked%3A%20a%20missing%20field%20or%20a%20wrong%20type%20would%20have%20been%20refused%20when%20the%20page%20was%20read.%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Counting%20from%201.*%20The%20first%20record%20is%20%60%5B0%5D%60.%0A%20%20%20%20-%20*Relying%20on%20order.*%20Which%20region%20and%20year%20a%20record%20is%2C%20is%20in%20its%20%60key%60%2C%20not%20in%20its%20position%20on%20the%20page.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%3A%20%C2%A74%20and%20Exercise%204.3.%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%209%20%C2%B7%20Percent%20difference%20and%20tolerance%0A%0A%20%20%20%20To%20compare%20a%20survey%20total%20with%20a%20published%20figure%2C%20compute%20how%20far%20apart%20they%20are%2C%20**as%20a%20share%20of%20the%20published%20figure**%3A%0A%0A%20%20%20%20%24%24%5Ctext%7Bdifference%20%5C%25%7D%20%3D%20%5Cfrac%7B%5Ctext%7Bsurvey%7D%20-%20%5Ctext%7Bpublished%7D%7D%7B%5Ctext%7Bpublished%7D%7D%20%5Ctimes%20100%24%24%0A%0A%20%20%20%20A%20**positive**%20result%20means%20the%20survey%20is%20above%20the%20published%20figure%2C%20a%20**negative**%20one%20below.%20A%20**tolerance**%20is%20how%20far%20apart%20you%20accept%20before%20someone%20should%20look%3A%20with%20a%20tolerance%20of%201%20%25%2C%20a%20difference%20of%20%E2%88%920.4%20%25%20passes%20and%20%2B2.6%20%25%20does%20not.%20Compare%20the%20number%20**without%20its%20sign**%20(%60abs(...)%60)%20with%20the%20tolerance.%20Change%20the%20numbers%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%20pct_survey%20%3D%20mo.ui.number(start%3D0%2C%20stop%3D10_000_000%2C%20step%3D1000%2C%20value%3D2_946_000%2C%20label%3D%22survey%20total%22)%0A%20%20%20%20pct_published%20%3D%20mo.ui.number(start%3D1%2C%20stop%3D10_000_000%2C%20step%3D1000%2C%20value%3D2_871_400%2C%20label%3D%22published%22)%0A%20%20%20%20pct_tolerance%20%3D%20mo.ui.slider(0.5%2C%205.0%2C%20step%3D0.5%2C%20value%3D1.0%2C%20label%3D%22tolerance%20(%25)%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.hstack(%5Bpct_survey%2C%20pct_published%2C%20pct_tolerance%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20pct_published%2C%20pct_survey%2C%20pct_tolerance%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(pct_published%2C%20pct_survey%2C%20pct_tolerance)%3A%0A%20%20%20%20explain.pct_demo(pct_survey.value%2C%20pct_published.value%2C%20pct_tolerance.value)%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**Common%20mistakes**%0A%0A%20%20%20%20-%20*Dividing%20by%20the%20survey%20total*%20instead%20of%20the%20published%20figure%3A%20the%20result%20is%20close%2C%20but%20not%20the%20same%2C%20and%20it%20is%20not%20what%20the%20published%20figure%20is%20compared%20with.%0A%20%20%20%20-%20*Comparing%200.026%20with%201.*%20A%20tolerance%20of%201%20%25%20is%20%600.01%60%20as%20a%20share%2C%20or%20%601%60%20in%20percent.%20Use%20the%20same%20scale%20on%20both%20sides.%0A%20%20%20%20-%20*Forgetting%20the%20sign.*%20%E2%88%922.6%20%25%20is%20as%20far%20away%20as%20%2B2.6%20%25%3B%20compare%20%60abs(...)%60.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B10_get_round.py%5D(%2F%3Ffile%3D10_get_round.py)%3A%20Exercise%204.4.%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%2010%20%C2%B7%20Reading%20an%20error%0A%0A%20%20%20%20An%20error%20is%20Python%20telling%20you%2C%20as%20exactly%20as%20it%20can%2C%20what%20went%20wrong%20and%20where.%20It%20looks%20alarming%2C%20but%20it%20is%20read%20in%20a%20fixed%20order.%20Here%20is%20a%20typical%20one%2C%20from%20Exercise%203.1%20when%20the%20columns%20have%20not%20been%20renamed%20yet%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%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%60%60text%5Cn%7Bexplain.SAMPLE_TRACEBACK%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%201.%20**Read%20the%20last%20line%20first.**%20It%20names%20the%20**kind**%20of%20error%20and%20the%20value%20involved%3A%20%60KeyError%3A%20'code'%60%20means%20*there%20is%20no%20column%20called%20%60code%60*.%0A%20%20%20%202.%20**Then%20the%20line%20just%20above%20it.**%20It%20is%20the%20line%20of%20your%20code%20that%20failed%2C%20and%20%60~~~%5E%5E%5E%60%20points%20at%20the%20exact%20part%3A%20%60table%5B%22code%22%5D%60.%0A%20%20%20%203.%20**The%20lines%20further%20up**%20show%20how%20Python%20got%20there%3A%20the%20cell%20called%20%60load_code_list%60%2C%20which%20ran%20that%20line.%20You%20rarely%20need%20them.%0A%0A%20%20%20%20Here%20the%20table%20still%20has%20the%20column%20%60Code%60%20with%20a%20capital%20C%2C%20because%20the%20rename%20(step%202)%20has%20not%20been%20written%20yet.%0A%20%20%20%20%22%22%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.vstack(%5Bmo.md(%22**The%20errors%20you%20will%20meet%20most%20often%2C%20and%20what%20they%20usually%20mean%3A**%22)%2C%20mo.ui.table(explain.error_table()%2C%20selection%3DNone%2C%20pagination%3DFalse)%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**What%20to%20do%2C%20in%20order**%0A%0A%20%20%20%201.%20Read%20the%20last%20line%2C%20then%20your%20line%20above%20it.%0A%20%20%20%202.%20Try%20your%20function%20on%20one%20small%20input%2C%20in%20the%20**Try%20it**%20cell%20under%20the%20exercise%2C%20and%20look%20at%20what%20it%20gives%20back.%0A%20%20%20%203.%20Look%20at%20the%20values%20on%20the%20way%3A%20add%20a%20line%20such%20as%20%60print(table.columns)%60%20or%20%60print(value%2C%20type(value))%60%20before%20the%20line%20that%20fails%2C%20run%2C%20and%20read%20what%20it%20prints.%20Remove%20the%20line%20afterwards.%0A%20%20%20%204.%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%0A%20%20%20%20**marimo's%20own%20messages%3A**%20*multiple%20definitions*%20means%20two%20cells%20create%20the%20same%20name%20(rename%20one%2C%20or%20start%20it%20with%20%60_%60)%3B%20a%20cell%20that%20says%20it%20is%20*stopped*%20or%20waiting%20is%20waiting%20for%20an%20exercise%20above%20it%20to%20be%20finished.%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%2011%20%C2%B7%20Rules%20with%20pandera%0A%0A%20%20%20%20A%20**schema**%20lists%20the%20rules%20a%20correct%20table%20must%20meet%3A%20one%20entry%20per%20column%20(its%20type%2C%20whether%20it%20may%20be%20missing%2C%20the%20checks%20its%20values%20must%20pass)%2C%20and%20rules%20about%20several%20columns%20at%20once.%20%60schema.validate(table%2C%20lazy%3DTrue)%60%20checks%20every%20rule%20on%20every%20row%20and%2C%20if%20anything%20fails%2C%20raises%20one%20error%20whose%20%60failure_cases%60%20is%20a%20table%20of%20**every**%20failure%3A%20which%20column%2C%20which%20rule%2C%20which%20value%2C%20which%20row.%0A%0A%20%20%20%20Without%20%60lazy%3DTrue%60%2C%20pandera%20stops%20at%20the%20**first**%20failure%3A%20you%20fix%20it%2C%20run%20again%2C%20meet%20the%20next%20one%2C%20and%20so%20on.%20With%20it%2C%20you%20see%20the%20whole%20problem%20at%20once.%20Pick%20rules%2C%20and%20switch%20%60lazy%60%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%20p11_rules%20%3D%20mo.ui.multiselect(options%3Dlist(explain.PANDERA_RULES)%2C%20value%3Dlist(explain.PANDERA_RULES)%2C%20label%3D%22rules%3A%22)%0A%20%20%20%20p11_lazy%20%3D%20mo.ui.checkbox(label%3D%22lazy%3DTrue%22%2C%20value%3DTrue)%0A%20%20%20%20mo.hstack(%5Bp11_rules%2C%20p11_lazy%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20p11_lazy%2C%20p11_rules%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(p11_lazy%2C%20p11_rules)%3A%0A%20%20%20%20explain.pandera_demo(p11_rules.value%2C%20p11_lazy.value)%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**Failures%20and%20warnings.**%20A%20check%20with%20%60raise_warning%3DTrue%60%20does%20not%20fail%3A%20it%20**warns**%20and%20lets%20the%20table%20through.%20Use%20warnings%20for%20the%20unusual%20(a%20total%202%20%25%20from%20the%20published%20figure)%2C%20failures%20for%20the%20impossible%20(an%20age%20of%20150).%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Forgetting%20%60nullable%3DTrue%60*%20on%20a%20column%20that%20may%20be%20missing%3A%20every%20missing%20value%20becomes%20a%20failure.%0A%20%20%20%20-%20*Whole%20numbers%20in%20a%20decimal%20column*%3A%20%60pa.Column(float)%60%20refuses%20a%20column%20of%20%60int%60%3B%20add%20%60coerce%3DTrue%60%20to%20convert%20first.%0A%20%20%20%20-%20*Changing%20the%20rule%20until%20the%20check%20passes.*%20A%20failure%20is%20a%20question%20about%20the%20data%3A%20look%20at%20the%20rows%20first.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B20_check_round.py%5D(%2F%3Ffile%3D20_check_round.py)%3A%20Exercises%202.1%20and%202.2%2C%20and%20section%203.%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%2012%20%C2%B7%20Database%20constraints%0A%0A%20%20%20%20A%20database%20table%20can%20hold%20rules%20of%20its%20own%2C%20checked%20on%20**every**%20row%20it%20is%20given%2C%20by%20any%20program%3A%20a%20%60PRIMARY%20KEY%60%20(no%20two%20rows%20share%20these%20columns)%2C%20%60NOT%20NULL%60%20(never%20missing)%2C%20%60CHECK%20(condition)%60%20(every%20row%20meets%20it)%2C%20and%20%60REFERENCES%60%20(a%20**foreign%20key**%3A%20the%20value%20must%20exist%20in%20another%20table).%20A%20row%20that%20breaks%20one%20is%20**refused**%20with%20an%20error%2C%20and%20nothing%20of%20it%20is%20stored.%20Try%20rows%20against%20a%20small%20table%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%20p12_insert%20%3D%20mo.ui.dropdown(options%3Dlist(explain.CONSTRAINT_INSERTS)%2C%20value%3D%22an%20area%20code%20that%20is%20not%20in%20areas%22%2C%20label%3D%22Insert%3A%22)%0A%20%20%20%20p12_insert%0A%20%20%20%20return%20(p12_insert%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(p12_insert)%3A%0A%20%20%20%20explain.constraint_demo(p12_insert.value)%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**Where%20the%20rules%20come%20from.**%20In%20this%20course%20the%20table's%20constraints%20are%20**generated**%20from%20the%20pandera%20schema%2C%20so%20each%20rule%20is%20written%20once.%20Rules%20about%20several%20columns%20at%20once%20stay%20in%20pandera%3A%20the%20database%20would%20need%20a%20second%2C%20hand-written%20copy%20of%20them.%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Expecting%20a%20CHECK%20to%20refuse%20a%20missing%20value.*%20%60CHECK%20(age%20BETWEEN%200%20AND%20120)%60%20lets%20a%20missing%20age%20through%20(SQL%20treats%20%22unknown%22%20as%20not%20failing)%3B%20%60NOT%20NULL%60%20is%20the%20rule%20that%20refuses%20missing%20values.%0A%20%20%20%20-%20*A%20foreign%20key%20to%20a%20code%20that%20will%20be%20removed.*%20The%20referenced%20row%20cannot%20be%20deleted%20while%20rows%20point%20to%20it.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B22_store_round.py%5D(%2F%3Ffile%3D22_store_round.py)%3A%20sections%201%2C%202%20and%204.%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%2013%20%C2%B7%20Transactions%0A%0A%20%20%20%20A%20**transaction**%20groups%20steps%20so%20that%20they%20are%20kept%20**all%20together**%20(%60COMMIT%60)%20or%20**not%20at%20all**%20(%60ROLLBACK%60).%20A%20load%20that%20deletes%20a%20round%20and%20inserts%20it%20again%20belongs%20in%20one%3A%20if%20an%20insert%20fails%20half-way%2C%20the%20rollback%20puts%20the%20table%20back%20as%20it%20was%2C%20instead%20of%20leaving%20half%20a%20round.%0A%0A%20%20%20%20Deleting%20the%20round's%20rows%20before%20inserting%20them%20also%20makes%20the%20load%20**idempotent**%3A%20running%20it%20twice%20gives%20the%20same%20table%20as%20running%20it%20once.%20Load%20with%20and%20without%20a%20transaction%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%20p13_transaction%20%3D%20mo.ui.switch(label%3D%22inside%20a%20transaction%22%2C%20value%3DTrue)%0A%20%20%20%20p13_transaction%0A%20%20%20%20return%20(p13_transaction%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(p13_transaction)%3A%0A%20%20%20%20explain.transaction_demo(p13_transaction.value)%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%60%60%60python%0A%20%20%20%20con.execute(%22BEGIN%20TRANSACTION%22)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20...%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20the%20steps%0A%20%20%20%20%20%20%20%20con.execute(%22COMMIT%22)%0A%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20con.execute(%22ROLLBACK%22)%20%20%20%20%23%20undo%20everything%20since%20BEGIN%0A%20%20%20%20%20%20%20%20raise%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20and%20let%20the%20caller%20know%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*No%20%60ROLLBACK%60%20after%20an%20error*%3A%20the%20transaction%20stays%20open%2C%20and%20the%20next%20%60BEGIN%60%20fails%20with%20*cannot%20start%20a%20transaction%20within%20a%20transaction*.%0A%20%20%20%20-%20*Swallowing%20the%20error*%3A%20after%20the%20rollback%2C%20%60raise%60%20it%20again%2C%20or%20the%20caller%20believes%20the%20load%20worked.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B22_store_round.py%5D(%2F%3Ffile%3D22_store_round.py)%3A%20Exercises%203.1%20and%204.1.%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%2014%20%C2%B7%20Crosswalks%20between%20editions%0A%0A%20%20%20%20A%20code%20list%20changes%20between%20**editions**%3A%20a%20municipality%20gets%20a%20new%20code%2C%20one%20splits%20in%20two%2C%20two%20merge.%20Data%20coded%20in%20different%20editions%20cannot%20be%20compared%20area%20by%20area%20until%20one%20of%20them%20is%20translated%20into%20the%20other's%20codes.%20The%20table%20that%20translates%20is%20a%20**crosswalk**%3A%20one%20row%20per%20pair%20of%20codes%20that%20cover%20the%20same%20ground.%0A%0A%20%20%20%20Which%20way%20you%20translate%20decides%20what%20can%20go%20wrong.%20Pick%20a%20direction%2C%20and%20switch%20the%20check%20on%20and%20off%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%20p14_direction%20%3D%20mo.ui.dropdown(options%3D%5B%22new%20codes%20to%20old%22%2C%20%22old%20codes%20to%20new%22%5D%2C%20value%3D%22new%20codes%20to%20old%22%2C%20label%3D%22Direction%3A%22)%0A%20%20%20%20p14_validate%20%3D%20mo.ui.switch(label%3D'validate%3D%22many_to_one%22'%2C%20value%3DTrue)%0A%20%20%20%20mo.hstack(%5Bp14_direction%2C%20p14_validate%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20p14_direction%2C%20p14_validate%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(p14_direction%2C%20p14_validate)%3A%0A%20%20%20%20explain.crosswalk_demo(p14_direction.value%2C%20p14_validate.value)%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**New%20to%20old**%20is%20safe%3A%20every%20new%20code%20has%20exactly%20one%20old%20code%2C%20so%20each%20person%20keeps%20one%20row%2C%20and%20the%20split%20municipality%20is%20counted%20as%20it%20was%20before.%20**Old%20to%20new**%20cannot%20say%20which%20successor%20a%20person%20of%20the%20split%20municipality%20lives%20in%3A%20a%20plain%20join%20copies%20them%20into%20both.%20The%20honest%20answers%20are%20to%20keep%20the%20old%20geography%2C%20or%20to%20share%20the%20old%20total%20out%20by%20each%20successor's%20share%20(stretch%20S3.3).%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Joining%20without%20%60validate%3D%60.*%20The%20copies%20do%20not%20raise%20an%20error%3A%20the%20table%20is%20just%20longer%2C%20and%20every%20total%20is%20too%20big.%0A%20%20%20%20-%20*Assuming%20a%20changed%20code%20is%20a%20new%20place.*%20A%20code%20that%20changed%20for%20the%20same%20people%20is%20a%20rename%3A%20the%20crosswalk%20links%20it%2C%20and%20its%20numbers%20continue.%0A%20%20%20%20-%20*Comparing%20rounds%20coded%20in%20different%20editions%20directly.*%20The%20split%20municipality%20appears%20to%20lose%20people%20in%20one%20round%20and%20the%20new%20code%20to%20gain%20them%20from%20nothing.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B30_new_edition.py%5D(%2F%3Ffile%3D30_new_edition.py)%3A%20section%203%3B%20%5B83_stretch_day3.py%5D(%2F%3Ffile%3D83_stretch_day3.py)%3A%20S3.3.%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%2015%20%C2%B7%20Small-cell%20suppression%0A%0A%20%20%20%20A%20published%20cell%20built%20from%20very%20few%20respondents%20does%20two%20kinds%20of%20harm%3A%20it%20can%20point%20at%20**individuals**%20(the%20two%20women%20over%2065%20in%20a%20thin%20province)%2C%20and%20it%20is%20**unreliable**%20(a%20handful%20of%20answers%20swings%20wildly%20between%20rounds).%20So%20a%20cell%20from%20fewer%20respondents%20than%20a%20**threshold**%20is%20published%20empty%20and%20marked%20**suppressed**.%20The%20number%20of%20respondents%20itself%20is%20not%20published.%0A%0A%20%20%20%20Hiding%20a%20cell%20is%20not%20always%20enough%3A%20if%20a%20total%20is%20published%20and%20only%20**one**%20of%20its%20cells%20is%20hidden%2C%20the%20total%20minus%20the%20visible%20cells%20gives%20the%20hidden%20one%20back.%20**Complementary%20suppression**%20hides%20a%20second%20cell%20so%20it%20cannot%20be%20worked%20out.%20Move%20the%20threshold%20and%20switch%20it%20on%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%20p15_threshold%20%3D%20mo.ui.slider(5%2C%20120%2C%20step%3D5%2C%20value%3D30%2C%20label%3D%22threshold%20(respondents)%3A%22%2C%20show_value%3DTrue)%0A%20%20%20%20p15_complementary%20%3D%20mo.ui.switch(label%3D%22complementary%20suppression%22%2C%20value%3DFalse)%0A%20%20%20%20mo.hstack(%5Bp15_threshold%2C%20p15_complementary%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20p15_complementary%2C%20p15_threshold%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(p15_complementary%2C%20p15_threshold)%3A%0A%20%20%20%20explain.suppression_demo(p15_threshold.value%2C%20p15_complementary.value)%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%60%60%60python%0A%20%20%20%20small%20%3D%20table%5B%22respondents%22%5D%20%3C%20threshold%0A%20%20%20%20published%20%3D%20table.assign(weighted_persons%3Dtable%5B%22weighted_persons%22%5D.where(~small)%2C%20suppressed%3Dsmall).drop(columns%3D%22respondents%22)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Writing%200%20for%20a%20suppressed%20cell.*%20A%20zero%20is%20a%20number%3A%20it%20says%20%22nobody%22%2C%20which%20is%20false.%20Leave%20it%20empty%2C%20and%20mark%20it.%0A%20%20%20%20-%20*Suppressing%20in%20one%20file%20and%20not%20in%20another.*%20Every%20format%20of%20the%20release%20comes%20from%20the%20same%20suppressed%20table%2C%20and%20each%20is%20checked%20after%20it%20is%20written.%0A%20%20%20%20-%20*Publishing%20the%20respondent%20counts.*%20They%20tell%20a%20reader%20exactly%20which%20cells%20are%20small%2C%20and%20how%20small.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B31_share_round.py%5D(%2F%3Ffile%3D31_share_round.py)%3A%20section%203%3B%20%5B83_stretch_day3.py%5D(%2F%3Ffile%3D83_stretch_day3.py)%3A%20S3.1.%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%2016%20%C2%B7%20File%20formats%3A%20what%20survives%20the%20round%20trip%0A%0A%20%20%20%20A%20file%20is%20only%20as%20good%20as%20what%20comes%20back%20when%20someone%20reads%20it.%20Each%20format%20keeps%20some%20things%20and%20loses%20others%3A%0A%0A%20%20%20%20%7C%20format%20%7C%20keeps%20the%20types%3F%20%7C%20an%20empty%20cell%20is%20%7C%20good%20for%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**CSV**%20%7C%20no%3A%20every%20reader%20guesses%2C%20and%20a%20code%20that%20looks%20like%20a%20number%20loses%20its%20zeros%20%7C%20an%20empty%20field%20%7C%20everyone%2C%20in%20any%20program%2C%20with%20a%20**schema%20file**%20beside%20it%20that%20says%20each%20column's%20type%20%7C%0A%20%20%20%20%7C%20**Parquet**%20%7C%20yes%2C%20inside%20the%20file%20%7C%20missing%20%7C%20analysts%20reading%20it%20again%20and%20again%3A%20small%20and%20fast%20%7C%0A%20%20%20%20%7C%20**SQLite**%20%7C%20mostly%3A%20text%2C%20numbers%3B%20true%2Ffalse%20becomes%201%20and%200%20%7C%20%60NULL%60%20%7C%20a%20whole%20database%20in%20one%20file%3B%20a%20data%20portal%20%7C%0A%0A%20%20%20%20Write%20a%20small%20release%20table%20and%20read%20it%20back%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%20p16_format%20%3D%20mo.ui.dropdown(options%3D%5B%22CSV%22%2C%20%22Parquet%22%2C%20%22SQLite%22%5D%2C%20value%3D%22CSV%22%2C%20label%3D%22Format%3A%22)%0A%20%20%20%20p16_types%20%3D%20mo.ui.switch(label%3D%22tell%20the%20reader%20the%20types%22%2C%20value%3DFalse)%0A%20%20%20%20mo.hstack(%5Bp16_format%2C%20p16_types%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20p16_format%2C%20p16_types%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(p16_format%2C%20p16_types)%3A%0A%20%20%20%20explain.formats_demo(p16_format.value%2C%20p16_types.value)%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%20A%20**Table%20Schema**%20(%60release_table.schema.json%60)%20is%20the%20CSV's%20missing%20half%3A%20a%20small%20JSON%20file%20listing%20each%20column's%20name%2C%20type%2C%20description%20and%20whether%20it%20may%20be%20empty%2C%20in%20a%20standard%20form%20(specs.frictionlessdata.io)%20that%20tools%20can%20read.%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Opening%20a%20CSV%20of%20codes%20in%20a%20spreadsheet%20and%20saving%20it.*%20The%20program%20turns%20%600100100000%60%20into%20%60100100000%60%2C%20and%20the%20file%20is%20changed%20for%20everyone%20after%20you.%0A%20%20%20%20-%20*Trusting%20that%20the%20export%20worked.*%20Read%20every%20file%20back%20and%20compare%3A%20the%20row%20count%2C%20and%20the%20cells%20that%20must%20be%20empty.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B31_share_round.py%5D(%2F%3Ffile%3D31_share_round.py)%3A%20sections%201%20and%205.%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%2017%20%C2%B7%20Datasette%20addresses%20and%20APIs%0A%0A%20%20%20%20Every%20page%20Datasette%20shows%20is%20a%20web%20address%2C%20and%20every%20part%20of%20the%20address%20means%20something%3A%20which%20database%2C%20which%20table%20or%20query%2C%20which%20filter%2C%20which%20format.%20Change%20the%20parts%20and%20read%20what%20each%20one%20does%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%20p17_page%20%3D%20mo.ui.dropdown(options%3D%5B%22the%20table%22%2C%20%22the%20canned%20query%22%5D%2C%20value%3D%22the%20table%22%2C%20label%3D%22Page%3A%22)%0A%20%20%20%20p17_sex%20%3D%20mo.ui.dropdown(options%3D%5B%22everyone%22%2C%20%22Female%22%2C%20%22Male%22%5D%2C%20value%3D%22everyone%22%2C%20label%3D%22Filter%20sex%3A%22)%0A%20%20%20%20p17_facet%20%3D%20mo.ui.switch(label%3D%22facet%20by%20age%20group%22%2C%20value%3DFalse)%0A%20%20%20%20p17_json%20%3D%20mo.ui.switch(label%3D%22.json%22%2C%20value%3DFalse)%0A%20%20%20%20p17_shape%20%3D%20mo.ui.dropdown(options%3D%5B%22objects%20(default)%22%2C%20%22array%22%5D%2C%20value%3D%22objects%20(default)%22%2C%20label%3D%22_shape%3A%22)%0A%20%20%20%20mo.hstack(%5Bp17_page%2C%20p17_sex%2C%20p17_facet%2C%20p17_json%2C%20p17_shape%5D%2C%20justify%3D%22start%22%2C%20gap%3D1%2C%20wrap%3DTrue)%0A%20%20%20%20return%20p17_facet%2C%20p17_json%2C%20p17_page%2C%20p17_sex%2C%20p17_shape%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(p17_facet%2C%20p17_json%2C%20p17_page%2C%20p17_sex%2C%20p17_shape)%3A%0A%20%20%20%20explain.datasette_url_demo(p17_page.value%2C%20p17_sex.value%2C%20p17_facet.value%2C%20p17_json.value%2C%20p17_shape.value)%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%20A%20table's%20JSON%20comes%20in%20**pages**%20of%20up%20to%20100%20rows%3A%20%60next_url%60%20is%20the%20next%20page's%20address%2C%20and%20%60null%60%20on%20the%20last.%20A%20canned%20query%20answers%20in%20one%20page%2C%20up%20to%20Datasette's%20row%20limit%2C%20and%20says%20%60%22truncated%22%3A%20true%60%20when%20it%20had%20to%20stop.%20A%20program%20reads%20every%20page%2C%20and%20stops%20on%20%60%22ok%22%3A%20false%60%2C%20like%20the%20OpenSTAT%20pages%20in%20the%20Get%20module%20(primer%208).%0A%0A%20%20%20%20**Metadata%20or%20configuration%3F**%20Datasette%201.0%20keeps%20them%20apart%3A%20%60-m%20metadata.json%60%20is%20what%20the%20data%20**means**%20(titles%2C%20descriptions%2C%20source%2C%20licence)%3B%20%60-c%20datasette.json%60%20is%20how%20Datasette%20**behaves**%20(facets%2C%20canned%20queries%2C%20who%20may%20run%20SQL).%20Instructions%20written%20for%20the%20older%200.x%20versions%20put%20both%20in%20one%20file.%0A%0A%20%20%20%20**Common%20mistakes**%0A%0A%20%20%20%20-%20*Reading%20only%20the%20first%20page.*%20100%20rows%20look%20like%20a%20whole%20table.%0A%20%20%20%20-%20*Building%20SQL%20from%20what%20a%20visitor%20typed.*%20A%20canned%20query's%20%60%3Aregion%60%20is%20a%20parameter%3A%20the%20value%20is%20never%20read%20as%20SQL.%0A%0A%20%20%20%20**Used%20in%3A**%20%5B32_datasette.py%5D(%2F%3Ffile%3D32_datasette.py)%3A%20sections%204%20and%205%3B%20%5B83_stretch_day3.py%5D(%2F%3Ffile%3D83_stretch_day3.py)%3A%20S3.4.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
3794bda44b077cf50f0193641ed18cfe