{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "# Population in Zürich\n", "\n", "Zürich Statistical Office collects data on the city and its residents. This data is published as [Linked Data](https://en.wikipedia.org/wiki/Linked_data). \n", "\n", "In this tutorial, we will show **how to work with Linked Data.** Mainly, we will see how to work with population dataset. \n", "We will look into how to query, process, and visualize it. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[1. Population in city districts](#Population-in-city-districts) \n", "[2. Population origin](#Population-origin) \n", "[3. Population distribution: age and time](#Population-distribution:-age-and-time) \n", "[4. Population distribution: age and sex](#Population-distribution:-age-and-sex) \n", "[5. Population distribution: age and origin](#Population-distribution:-age-and-origin) \n", "[6. Population and real estate prices](#Population-and-real-estate-prices) \n", "[7. Causes of death](#Causes-of-death) \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### SPARQL endpoint\n", "\n", "Population data is published as Linked Data thatcan be accessed with [SPARQL queries](https://www.w3.org/TR/rdf-sparql-query/). \n", "You can send queries using HTTP requests. The API endpoint is **[https://ld.stadt-zuerich.ch/query](https://ld.stadt-zuerich.ch/query).** \n", " \n", " \n", "Let's use `SparqlClient` from [graphly](https://github.com/zazuko/graphly) to communicate with the database. \n", "Graphly will allow us to:\n", "* send SPARQL queries\n", "* automatically add prefixes to all queries\n", "* format response to `pandas` or `geopandas`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Uncomment to install dependencies in Colab environment\n", "#!pip install git+https://github.com/zazuko/graphly.git" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import re\n", "\n", "import pandas as pd\n", "import numpy as np\n", "\n", "import plotly.express as px\n", "import plotly.graph_objects as go\n", "from plotly.subplots import make_subplots\n", "\n", "from graphly.api_client import SparqlClient" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def natural_keys(txt: str) -> list[int]:\n", " \"\"\"Extracts the digits from string\n", " Args:\n", " txt: string with digits\n", "\n", " Returns:\n", " list[int] digits in string\n", " \"\"\"\n", " \n", " return [int(s) for s in txt.split() if s.isdigit()]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sparql = SparqlClient(\"https://ld.stadt-zuerich.ch/query\")\n", "sparql.add_prefixes({\n", " \"schema\": \"