|
| 1 | +--- |
| 2 | +jupytext: |
| 3 | + formats: md:myst |
| 4 | + text_representation: |
| 5 | + extension: .md |
| 6 | + format_name: myst |
| 7 | + format_version: 0.13 |
| 8 | + jupytext_version: 1.14.5 |
| 9 | +kernelspec: |
| 10 | + display_name: itables |
| 11 | + language: python |
| 12 | + name: itables |
| 13 | +--- |
| 14 | + |
| 15 | +# Pandas Style |
| 16 | + |
| 17 | +Starting with `itables>=1.6.0`, ITables provides support for |
| 18 | +[Pandas Style](https://pandas.pydata.org/docs/user_guide/style.html). |
| 19 | + |
| 20 | +```{code-cell} |
| 21 | +import pandas as pd |
| 22 | +import numpy as np |
| 23 | +from itables import init_notebook_mode |
| 24 | +
|
| 25 | +init_notebook_mode(all_interactive=True) |
| 26 | +``` |
| 27 | + |
| 28 | +```{code-cell} |
| 29 | +:tags: [remove-input] |
| 30 | +
|
| 31 | +import itables.options as opt |
| 32 | +
|
| 33 | +opt.lengthMenu = [5, 10, 20, 50, 100, 200, 500] |
| 34 | +``` |
| 35 | + |
| 36 | +This is the DataFrame that we are going to style: |
| 37 | + |
| 38 | +```{code-cell} |
| 39 | +x = np.linspace(0, np.pi, 21) |
| 40 | +df = pd.DataFrame({"sin": np.sin(x), "cos": np.cos(x)}, index=pd.Index(x, name="alpha")) |
| 41 | +
|
| 42 | +df |
| 43 | +``` |
| 44 | + |
| 45 | +## Color |
| 46 | + |
| 47 | +From now on we will display `df.style` |
| 48 | +(a Pandas `Styler` object) rather than our DataFrame `df`. |
| 49 | + |
| 50 | +Let's start with a background gradient: |
| 51 | + |
| 52 | +```{code-cell} |
| 53 | +s = df.style |
| 54 | +s.background_gradient(axis=None, cmap="YlOrRd") |
| 55 | +``` |
| 56 | + |
| 57 | +## Format |
| 58 | + |
| 59 | +We can also choose how the data is formatted: |
| 60 | + |
| 61 | +```{code-cell} |
| 62 | +s.format("{:.3f}") |
| 63 | +``` |
| 64 | + |
| 65 | +## Caption |
| 66 | + |
| 67 | +```{code-cell} |
| 68 | +s.set_caption("A Pandas Styler object with background colors").set_table_styles( |
| 69 | + [{"selector": "caption", "props": "caption-side: bottom; font-size:1em;"}] |
| 70 | +) |
| 71 | +``` |
| 72 | + |
| 73 | +## Tooltips |
| 74 | + |
| 75 | +```{code-cell} |
| 76 | +ttips = pd.DataFrame( |
| 77 | + { |
| 78 | + "sin": ["The sinus of {:.6f} is {:.6f}".format(t, np.sin(t)) for t in x], |
| 79 | + "cos": ["The cosinus of {:.6f} is {:.6f}".format(t, np.cos(t)) for t in x], |
| 80 | + }, |
| 81 | + index=df.index, |
| 82 | +) |
| 83 | +s.set_tooltips(ttips).set_caption("With tooltips") |
| 84 | +``` |
| 85 | + |
| 86 | +```{note} |
| 87 | +Unlike Pandas or Polar DataFrames, `Styler` objects are rendered directly using their |
| 88 | +`to_html` method, rather than passing the underlying table data to the datatables.net |
| 89 | +library. |
| 90 | +
|
| 91 | +Because of this, the rendering of the table might differ slightly from the rendering of the |
| 92 | +corresponding DataFrame. In particular, |
| 93 | +- The downsampling is not available - please pay attention to the size of the table being rendered |
| 94 | +- Sorting of numbers will not work if the column contains NaNs. |
| 95 | +``` |
0 commit comments