Real estate

Real estate in Zürich

the Zürich Statistical Office collects data on the city and its residents. This data is published as Linked Data.

In this tutorial, we will show how to work with Linked Data. Mainly, we will see how to work with the real estate dataset.
We will look into how to query, process, and visualize it.

SPARQL endpoint

Data on the real estate market is published as Linked Data. It can be accessed with SPARQL queries.
You can send queries using HTTP requests. The API endpoint is

Let's use SparqlClient from graphly to communicate with the database. Graphly will allow us to:

  • send SPARQL queries
  • automatically add prefixes to all queries
  • format response to pandas or geopandas
In [1]:
# Uncomment to install dependencies in Colab environment
#!pip install git+
In [2]:
import datetime
import re

import pandas as pd
import numpy as np

import as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

from graphly.api_client import SparqlClient
In [3]:
sparql = SparqlClient("")
    "schema": "<>",
    "cube": "<>",
    "property": "<>",
    "measure": "<>",
    "skos": "<>",
    "ssz": "<>"

SPARQL queries can become very long. To improve the readibility, we will work wih prefixes.

Using the add_prefixes method, we define persistent prefixes. Every time you send a query, graphly will automatically add the prefixes for you.

Housing prices

Let's find the average price per m2 for an apartment in Zurich. This data is available in the QMP-EIG-HAA-OBJ-ZIM data cube. It will allow us to find the price per city district and apartment type. The data is also available for different points in time.

The query for housing prices in city of Zürich for different districts and apartment types over time looks as follows:

In [4]:
query = """
SELECT ?time ?place ?rooms ?price
  ssz:QMP-EIG-HAA-OBJ-ZIM a cube:Cube;
             cube:observationSet/cube:observation ?observation.   
  ?observation property:TIME ?time ;
                       property:RAUM ?place_uri;
                       property:ZIM/schema:name ?rooms;
                       measure:QMP ?price .
  ?place_uri skos:inScheme <> ;
         schema:name ?place .
  FILTER regex(str(?place),"ab|Stadtgebiet vor")
  FILTER (?price > 0)
ORDER BY ?time

df = sparql.send_query(query)
time place rooms price
0 2009-12-31 Kreis 10 (ab 1934) 4- und 4,5-Zimmer Wohnung 9306.0
1 2009-12-31 Kreis 2 (ab 1893) 1- und 1,5-Zimmer Wohnung 12176.0
2 2009-12-31 Kreis 3 (ab 1915) 1- und 1,5-Zimmer Wohnung 7894.0
3 2009-12-31 Kreis 10 (ab 1934) 6- und 6,5-Zimmer Wohnung 9463.0
4 2009-12-31 Kreis 9 (ab 1934) 2- und 2,5-Zimmer Wohnung 7980.0

Prices per apartment type

Let's visualize the housing prices per apartment type. To do this, we will aggregate the prices per rooms.
The cleaned dataframe becomes:

In [5]: = x: re.findall('Kreis \d+', x)[0])
df.rooms = df.rooms.apply(lambda x: int(re.findall('\d+', x)[0]))
plot_df = df[["rooms", "price"]][df.time == df.time.max()].groupby(["rooms"]).mean().astype(int).sort_values(by="rooms").reset_index()
rooms price
0 1 12706
1 2 12841
2 3 12618
3 4 12138
4 5 13792
5 6 13361
6 7 12146
7 8 20804
In [6]:
fig =, x="rooms", y="price")
    title='Housing prices in Zürich', 
    yaxis_title="CHF per m<sup>2</sup>",