The Federal Chancellery maintains data on the Swiss political system. The curia dataset is publicly available it provides data on political parties, parliamentary comissions, members of parliament and their affiliations.
Parliament data is also available as Linked Data.
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/.
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import warnings
from plotly.subplots import make_subplots
from graphly.api_client import SparqlClient
%matplotlib inline
pd.set_option('display.max_rows', 100)
warnings.filterwarnings('ignore')
# Uncomment to install dependencies in Colab environment
#!pip install git+https://github.com/zazuko/graphly.git
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.
query = """
SELECT ?council ?faction (COUNT(?mep) AS ?members)
FROM <https://lindas.admin.ch/fch/curia>
FROM <https://lindas.admin.ch/fch/rvov>
WHERE {
VALUES ?_council { <https://politics.ld.admin.ch/council/S> <https://politics.ld.admin.ch/council/N> <https://politics.ld.admin.ch/council/UFA>}
?mep a schema:Person;
schema:memberOf/schema:memberOf ?_council;
schema:memberOf ?role.
FILTER NOT EXISTS { ?role schema:endDate ?end }
?role schema:memberOf ?_faction.
?_faction a <http://schema.org/ParliamentaryGroup>;
schema:name ?faction.
?_council schema:name ?council.
FILTER (lang(?council) = 'de')
FILTER (lang(?faction) = 'de')
}
GROUP BY ?council ?faction
ORDER BY DESC(?council) DESC (?members)
"""
df = sparql.send_query(query)
N_FACTIONS=df.faction.nunique()
cmap = plt.get_cmap('YlOrRd')
colors = [mcolors.rgb2hex(cmap((i+1)/N_FACTIONS)) for i in range(N_FACTIONS)]
factions = [
"Grünliberale Fraktion",
"Grüne Fraktion",
"Die Mitte-Fraktion. Die Mitte. EVP.",
"Sozialdemokratische Fraktion",
"FDP-Liberale Fraktion",
"Fraktion der Schweizerischen Volkspartei"
]
colormap = dict(zip(factions,colors))
fig = make_subplots(rows=1, cols=3, subplot_titles=df["council"].unique(), specs=[[{"type": "pie"}, {"type": "pie"}, {"type": "pie"}]])
for i, council in enumerate(df["council"].unique()):
members = dict(zip(df[df.council == council]["faction"],df[df.council == council]["members"]))
fig.add_trace(go.Pie(
values=[members[f] if f in members else 0 for f in factions],
labels=factions, sort=False
), row=1, col=i+1)
fig.update_traces(textinfo='none', marker={"colors": [colormap[f] for f in factions]})
fig.update_annotations(yshift=-280)
fig.update_layout(height=400, title={"text": "Members of Parliament (as of today)", "x": 0.5})
fig.show()
query = """
SELECT ?council ?committee (COUNT(?mep) AS ?members)
FROM <https://lindas.admin.ch/fch/curia>
FROM <https://lindas.admin.ch/fch/rvov>
WHERE {
VALUES ?_council { <https://politics.ld.admin.ch/council/S> <https://politics.ld.admin.ch/council/N>}
?mep a schema:Person;
schema:memberOf/schema:memberOf ?_council;
schema:memberOf ?role.
FILTER NOT EXISTS { ?role schema:endDate ?end }
?role schema:memberOf ?_committee.
?_committee a <http://schema.org/ParliamentaryCommittee>;
schema:additionalType schema:MainCommittee;
schema:name ?committee.
?_council schema:name ?council.
FILTER (lang(?council) = 'de')
FILTER (lang(?committee) = 'de')
}
GROUP BY ?council ?committee
ORDER BY DESC (?members)
"""
df = sparql.send_query(query)
px.treemap(df, path=["council", "committee"], values="members")
The above chart shows the number of members per committee.
query = """
SELECT ?faction (COUNT(?mep) AS ?members)
FROM <https://lindas.admin.ch/fch/curia>
WHERE {
?mep a schema:Person;
schema:memberOf/schema:memberOf <https://politics.ld.admin.ch/council/UFA>;
schema:memberOf ?role.
FILTER NOT EXISTS { ?role schema:endDate ?end }
?role schema:memberOf ?_faction.
?_faction a <http://schema.org/ParliamentaryGroup>;
schema:name ?faction.
FILTER (lang(?faction) = 'de')
}
GROUP BY ?faction
ORDER BY DESC(?faction) DESC (?members)
"""
df_members = sparql.send_query(query)
query = """
SELECT ?faction (count(?_committee) as ?committees)
FROM <https://lindas.admin.ch/fch/curia>
WHERE {
?mep a schema:Person;
schema:memberOf ?ufa_role;
schema:memberOf ?committee_role;
schema:memberOf ?faction_role.
?ufa_role schema:memberOf <https://politics.ld.admin.ch/council/UFA>.
FILTER NOT EXISTS { ?ufa_role schema:endDate ?end }
?faction_role schema:memberOf ?_faction.
?committee_role schema:memberOf ?_committee.
?_faction a <http://schema.org/ParliamentaryGroup>;
schema:name ?faction.
?_committee a schema:ParliamentaryCommittee;
schema:additionalType schema:MainCommittee.
FILTER (lang(?faction) = 'de')
}
GROUP BY ?faction
ORDER BY DESC (?count)
"""
df_committees = sparql.send_query(query)
df = pd.merge(df_committees,df_members, on='faction', how='left')
df["metric"] = df["committees"]/df["members"]
df.sort_values(by="metric", ascending=False, inplace=True)
df
fig = px.bar(df, x="faction", y="metric", hover_data=["committees", "members"], labels={'metric':'Affiliations per member', "faction": "Faction", "committees": "Committee affiliations","members": "Members"})
fig.update_layout(
title='Committee Affiliations',
title_x=0.5,
yaxis_title="Affiliations per member",
xaxis_title=""
)
fig.show()
The above chart shows in how many committees members of the different parties are engaged in on average.