Notebook display
DataFrame, Series and Index have Display(), which returns the value in
every format a notebook front end may show, keyed by MIME type: text/plain
(what String prints, laid out as pandas prints it) and text/html (the
table pandas shows in a notebook). It is the display contract of the
go-notebook kernel, so a cell whose
last expression is a frame shows it as a table.
Every output on this page was produced by go-dataframe v0.1.0 from the code
shown above it: figures/<name>.go in this site’s repository writes it, and
the CI writes it again and fails if one byte differs.
A frame
The table the examples share, built from Go slices:
package main
func init() { registerOutput("display", display) }
// What a notebook cell whose value is a frame shows: Display returns both
// MIME types, text/plain (String) and text/html (HTML).
func display() (string, string) {
df := rainfall()
d := df.Display()
return string(d["text/plain"]), string(d["text/html"])
}Printed (text/plain):
city year rain days
0 Lyon 2022 741.2 98
1 Paris 2022 512.9 103
2 Nice 2022 698.4 61
3 Lyon 2023 830.5 112
4 Paris 2023 640.2 118
5 Nice 2023 702.3 64
6 Lyon 2024 810.0 107
7 Paris 2024 598.7 111
8 Nice 2024 760.1 70Shown in a notebook (text/html):
| city | year | rain | days | |
|---|---|---|---|---|
| 0 | Lyon | 2022 | 741.2 | 98 |
| 1 | Paris | 2022 | 512.9 | 103 |
| 2 | Nice | 2022 | 698.4 | 61 |
| 3 | Lyon | 2023 | 830.5 | 112 |
| 4 | Paris | 2023 | 640.2 | 118 |
| 5 | Nice | 2023 | 702.3 | 64 |
| 6 | Lyon | 2024 | 810.0 | 107 |
| 7 | Paris | 2024 | 598.7 | 111 |
| 8 | Nice | 2024 | 760.1 | 70 |
A group-by
package main
import "github.com/go-dataframe/dataframe"
func init() { registerOutput("groupby", groupby) }
// Group by city: the mean and the maximum yearly rainfall, and the rainy days
// summed over the three years.
func groupby() (string, string) {
g := dataframe.Must(rainfall().GroupBy("city"))
out := dataframe.Must(g.Agg(
dataframe.Agg("rain", "mean", "max"),
dataframe.Agg("days", "sum"),
))
return out.String(), out.HTML()
}Printed (text/plain):
rain_mean rain_max days
city
Lyon 793.900000 830.5 317
Nice 720.266667 760.1 195
Paris 583.933333 640.2 332Shown in a notebook (text/html):
| rain_mean | rain_max | days | |
|---|---|---|---|
| city | |||
| Lyon | 793.900000 | 830.5 | 317 |
| Nice | 720.266667 | 760.1 | 195 |
| Paris | 583.933333 | 640.2 | 332 |
Describe
package main
func init() { registerOutput("describe", describe) }
// df.describe(): count, mean, std, min, quartiles and max of each numeric column.
func describe() (string, string) {
d := rainfall().Describe()
return d.String(), d.HTML()
}Printed (text/plain):
year rain days
count 9.000000 9.000000 9.000000
mean 2023.000000 699.366667 93.777778
std 0.866025 102.034357 22.415273
min 2022.000000 512.900000 61.000000
25% 2022.000000 640.200000 70.000000
50% 2023.000000 702.300000 103.000000
75% 2024.000000 760.100000 111.000000
max 2024.000000 830.500000 118.000000Shown in a notebook (text/html):
| year | rain | days | |
|---|---|---|---|
| count | 9.000000 | 9.000000 | 9.000000 |
| mean | 2023.000000 | 699.366667 | 93.777778 |
| std | 0.866025 | 102.034357 | 22.415273 |
| min | 2022.000000 | 512.900000 | 61.000000 |
| 25% | 2022.000000 | 640.200000 | 70.000000 |
| 50% | 2023.000000 | 702.300000 | 103.000000 |
| 75% | 2024.000000 | 760.100000 | 111.000000 |
| max | 2024.000000 | 830.500000 | 118.000000 |
How the text layout and the HTML are produced, and where they follow pandas, is in Design.
Created on by tannevaled
Modified on by tannevaled