Public
Edited
Jan 24, 2023
Insert cell
Insert cell
Insert cell
Insert cell
// table(dict) เป็น native, column-based
dt = aq.table({
u: ['a', 'a', 'b', 'b'],
x: Int32Array.of(1, 2, 3, 4), // read-only
y: Float64Array.of(5, 6, 7, 8)
})
Insert cell
dt.view() // เทียบกับ ._repr_html_() ของ python
Insert cell
[dt.numCols(), dt.numRows()]
Insert cell
dt._nrows // private
Insert cell
dt.columnNames()
Insert cell
dt.get('y', 2) // [col, row]
Insert cell
dt.array('y') // dt.y
Insert cell
// export as row-based
dt.objects()
Insert cell
{ // iterate as rows
let uu = ''
for (let row of dt) {
uu += row.u
}
return uu
}
Insert cell
[...dt] // spread as rows
Insert cell
// from() จะ copy ต่างจาก table() ที่ไม่ copy
aq.from([{a: 1, b: 0.2}, {a: 4, b: 0.3}])
.view()
Insert cell
// หัด object shorthand
{
let a = 'A'
let b = 'B'
return {a, b} // {a:a, {b:b}
}
Insert cell
// ถ้า input เป็น dict จะได้ 2 columns
aq.from({a:1, b:2, c:3})
.view()
Insert cell
// map ก็ด้วย
aq.from(new Map([['d', 4], ['e', 5], ['f', 6]]))
.view()
Insert cell
// Arrow Format ดีที่ efficient ไม่ต้อง copy
flights = aq.loadArrow('https://vega.github.io/vega-datasets/data/flights-200k.arrow')
Insert cell
flights.view({ limit: 5 }) // or view(5)
Insert cell
// JSON รับ dict ของ colname หรือ json string ก็ได้
aq.fromJSON('{"id":[1,2],"date":["2020-06-01","2020-09-01"]}')
.view()
Insert cell
// ใช้ table() กับ JSON.parse จะไม่แปลง date
aq.table(JSON.parse(
'{"id":[1,2],"date":["2020-06-01","2020-09-01"]}'
)).view()
// dt.toJSON() จะแปลงกลับเป็น string
// aq.loadJSON() จะทั้ง load url และ parse
Insert cell
Insert cell
// ใช้แบบปกติ
FileAttachment("beer.csv").csv()
Insert cell
Insert cell

// load และแสดงผลพร้อมกัน ด้วย viewof
viewof beers = aq // viewof shows the table view, but assigns the table value
.fromCSV(await FileAttachment('beer.csv').text())
.view({ height: 240 }) // set maximum height of table viewer in pixels
Insert cell
beer_csv = FileAttachment('beer.csv')
Insert cell
aq.fromCSV(await beer_csv.text())
Insert cell
beers.view(10) // ถ้าจะ scroll ใช้ {height: 200}
Insert cell
Insert cell
beers
Insert cell
// orderby
beers
.orderby('abv') // least alcoholic
.view(5)
Insert cell
// desc เพื่อหาที่มากสุด
beers
.orderby(aq.desc('abv')) // most ลดลง
.view(5)
Insert cell
// select เพื่อเลือก col เทียบ .array()
beers
.orderby(aq.desc('ibu')) // ความขม
.select('name', 'style', 'ibu') // [..] ก็ได้
.view(5)
Insert cell
// all(), not(...), range(start, stop)
beers
.orderby(aq.desc('ibu'))
.select(aq.not('brewery_id')) // ไม่เอาอันนี้
.view(5)
Insert cell
Insert cell
// filter เพื่อเลือก row
// ใช้ op.includes เพื่อหา substr
// แต่ละ row จะอ้างถึง d.colname ได้
beers
.filter(d=> op.includes(op.lower(d.name), 'hop'))
.view({height: 200})
Insert cell
Insert cell
// derive สร้าง col ใหม่
// สร้างเป็น table ใหม่ เพิ่ม col ใหม่ (หรือแทนที่ถ้าซ้ำ)
// นิยาม intensity = 3 * abv + 1/3 * log10(ibu)
beers
.derive({intensity:
d=> 3*d.abv + op.log10(d.ibu)/3
}).orderby(aq.desc('intensity'))
.view(10)
Insert cell
// rollup, groupby คล้ายๆ d3
// คล้าย derive แต่ต้องเป็น agg op
beers
.rollup({
mean_abv: d=>op.mean(d.abv),
mean_ibu: d=>op.mean(d.ibu),
mean_intensity: d=>
op.mean(3*d.abv + op.log10(d.ibu)/3),
corr_abv_ibu: d=> op.corr(d.abv, d.ibu),
count: d=>op.count()
}).
view()
Insert cell
// groupby แยกคำนวณให้แต่ละกลุ่ม
beers
.groupby('style')
.rollup({
mean_abv: d => op.mean(d.abv),
mean_ibu: d => op.mean(d.ibu),
mean_intensity: d =>
op.mean(3 * d.abv + op.log10(d.ibu) / 3),
count: op.count()
})
.filter(d=> d.count > 20) // ข้ามที่ข้อมูลน้อย
.orderby(aq.desc('mean_intensity'))
.view(10)
Insert cell
// count() ใช้บ่อย แทน rollup + op.count()
beers
.groupby('style')
.count()
.view(10)
Insert cell
// group ตามสูตรคำนวณก็ได้
beers.groupby({ type: d=> op.lower(d.style)})
Insert cell
// window op ก็มี แต่ข้ามไปละกัน
Insert cell
Insert cell
// brewery_id โยงไปที่ข้อมูลในอีก table
Insert cell
viewof breweries = aq
.fromCSV(await FileAttachment("breweries.csv").text())
.view(10)
Insert cell
[breweries.numCols(), breweries.numRows()]
Insert cell
// join ง่ายๆ ให้มันฉลาดหา col เองจากชื่อ col
// จากนั้นมันก็เอามาเติมให้หมด
// default เป็น inner join
beers
.join(breweries)
.view(5)
Insert cell
beers
.join(breweries, 'brewery_id')
// หรือเต็มๆ ว่า ['brewery_id', 'brewery_id']
.view(5)
Insert cell
// join ดึงมาแค่บาง column เขียนแบบเต็ม
beers
.join(breweries, 'brewery_id',
[aq.all(), aq.not('brewery_id')])
.view(5)
Insert cell
// แต่ละ style มีกี่ brewery ไม่นับซ้ำ
beers
.join(breweries, 'brewery_id')
.groupby('style')
.rollup({ breweries: op.distinct('brewery_id') } )
.orderby(aq.desc('breweries'))
.view(10)
Insert cell
Insert cell
// semijoin เอาเฉพาะที่ match
// antijoin เอาเฉพาะที่ไม่ match
Insert cell
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