# Simple invocation of Wikidata SPARQL query
# Based on http://ramiro.org/notebook/us-presidents-causes-of-death/

import requests
import pandas as pd

query = '''
SELECT ?president ?cause ?dob ?dod WHERE {
    ?pid wdt:P39 wd:Q11696 .
    ?pid wdt:P509 ?cid .
    ?pid wdt:P569 ?dob .
    ?pid wdt:P570 ?dod .

    OPTIONAL {
        ?pid rdfs:label ?president filter (lang(?president) = "en") .
    }
    OPTIONAL {
        ?cid rdfs:label ?cause filter (lang(?cause) = "en") .
    }
}
'''
url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'
data = requests.get(url, params={'query': query, 'format': 'json'}).json()
presidents = []
for item in data['results']['bindings']:
    presidents.append({
        'name': item['president']['value'],
        'cause of death': item['cause']['value'],
        'date of birth': item['dob']['value'],
        'date of death': item['dod']['value']})

df = pd.DataFrame(presidents)
print(len(df))
df
49
cause of death date of birth date of death name
0 influenza 1833-08-20T00:00:00Z 1901-03-13T00:00:00Z Benjamin Harrison
1 Alzheimer's disease 1911-02-06T00:00:00Z 2004-06-05T00:00:00Z Ronald Reagan
2 cholera 1784-11-24T00:00:00Z 1850-07-09T00:00:00Z Zachary Taylor
3 cholera 1795-11-02T00:00:00Z 1849-06-15T00:00:00Z James K. Polk
4 Parkinson disease 1924-06-12T00:00:00Z 2018-11-30T00:00:00Z George H. W. Bush
5 stroke 1829-10-05T00:00:00Z 1886-11-18T00:00:00Z Chester A. Arthur
6 stroke 1856-12-28T00:00:00Z 1924-02-03T00:00:00Z Woodrow Wilson
7 stroke 1800-01-07T00:00:00Z 1874-03-08T00:00:00Z Millard Fillmore
8 stroke 1790-03-29T00:00:00Z 1862-01-18T00:00:00Z John Tyler
9 stroke 1913-01-09T00:00:00Z 1994-04-22T00:00:00Z Richard Nixon
10 stroke 1808-12-29T00:00:00Z 1875-07-31T00:00:00Z Andrew Johnson
11 pneumonia 1833-08-20T00:00:00Z 1901-03-13T00:00:00Z Benjamin Harrison
12 pneumonia 1782-12-05T00:00:00Z 1862-07-24T00:00:00Z Martin Van Buren
13 pneumonia 1884-05-08T00:00:00Z 1972-12-26T00:00:00Z Harry S. Truman
14 pneumonia 1911-02-06T00:00:00Z 2004-06-05T00:00:00Z Ronald Reagan
15 tuberculosis 1767-03-15T00:00:00Z 1845-06-08T00:00:00Z Andrew Jackson
16 tuberculosis 1758-04-28T00:00:00Z 1831-07-04T00:00:00Z James Monroe
17 liver cirrhosis 1804-11-23T00:00:00Z 1869-10-08T00:00:00Z Franklin Pierce
18 gastroenteritis 1784-11-24T00:00:00Z 1850-07-09T00:00:00Z Zachary Taylor
19 heart failure 1857-09-15T00:00:00Z 1930-03-08T00:00:00Z William Howard Taft
20 heart failure 1865-11-02T00:00:00Z 1923-08-02T00:00:00Z Warren G. Harding
21 heart failure 1782-12-05T00:00:00Z 1862-07-24T00:00:00Z Martin Van Buren
22 heart failure 1751-03-16T00:00:00Z 1836-06-28T00:00:00Z James Madison
23 heart failure 1735-10-30T00:00:00Z 1826-07-04T00:00:00Z John Adams
24 sepsis 1773-02-09T00:00:00Z 1841-04-04T00:00:00Z William Henry Harrison
25 arteriosclerosis 1913-07-14T00:00:00Z 2006-12-26T00:00:00Z Gerald Ford
26 myocardial infarction 1872-07-04T00:00:00Z 1933-01-05T00:00:00Z Calvin Coolidge
27 myocardial infarction 1822-10-04T00:00:00Z 1893-01-17T00:00:00Z Rutherford B. Hayes
28 myocardial infarction 1865-11-02T00:00:00Z 1923-08-02T00:00:00Z Warren G. Harding
29 myocardial infarction 1837-03-18T00:00:00Z 1908-06-24T00:00:00Z Grover Cleveland
30 myocardial infarction 1890-10-14T00:00:00Z 1969-03-28T00:00:00Z Dwight D. Eisenhower
31 myocardial infarction 1908-08-27T00:00:00Z 1973-01-22T00:00:00Z Lyndon B. Johnson
32 colorectal cancer 1874-08-10T00:00:00Z 1964-10-20T00:00:00Z Herbert Hoover
33 aneurysm 1831-11-19T00:00:00Z 1881-09-19T00:00:00Z James A. Garfield
34 bacteremia 1743-04-13T00:00:00Z 1826-07-04T00:00:00Z Thomas Jefferson
35 coronary thrombosis 1872-07-04T00:00:00Z 1933-01-05T00:00:00Z Calvin Coolidge
36 coronary thrombosis 1858-10-27T00:00:00Z 1919-01-06T00:00:00Z Theodore Roosevelt
37 multiple organ dysfunction syndrome 1884-05-08T00:00:00Z 1972-12-26T00:00:00Z Harry S. Truman
38 epiglottitis 1732-02-22T00:00:00Z 1799-12-14T00:00:00Z George Washington
39 cerebral hemorrhage 1767-07-11T00:00:00Z 1848-02-23T00:00:00Z John Quincy Adams
40 cerebral hemorrhage 1882-01-30T00:00:00Z 1945-04-12T00:00:00Z Franklin Delano Roosevelt
41 respiratory failure 1791-04-23T00:00:00Z 1868-06-01T00:00:00Z James Buchanan
42 laryngeal cancer 1822-04-27T00:00:00Z 1885-07-23T00:00:00Z Ulysses S. Grant
43 ballistic trauma 1843-01-29T00:00:00Z 1901-09-14T00:00:00Z William McKinley
44 ballistic trauma 1831-11-19T00:00:00Z 1881-09-19T00:00:00Z James A. Garfield
45 ballistic trauma 1917-05-29T00:00:00Z 1963-11-22T00:00:00Z John F. Kennedy
46 ballistic trauma 1809-02-12T00:00:00Z 1865-04-15T00:00:00Z Abraham Lincoln
47 internal bleeding 1874-08-10T00:00:00Z 1964-10-20T00:00:00Z Herbert Hoover
48 uremia 1743-04-13T00:00:00Z 1826-07-04T00:00:00Z Thomas Jefferson