Public
Edited
Mar 5, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// We create a new SimpleData instance.
cityInfo = new SimpleData()
// We call the method loadDataFromUrl.
.loadDataFromUrl({
// We pass the URL to the city-info.csv file.
url: "https://raw.githubusercontent.com/nshiab/cities-temperature-example/main/output/cities-for-observable/cities-info.csv"
})
Insert cell
Insert cell
cityInfo.showTable()
Insert cell
Insert cell
citiesDataRaw = new SimpleData().loadDataFromUrl({
url: [
"https://raw.githubusercontent.com/nshiab/cities-temperature-example/main/output/cities-for-observable/1.csv",
"https://raw.githubusercontent.com/nshiab/cities-temperature-example/main/output/cities-for-observable/2.csv",
"https://raw.githubusercontent.com/nshiab/cities-temperature-example/main/output/cities-for-observable/3.csv"
], // We pass a list of URLs with data structured the same way.
autoType: true // autoType to true will parse numbers and dates. CSV files are just text.
})
Insert cell
The cell above returns a SimpleData instance with ${citiesDataRaw.getLength()} items in it. Let's see the data as a table.
Insert cell
citiesDataRaw.showTable()
Insert cell
Insert cell
Insert cell
citiesDataRaw // No need to create a variable. We just want to see the table.
.clone() // We clone the SimpleData instance to avoid overwriting citiesDataRaw.
.checkValues({ nbItemsToCheck: 25000, randomize: true }) // Checking values could be costly. So we limit the nbItemsToCheck, and pick them randomly.
.showTable() // Let's see the result.
Insert cell
Insert cell
citiesDataCleaned = citiesDataRaw
.clone()
.keepNumbers({ key: "tmax" }) // This comment is useless. Everything is so clear when using a SimpleData instance.
.valuesToString({ key: "id" })
Insert cell
Insert cell
citiesDataCleaned
.clone()
.checkValues({ nbItemsToCheck: 25000, randomize: true })
.showTable()
Insert cell
Insert cell
citiesDataMerged = citiesDataCleaned
.clone()
// mergeItems works as a left join.
.mergeItems({
dataToBeMerged: cityInfo, // We pass the cityInfo as the data to be merged.
commonKey: "id" // We use the key id to match items in both instances.
})
Insert cell
Insert cell
citiesDataMerged.showTable()
Insert cell
Insert cell
citiesDataSelected = citiesDataMerged
.clone()
.selectKeys({ keys: ["date", "tmax", "name"] }) // In real-world case use, you would chain this right after merging. No need to do an extra cell. But, again, in this notebook, I want you to understand each step.
Insert cell
citiesDataSelected.showTable()
Insert cell
Insert cell
citiesDataNewKeys = citiesDataSelected
.clone()
.addKey({ key: "year", itemGenerator: (item) => item.date.getFullYear() }) // itemGenerator is a function that will be applied to each item.
.addKey({
key: "decade",
itemGenerator: (item) => Math.floor(item.year / 10) * 10
})
Insert cell
citiesDataNewKeys.showTable()
Insert cell
Insert cell
citiesDataTidy = citiesDataNewKeys
.clone()
// Like a pivot_longer() in R or wide_to_long() with Pandas
.keysToValues({
keys: ["year", "decade"],
newKeyForKeys: "timeframe",
newKeyForValues: "timeframeValue"
})
Insert cell
citiesDataTidy.showTable()
Insert cell
Insert cell
citiesDataSummarized = citiesDataTidy.summarize({
// tmax as the value that will be summarized
keyValue: "tmax",
// Summarize tmax for each city and each timeframe.
keyCategory: ["name", "timeframe", "timeframeValue"],
// Compute the mean, but also count the number of days per year or decade.
summary: ["mean", "count"]
})
Insert cell
citiesDataSummarized.showTable()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dataForCityChart.getChart({
x: "timeframeValue",
y: "mean",
color: "mean",
type: "dot",
trend: true,
showTrendEquation: true,
title: city
})
Insert cell
dataForCityChart.showTable({ nbItemsInTable: 5 })
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