%22%22%22Tutorial%201%3A%20find%20a%20GovData%20dataset%2C%20see%20its%20columns%2C%20read%20it%20as%20a%20table.%0A%0ARun%20with%3A%20uv%20run%20marimo%20edit%20tutorials%2F01_first_table.py%20(needs%20network%20access%2C%20no%20account).%0A%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20Tutorial%201%3A%20your%20first%20table%0A%0A%20%20%20%20**Goal%3A**%20find%20a%20dataset%20in%20Germany's%20open%20data%20catalog%20and%20read%20it%20as%20a%20table%20whose%20columns%20have%0A%20%20%20%20types%3A%20dates%20are%20dates%2C%20counts%20are%20numbers.%0A%0A%20%20%20%20**Time%3A**%20about%2015%20minutes.%20**You%20need%3A**%20basic%20Python%20and%20a%20network%20connection.%20No%20account.%0A%0A%20%20%20%20**Setup%3A**%20clone%20the%20repository%20and%20run%20%60uv%20sync%20--all-extras%60%20(see%20the%20README)%2C%20then%0A%20%20%20%20%60uv%20run%20marimo%20edit%20tutorials%2F01_first_table.py%60.%0A%0A%20%20%20%20%5Bmloda%5D(https%3A%2F%2Fgithub.com%2Fmloda-ai%2Fmloda)%20works%20like%20this%3A%20you%20name%20the%20columns%20you%20want%2C%20and%20mloda%0A%20%20%20%20finds%20the%20code%20that%20fetches%20and%20prepares%20them.%20In%20mloda%2C%20a%20column%20you%20ask%20for%20is%20called%20a%20*feature*.%0A%20%20%20%20This%20plugin%20adds%20that%20code%20for%20German%20open%20government%20data.%0A%0A%20%20%20%20%5BGovData%5D(https%3A%2F%2Fwww.govdata.de)%20is%20Germany's%20national%20catalog%20of%20open%20government%20data.%20It%20lists%20the%0A%20%20%20%20datasets%3B%20the%20files%20themselves%20live%20with%20each%20publisher%2C%20here%20the%20city%20of%20Stuttgart.%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%20import%20altair%20as%20alt%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20from%20mloda.user%20import%20Feature%2C%20mloda%0A%20%20%20%20from%20mloda_plugin_govdata.feature_groups.govdata%20import%20(%0A%20%20%20%20%20%20%20%20DownloadCache%2C%0A%20%20%20%20%20%20%20%20GovDataLocator%2C%0A%20%20%20%20%20%20%20%20StuttgartPopulationReader%2C%0A%20%20%20%20%20%20%20%20build_client%2C%0A%20%20%20%20%20%20%20%20resolve_distribution%2C%0A%20%20%20%20%20%20%20%20search_datasets%2C%0A%20%20%20%20)%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20DownloadCache%2C%0A%20%20%20%20%20%20%20%20Feature%2C%0A%20%20%20%20%20%20%20%20GovDataLocator%2C%0A%20%20%20%20%20%20%20%20StuttgartPopulationReader%2C%0A%20%20%20%20%20%20%20%20alt%2C%0A%20%20%20%20%20%20%20%20build_client%2C%0A%20%20%20%20%20%20%20%20mloda%2C%0A%20%20%20%20%20%20%20%20pd%2C%0A%20%20%20%20%20%20%20%20resolve_distribution%2C%0A%20%20%20%20%20%20%20%20search_datasets%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%201%3A%20find%20a%20dataset%0A%0A%20%20%20%20The%20catalog%20is%20in%20German%2C%20so%20we%20search%20in%20German%3A%20*Einwohner*%20means%20residents%2C%20*Altersgruppen*%20means%0A%20%20%20%20age%20groups.%20%60search_datasets%60%20asks%20the%20catalog%20and%20returns%20each%20match%20with%20its%20id%20(the%20catalog%20calls%0A%20%20%20%20it%20%60name%60)%20and%20title.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(build_client%2C%20pd%2C%20search_datasets)%3A%0A%20%20%20%20QUERY%20%3D%20%22einwohner%20stuttgart%20altersgruppen%22%20%20%23%20change%20it%20and%20run%20the%20cell%20again%0A%0A%20%20%20%20with%20build_client()%20as%20_client%3A%0A%20%20%20%20%20%20%20%20_hits%20%3D%20list(search_datasets(_client%2C%20QUERY%2C%20max_results%3D10))%0A%20%20%20%20pd.DataFrame(%7B%22id%22%3A%20%5B_d.name%20for%20_d%20in%20_hits%5D%2C%20%22title%22%3A%20%5B_d.title%20for%20_d%20in%20_hits%5D%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20Look%20for%20the%20id%20%60einwohner-nach-altersgruppen-und-stadtbezirken%60%3A%20residents%20by%20age%20group%20and%20city%0A%20%20%20%20district.%20The%20next%20cell%20holds%20that%20id%2C%20copied%20by%20hand%2C%20so%20the%20rest%20of%20the%20tutorial%20works%20even%20if%20the%0A%20%20%20%20search%20shows%20other%20results.%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%20DATASET_ID%20%3D%20%22einwohner-nach-altersgruppen-und-stadtbezirken%22%0A%20%20%20%20return%20(DATASET_ID%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%202%3A%20see%20the%20columns%0A%0A%20%20%20%20A%20*reader*%20turns%20one%20kind%20of%20file%20into%20a%20table.%20This%20plugin%20ships%20%60StuttgartPopulationReader%60%20for%20this%0A%20%20%20%20dataset%2C%20with%20a%20type%20for%20every%20column.%20(Other%20GovData%20CSV%20files%20with%20semicolons%20between%20values%20and%20one%0A%20%20%20%20header%20row%20work%20with%20%60GovDataReader%60%2C%20which%20reads%20every%20column%20as%20text.)%0A%0A%20%20%20%20%60peek%60%20lists%20the%20columns%20you%20can%20ask%20for.%20It%20downloads%20the%20file%20and%20keeps%20a%20copy%20on%20your%20disk.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DATASET_ID%2C%20StuttgartPopulationReader)%3A%0A%20%20%20%20StuttgartPopulationReader.peek(DATASET_ID)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20Four%20columns%3A%0A%0A%20%20%20%20-%20**Stichtag**%3A%20the%20reference%20date%20the%20count%20was%20taken%20on.%20Type%20%60date32%5Bday%5D%60%2C%20a%20calendar%20date.%0A%20%20%20%20-%20**Stadtbezirk**%3A%20the%20city%20district.%20Type%20%60string%60%2C%20text.%0A%20%20%20%20-%20**Alter%20in%2010%20Gruppen**%3A%20the%20age%20group%2C%20one%20of%20ten.%20Text.%0A%20%20%20%20-%20**Einwohner**%3A%20the%20number%20of%20residents.%20Type%20%60int64%60%2C%20a%20whole%20number.%0A%0A%20%20%20%20Typed%20columns%20mean%20dates%20are%20dates%20and%20counts%20are%20numbers%2C%20so%20you%20can%20compare%20and%20add%20them%20right%20away.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Step%203%3A%20read%20it%0A%0A%20%20%20%20Ask%20for%20each%20column%20as%20a%20%60Feature%60.%20The%20%60options%60%20say%20which%20reader%20to%20use%20and%20which%20dataset%20to%20read.%0A%20%20%20%20%60compute_frameworks%3D%5B%22PyArrowTable%22%5D%60%20picks%20the%20table%20format%20of%20the%20result%2C%20and%0A%20%20%20%20%60column_ordering%3D%22request_order%22%60%20keeps%20the%20columns%20in%20the%20order%20you%20asked%20for.%20The%20result%20is%20a%20list%0A%20%20%20%20of%20tables%3B%20here%20there%20is%20one.%20%60to_pandas()%60%20turns%20it%20into%20a%20pandas%20DataFrame.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DATASET_ID%2C%20Feature%2C%20StuttgartPopulationReader%2C%20mloda)%3A%0A%20%20%20%20_options%20%3D%20%7BStuttgartPopulationReader%3A%20DATASET_ID%7D%0A%20%20%20%20_result%20%3D%20mloda.run_all(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20Feature(%22Stichtag%22%2C%20options%3D_options)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Feature(%22Stadtbezirk%22%2C%20options%3D_options)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Feature(%22Alter%20in%2010%20Gruppen%22%2C%20options%3D_options)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20Feature(%22Einwohner%22%2C%20options%3D_options)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20compute_frameworks%3D%5B%22PyArrowTable%22%5D%2C%0A%20%20%20%20%20%20%20%20column_ordering%3D%22request_order%22%2C%0A%20%20%20%20)%0A%20%20%20%20population%20%3D%20_result%5B0%5D.to_pandas()%0A%20%20%20%20population%0A%20%20%20%20return%20(population%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20One%20row%20per%20date%2C%20district%20and%20age%20group.%20The%20age%20groups%20read%20like%20%2218%20bis%20unter%2030%20Jahre%22%3A%0A%20%20%20%20*bis%20unter*%20means%20%22up%20to%20but%20not%20including%22%2C%20so%20this%20group%20is%2018%20to%2029%20years%20old.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Pitfall%3A%20count%20each%20person%20once%0A%0A%20%20%20%20The%20city%20counts%20its%20residents%20several%20times%20a%20year%3A%20up%20to%20five%20reference%20dates%2C%20four%20in%20most%20recent%0A%20%20%20%20years.%20Here%20is%20the%20district%20Mitte%20(the%20city%20centre)%20in%202020%2C%20summed%20over%20the%20age%20groups.%20pandas%20keeps%0A%20%20%20%20the%20reference%20dates%20as%20plain%20dates%3B%20%60pd.to_datetime%60%20lets%20us%20ask%20for%20their%20year%2C%20month%20and%20day.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pd%2C%20population)%3A%0A%20%20%20%20_dates%20%3D%20pd.to_datetime(population%5B%22Stichtag%22%5D)%0A%20%20%20%20_mitte_2020%20%3D%20population%5B(population%5B%22Stadtbezirk%22%5D%20%3D%3D%20%22Mitte%22)%20%26%20(_dates.dt.year%20%3D%3D%202020)%5D%0A%20%20%20%20_mitte_2020.groupby(%22Stichtag%22)%5B%22Einwohner%22%5D.sum()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20Each%20line%20is%20the%20whole%20district%20on%20one%20date.%20Adding%20up%202020%20would%20count%20the%20same%20people%20four%0A%20%20%20%20times.%20So%20keep%20one%20date%20per%20year%3A%2031%20December.%20The%20newest%20year%20can%20be%20incomplete%20(when%20this%0A%20%20%20%20tutorial%20was%20written%2C%202023%20had%20only%20a%20March%20count)%3B%20without%20a%2031%20December%20count%20it%20drops%20out.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20pd%2C%20population)%3A%0A%20%20%20%20_dates%20%3D%20pd.to_datetime(population%5B%22Stichtag%22%5D)%0A%20%20%20%20year_end%20%3D%20population%5B(_dates.dt.month%20%3D%3D%2012)%20%26%20(_dates.dt.day%20%3D%3D%2031)%5D.assign(year%3D_dates.dt.year)%0A%20%20%20%20mo.md(f%22The%2031%20December%20series%20runs%20from%20%7Byear_end%5B'year'%5D.min()%7D%20to%20%7Byear_end%5B'year'%5D.max()%7D.%22)%0A%20%20%20%20return%20(year_end%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Try%20it%3A%20pick%20a%20district%0A%0A%20%20%20%20Choose%20a%20district%3B%20the%20chart%20below%20shows%20its%20residents%20on%2031%20December%20of%20each%20year.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20year_end)%3A%0A%20%20%20%20_names%20%3D%20sorted(year_end%5B%22Stadtbezirk%22%5D.unique())%0A%20%20%20%20district%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D_names%2C%20value%3D%22Mitte%22%20if%20%22Mitte%22%20in%20_names%20else%20_names%5B0%5D%2C%20label%3D%22District%20(Stadtbezirk)%22%0A%20%20%20%20)%0A%20%20%20%20district%0A%20%20%20%20return%20(district%2C)%0A%0A%0A%40app.cell%0Adef%20_(alt%2C%20district%2C%20year_end)%3A%0A%20%20%20%20_per_year%20%3D%20year_end%5Byear_end%5B%22Stadtbezirk%22%5D%20%3D%3D%20district.value%5D.groupby(%22year%22%2C%20as_index%3DFalse)%5B%22Einwohner%22%5D.sum()%0A%20%20%20%20alt.Chart(_per_year%2C%20title%3Df%22Residents%20of%20%7Bdistrict.value%7D%20on%2031%20December%22).mark_line(point%3DTrue).encode(%0A%20%20%20%20%20%20%20%20x%3Dalt.X(%22year%3AQ%22%2C%20title%3D%22Year%22%2C%20axis%3Dalt.Axis(format%3D%22d%22))%2C%0A%20%20%20%20%20%20%20%20y%3Dalt.Y(%22Einwohner%3AQ%22%2C%20title%3D%22Residents%22)%2C%0A%20%20%20%20%20%20%20%20tooltip%3D%5B%22year%22%2C%20%22Einwohner%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Where%20the%20data%20came%20from%0A%0A%20%20%20%20Every%20published%20number%20needs%20its%20source.%20The%20catalog%20entry%20names%20the%20publisher%20and%20the%20license%3B%20the%0A%20%20%20%20copy%20on%20your%20disk%20records%20when%20you%20downloaded%20the%20file.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DATASET_ID%2C%0A%20%20%20%20DownloadCache%2C%0A%20%20%20%20GovDataLocator%2C%0A%20%20%20%20StuttgartPopulationReader%2C%0A%20%20%20%20build_client%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20resolve_distribution%2C%0A)%3A%0A%20%20%20%20with%20build_client()%20as%20_client%3A%0A%20%20%20%20%20%20%20%20_source%20%3D%20resolve_distribution(GovDataLocator(dataset_id%3DDATASET_ID)%2C%20_client)%0A%20%20%20%20%20%20%20%20with%20DownloadCache(StuttgartPopulationReader.cache_dir%2C%20client%3D_client)%20as%20_cache%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_retrieved%20%3D%20_cache.get_or_download(_source.url%2C%20revalidate%3DFalse).retrieved_at%20%20%23%20the%20copy%20read%20above%0A%20%20%20%20_extras%20%3D%20_source.dataset.extras%20if%20_source.dataset%20else%20%7B%7D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20-%20**Publisher%3A**%20%7B_extras.get(%22publisher_name%22%2C%20%22not%20given%22)%7D%0A%20%20%20%20-%20**License%3A**%20%7B_source.license%7D%0A%20%20%20%20-%20**Dataset%20page%3A**%20%7B_extras.get(%22uri%22)%7D%0A%20%20%20%20-%20**File%3A**%20%7B_source.url%7D%0A%20%20%20%20-%20**Retrieved%3A**%20%7B_retrieved%3A%25Y-%25m-%25d%7D%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20The%20publisher%20is%20the%20statistics%20office%20of%20the%20city%20of%20Stuttgart%20(*Landeshauptstadt*%20means%20state%0A%20%20%20%20capital).%20The%20license%2C%20%5BCC%20BY%204.0%5D(https%3A%2F%2Fcreativecommons.org%2Flicenses%2Fby%2F4.0%2F)%2C%20lets%20you%20reuse%20the%0A%20%20%20%20data%20if%20you%20name%20the%20source%2C%20link%20the%20license%20and%20say%20whether%20you%20changed%20the%20data.%20Here%2C%20say%20that%0A%20%20%20%20you%20kept%20only%2031%20December%20and%20summed%20the%20age%20groups.%0A%0A%20%20%20%20%23%23%20Next%0A%0A%20%20%20%20%5BTutorial%202%5D(https%3A%2F%2Fgithub.com%2Fmloda-ai%2Fmloda-plugin-govdata%2Fblob%2Fmain%2Ftutorials%2F02_elections_and_air_quality.py)%0A%20%20%20%20reads%20election%20results%20and%20air%20measurements%2C%20steering%20each%20reader%20with%20options.%0A%20%20%20%20%5BReaders%5D(https%3A%2F%2Fgithub.com%2Fmloda-ai%2Fmloda-plugin-govdata%2Fblob%2Fmain%2Fdocs%2Freaders.md)%20lists%20every%0A%20%20%20%20source%20this%20plugin%20reads%2C%20how%20to%20search%20them%20and%20how%20to%20read%20any%20other%20GovData%20file.%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
ae7e5f17808f049e716be37aa77bb7e2