Members of Parliament

Swiss Parliament: Members

The Federal Chancellery maintains data on the Swiss political system. The curia dataset is publicly available and it provides data on political parties, parliamentary comissions, members of parliament and their affiliations.

Parliament data is also available as Linked Data.

Setup

SPARQL endpoint

Swiss political data can be accessed with SPARQL queries.
You can send queries using HTTP requests. The API endpoint is https://lindas.admin.ch/query/.

SPARQL client

We will use the 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]:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import datetime
import warnings

from graphly.api_client import SparqlClient

%matplotlib inline
pd.set_option('display.max_rows', 100)
warnings.filterwarnings('ignore')
In [2]:
# Uncomment to install dependencies in Colab environment
#!pip install git+https://github.com/zazuko/graphly.git
In [3]:
sparql = SparqlClient("https://int.lindas.admin.ch/query")

sparql.add_prefixes({
    "schema": "<http://schema.org/>"
})

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

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

MEP by gender, age and chamber

In [4]:
# https://s.zazuko.com/5nEJGu
query = """
SELECT ?council ?name ?gender ?age ?start
FROM <https://lindas.admin.ch/fch/curia>
FROM <https://lindas.admin.ch/fch/rvov>
WHERE {
  
  VALUES ?_council {<https://politics.ld.admin.ch/council/N> <https://politics.ld.admin.ch/council/S>}
  ?member a schema:Person ;
         schema:name ?name;
         schema:gender ?gender;
         schema:birthDate ?birthday;
         schema:memberOf ?role.
  
  ?role a schema:Role;
        schema:member ?member;
        schema:startDate ?start;
        schema:memberOf ?_council.
  
  ?_council schema:name ?council.
  
       
  FILTER (lang(?council) = "de")
  FILTER NOT EXISTS { ?role schema:endDate ?end }
  
  BIND( "2022-06-16"^^<http://www.w3.org/2001/XMLSchema#date> as ?today )
  BIND(YEAR(?today)-YEAR(?birthday) as ?age)
}
ORDER BY ?age
"""

df = sparql.send_query(query)
df = df.replace({"http://schema.org/Male": "Male", "http://schema.org/Female": "Female"})
In [5]:
bucket=5
df["bucket"] = df.age.apply(lambda x: "{}-{}".format((x//bucket)*bucket, (x//bucket+1)*bucket))
counts = df.groupby(["bucket", "gender"]).size().reset_index(name='count')

fig = px.bar(counts, x="bucket", y="count", color="gender",
             barmode="stack", color_discrete_sequence=[px.colors.qualitative.Plotly[1],px.colors.qualitative.Plotly[0]])

fig.update_layout(
    title='Members of Parliament (as of today)', 
    title_x=0.5,
    yaxis_title="Share in %",
    xaxis_title="Age"
)
fig.show()