Published
Edited
Feb 27, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
z = require('https://bundle.run/zebras@0.0.11')
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rainAmounts = [
18.3,
19,
14.1
]
Insert cell
rainDates = Array.from({length: 3}, (_, i) => Date.now() - 1000 * 60 * 60 * 24 * i)
Insert cell
Insert cell
newRainfall = {
let rainArray = []
for (var i = 0; i < rainAmounts.length; i++) {
let currRow = {rainAmount: rainAmounts[i],
date: rainDates[i]
}
rainArray.push(currRow) //appends the currRow just created onto the end of the table rainArray
//there is also option to specify an index you want it to go to if you don't want it at the end
}
return rainArray
}
Insert cell
//another way to do the same as above
rainfall = {
let rainDF = []
for (var i=0; i<rainAmounts.length; i++) {
rainDF.push({precipitation: rainAmounts[i], date: rainDates[i]}) //same as block above but you push directly
}
return rainDF
}
Insert cell
md`### 1.3 Simple Zebras Methods

Say we want to add a column, its easy. Data in this new table can be manipulated, queried, sorted, and munged. This would not transform the object in place. Instead, it returns a NEW OBJECT that contains the transformation that was just performed.
`
Insert cell
daySeries = ["Monday", "Tuesday", "Wednesday"]
Insert cell
addedCol = z.addCol("Day of the Week", daySeries, newRainfall)
//assign to new df so that the transformation is actually stored and accessible
Insert cell
newRainfall
//note that newRainfall was not modified by the column addition above
Insert cell
z.addCol("day", daySeries, rainfall) //day names the unnamed 3rd column
Insert cell
Insert cell
columns = Object.keys(rainfall[0])
Insert cell
Insert cell
shape = ({rows: rainfall.length, columns:Object.keys(rainfall[0]).length})
Insert cell
Insert cell
Insert cell
migrationUrl = "https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/90951b60444376eebfcbbee9beca00c083f489f6/API_SM.POP.NETM_DS2_en_csv_v2_10473747.csv"
Insert cell
migration = d3.csv(migrationUrl)
Insert cell
Insert cell
Insert cell
Insert cell
z.head(5, migration)
Insert cell
Insert cell
Insert cell
migration.length
Insert cell
Insert cell
({rows: migration.length, columns:Object.keys(migration[0]).length})
Insert cell
Insert cell
Insert cell
Object.values(migration[5])
Insert cell
Object.keys(migration[5])
Insert cell
firstRow = Object.values(migration[0])
Insert cell
lastRow = Object.values(migration[migration.length-1])
Insert cell
randomRow = Object.values(migration[Math.floor(Math.random() * migration.length)])
Insert cell
Insert cell
migrationColumns = Object.keys(migration[0])
Insert cell
Insert cell
numericalColumns = migrationColumns.slice(0, migrationColumns.length-4)
//this gives you the column NAMES of your desired columns, not the columns themselves
Insert cell
migrationParsed = z.parseNums(numericalColumns, migration)
//parseNums takes as parameters the NAMES of the columns you want to parse, and the df name
//therefore the result is the full migrations dataframe, with numbers parsed and strings still as strings
//this returns A NEW DF, not changes to the old one
Insert cell
Insert cell
z.head(5, migrationParsed)
Insert cell
Insert cell
countryName = z.getCol('Country Name', migrationParsed)
Insert cell
Insert cell
z.describe(z.getCol('1962', migrationParsed))
//why are these descriptive stats returned in strings? How do we parse them into #s for actual use?
Insert cell
Insert cell
z.valueCounts(z.getCol('Indicator Code', migrationParsed))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
z.print([
{
minReturn: z.min(dailyReturns).toFixed(5),
maxReturn: z.max(dailyReturns).toFixed(5),
meanDailyReturn: z.mean(dailyReturns).toFixed(5),
medianDailyReturn: z.median(dailyReturns).toFixed(5),
dailyVolatility: z.std(dailyReturns).toFixed(5)
}
])
Insert cell
Insert cell
vegalite({
data: {values: dataRolling},
width: 700,
height: 350,
mark: {type: "line", strokeWidth: .9},
encoding: {
x: {field: "Date", type: "temporal"},
y: {field: "rollingVolatility", type: "quantitative", scale: {type:'linear'}}
}
})
Insert cell
Insert cell
Insert cell
Insert cell
dates = z.deriveCol(r=>Date.parse(r.Date), dataWithReturns)
Insert cell
dataDates = z.addCol('DateStamp', dates, dataWithReturns)
Insert cell
days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
Insert cell
daysOfWeek = z.deriveCol(x=>days[(new Date(x.DateStamp)).getDay()], dataDates)
Insert cell
dataDays = z.addCol('Day', daysOfWeek, dataDates)
Insert cell
dataGrouped = z.groupBy(x=>x.Day, dataDays)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dataPiped = z.pipe(
[
z.parseNums(['Open','High','Low','Close', 'Adj Close', 'Volume']),
z.getCol('Close'),
z.pctChange(),
z.mean()
]
)(data)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csv("https://gist.githubusercontent.com/nickslevine/b518a5fc788099d8955e77b4a4dc0628/raw/60edf99d70141a44c05fc73dff758c219f1684b4/sandp.csv")
Insert cell
dataParsed = z.parseNums(['Open','High','Low','Close', 'Adj Close', 'Volume'], data)
Insert cell
vegalite = require("@observablehq/vega-lite")
Insert cell
Insert cell
prices = z.getCol('Close', dataParsed)
Insert cell
dailyReturns = z.pctChange(prices)
Insert cell
dataWithReturns = z.addCol('DailyReturns', dailyReturns, dataParsed)

Insert cell
dataSorted = z.sortByCol('DailyReturns', 'asc', dataWithReturns)
Insert cell
rollingVolatility = z.rolling(z.std, 252, dailyReturns)
Insert cell
rollingMean = z.rolling(z.mean, 252, dailyReturns)
Insert cell
dataRolling = z.pipe([
z.addCol('rollingVolatility', rollingVolatility),
z.addCol('rollingMean', rollingMean),
])(dataWithReturns)
Insert cell
annualisedVolatility = z.deriveCol((x=>x.rollingVolatility * Math.sqrt(252)), dataRolling)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more