Skip to content

Notebook display

What a frame prints, and the HTML table a notebook shows, as go-dataframe v0.1.0 produced them.

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    70

Shown in a notebook (text/html):

cityyearraindays
0Lyon2022741.298
1Paris2022512.9103
2Nice2022698.461
3Lyon2023830.5112
4Paris2023640.2118
5Nice2023702.364
6Lyon2024810.0107
7Paris2024598.7111
8Nice2024760.170

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   332

Shown in a notebook (text/html):

rain_meanrain_maxdays
city
Lyon793.900000830.5317
Nice720.266667760.1195
Paris583.933333640.2332

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.000000

Shown in a notebook (text/html):

yearraindays
count9.0000009.0000009.000000
mean2023.000000699.36666793.777778
std0.866025102.03435722.415273
min2022.000000512.90000061.000000
25%2022.000000640.20000070.000000
50%2023.000000702.300000103.000000
75%2024.000000760.100000111.000000
max2024.000000830.500000118.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