{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting sparqlwrapper\n", " Using cached SPARQLWrapper-1.8.0.zip\n", "Collecting rdflib>=4.0 (from sparqlwrapper)\n", " Using cached rdflib-4.2.2-py3-none-any.whl\n", "Requirement already satisfied: pyparsing in /home/sirex/.venvs/py36/lib/python3.6/site-packages (from rdflib>=4.0->sparqlwrapper)\n", "Collecting isodate (from rdflib>=4.0->sparqlwrapper)\n", " Using cached isodate-0.5.4.tar.gz\n", "Building wheels for collected packages: sparqlwrapper, isodate\n", " Running setup.py bdist_wheel for sparqlwrapper ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /home/sirex/.cache/pip/wheels/3c/b6/36/90a0877c0a7ac2933a16c446b535af632831b3fd692e1910fe\n", " Running setup.py bdist_wheel for isodate ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /home/sirex/.cache/pip/wheels/61/c0/d2/6b4a10c222ba9261ab9872a8f05d471652962284e8c677e5e7\n", "Successfully built sparqlwrapper isodate\n", "Installing collected packages: isodate, rdflib, sparqlwrapper\n", "Successfully installed isodate-0.5.4 rdflib-4.2.2 sparqlwrapper-1.8.0\n" ] } ], "source": [ "!pip install sparqlwrapper" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import matplotlib as mpl\n", "\n", "from pandas.io.json import json_normalize\n", "from SPARQLWrapper import SPARQLWrapper, JSON" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mpl.style.use('ggplot')\n", "mpl.rc('font', family='Ubuntu', size=16)\n", "mpl.rc('figure', figsize=(16, 10))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def select(query, service='https://query.wikidata.org/sparql'):\n", " sparql = SPARQLWrapper(service)\n", " sparql.setQuery(query)\n", " sparql.setReturnFormat(JSON)\n", " result = sparql.query().convert()\n", " return json_normalize(result['results']['bindings'])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data = select('''\n", "SELECT ?sLabel ?population ?s WHERE {\n", " ?s p:P31/ps:P31 wd:Q515 ;\n", " wdt:P17 wd:Q37 ;\n", " wdt:P1082 ?population .\n", " SERVICE wikibase:label { bd:serviceParam wikibase:language \"lt\". }\n", "}\n", "ORDER BY DESC(?population)\n", "''')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | 0 | \n", "
|---|---|
| population.datatype | \n", "http://www.w3.org/2001/XMLSchema#decimal | \n", "
| population.type | \n", "literal | \n", "
| population.value | \n", "532762 | \n", "
| s.type | \n", "uri | \n", "
| s.value | \n", "http://www.wikidata.org/entity/Q216 | \n", "
| sLabel.type | \n", "literal | \n", "
| sLabel.value | \n", "Vilnius | \n", "
| sLabel.xml:lang | \n", "lt | \n", "