Public
Edited
Aug 20, 2024
3 forks
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
medals = d3
.csv(
`https://raw.githubusercontent.com/taniki/paris2024-data/main/datasets/medal_countries.long.auto.csv`
)
.then((d) => aq.from(d))
Insert cell
Inputs.table(medals, { layout: "auto" })
Insert cell
Insert cell
query = `SELECT DISTINCT ?item ?itemLabel ?country ?countryLabel ?code WHERE {
VALUES ?item {
wd:Q55643
wd:Q48
wd:Q15
wd:Q18
wd:Q49
wd:Q46
}
?item (wdt:P527*) ?country.
?country
wdt:P984 ?code.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}`
Insert cell
query_old = `SELECT DISTINCT ?item ?itemLabel ?country ?countryLabel ?code WHERE {
?item wdt:P31 wd:Q5107;
(wdt:P527*) ?country.
?country
wdt:P984 ?code.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}`
Insert cell
output = fetch(
`https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}`,
{ headers: { accept: "application/sparql-results+json" } }
).then((response) => response.json())
Insert cell
countries = aq.from(
output.results.bindings.map((d) => ({
item: d.item.value,
itemLabel: d.itemLabel.value,
country: d.country.value,
countryLabel: d.countryLabel.value,
code: d.code.value
}))
)
Insert cell
viewof continent = Inputs.select(
countries.dedupe(["item", "itemLabel"]).array("itemLabel")
)
Insert cell
Insert cell
Inputs.table(countries.filter(aq.escape((d) => d.itemLabel == continent)), {
columns: ["itemLabel", "code", "countryLabel"],
layout: "auto"
})
Insert cell
Insert cell
missing_countries = medals
.antijoin(countries, ["code", "code"])
.dedupe(["code", "country"])
.select(["code", "country"])
Insert cell
Insert cell
Inputs.table(missing_countries)
Insert cell
Insert cell
Inputs.table(
countries
.groupby(["code", "country", "countryLabel"])
.derive({ count: (d) => d.count() })
.filter((d) => d.count > 1)
.orderby("code"),
{
columns: ["itemLabel", "code", "countryLabel"],
layout: "auto"
}
)
Insert cell
Insert cell
medals_by_countries = medals
.join_left(countries, ["code", "code"])
.derive({
item2: (d) => `<a href="${d.item}"> ${d.itemLabel}`,
country2: (d) => `<a href="${d.country_2}"> ${d.countryLabel}`
})
Insert cell
medals_by_continent = medals_by_countries
.groupby(["item", "itemLabel", "medal"])
.rollup({ n_countries: op.count(), medals: (d) => op.sum(d.quantity) })
.derive({ item2: (d) => `<a href="${d.item}"> ${d.itemLabel}` })
Insert cell
Insert cell
Insert cell
Insert cell
import { aq, op } from "@uwdata/arquero"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more