Pandas Style#
Starting with itables>=1.6.0
, ITables provides support for
Pandas Style.
Warning
Pandas Style object use HTML. Please make sure that you trust the content of your table before
passing allow_html=True
.
Note
Unlike Pandas or Polar DataFrames, Styler
objects are rendered directly using their
to_html
method, rather than passing the underlying table data to the DataTables
library.
Because of this, the rendering of the table might differ slightly from the rendering of the corresponding DataFrame. In particular,
The downsampling is not available - please pay attention to the size of the table being rendered
Sorting of numbers will not work if the column contains NaNs.
import numpy as np
import pandas as pd
import itables
itables.init_notebook_mode()
# Before you do this, make sure that you trust the content of your tables
itables.options.allow_html = True
This is the DataFrame that we are going to style:
x = np.linspace(0, np.pi, 21)
df = pd.DataFrame({"sin": np.sin(x), "cos": np.cos(x)}, index=pd.Index(x, name="alpha"))
df
Loading ITables v2.4.0 from the init_notebook_mode cell...
(need help?) |
Color#
From now on we will display df.style
(a Pandas Styler
object) rather than our DataFrame df
.
Let’s start with a background gradient:
s = df.style
s.background_gradient(axis=None, cmap="YlOrRd")
Loading ITables v2.4.0 from the init_notebook_mode cell...
(need help?) |
Format#
We can also choose how the data is formatted:
s.format("{:.3f}").format_index("{:.3f}")
Loading ITables v2.4.0 from the init_notebook_mode cell...
(need help?) |
Caption#
s.set_caption("A Pandas Styler object with background colors").set_table_styles(
[{"selector": "caption", "props": "caption-side: bottom; font-size:1em;"}]
)
Loading ITables v2.4.0 from the init_notebook_mode cell...
(need help?) |
Tooltips#
ttips = pd.DataFrame(
{
"sin": ["The sinus of {:.6f} is {:.6f}".format(t, np.sin(t)) for t in x],
"cos": ["The cosinus of {:.6f} is {:.6f}".format(t, np.cos(t)) for t in x],
},
index=df.index,
)
s.set_tooltips(ttips).set_caption("With tooltips")
Loading ITables v2.4.0 from the init_notebook_mode cell...
(need help?) |