Styling data frames
[2]:
import polars as pl
import polars.selectors as cs
import great_tables
It is sometimes useful to highlight features in a data frame when viewing them. (Note that this is generally far less useful than making informative plots, which we will come to shortly.) We sometimes also want to make a table for display. For this purpose, Polars works seamlessly with Great Tables package. To convert a Polars data frame to a Great Tables object that can then be stylized, simply use the df.style
attribute of a Polars data
frame. You can read about the endless possibilities for styling Polars data frames using Great Tables in the documentation.
Here, we will do a quick demonstration, again using a data set from Beattie, et al. containing results from a study the effects of sleep quality on performance in the Glasgow Facial Matching Test (GMFT). We will make a pretty-looking table also highlighting rows corresponding to women who scored at 90% or above.
[3]:
# Load data set
df = pl.read_csv(os.path.join(data_path, 'gfmt_sleep.csv'), null_values='*')
# Style the table
(
df.sort(by='participant number').style
.tab_options(table_font_size='x-small')
.tab_header(title='GFMT sleep study')
.tab_spanner('Participant data', ['participant number', 'gender', 'age'])
.tab_spanner('GFMT results', cs.contains('correct'))
.tab_spanner('Sleep indicators', ['sci', 'psqi', 'ess'])
.tab_style(
style=great_tables.style.fill('#99dbc9'),
locations=great_tables.loc.body(
rows=(pl.col('gender') == 'f')
& (pl.col('percent correct') >= 90)
),
)
)
[3]:
GFMT sleep study | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Participant data | GFMT results | Sleep indicators | ||||||||||||
participant number | gender | age | correct hit percentage | correct reject percentage | percent correct | confidence when correct hit | confidence incorrect hit | confidence correct reject | confidence incorrect reject | confidence when correct | confidence when incorrect | sci | psqi | ess |
1 | f | 42 | 80 | 65 | 72.5 | 51.5 | 44.5 | 43.0 | 49.0 | 51.0 | 49.0 | 29 | 1 | 5 |
2 | f | 45 | 80 | 90 | 85.0 | 75.0 | 55.5 | 80.0 | 75.0 | 78.5 | 67.0 | 19 | 5 | 1 |
3 | f | 16 | 70 | 80 | 75.0 | 70.0 | 57.0 | 54.0 | 53.0 | 57.0 | 54.5 | 23 | 1 | 3 |
4 | f | 21 | 70 | 65 | 67.5 | 63.5 | 64.0 | 50.0 | 50.0 | 60.0 | 50.0 | 26 | 5 | 4 |
5 | f | 18 | 90 | 100 | 95.0 | 76.5 | 83.0 | 80.0 | None | 80.0 | 83.0 | 21 | 7 | 5 |
6 | f | 28 | 95 | 80 | 87.5 | 100.0 | 85.0 | 94.0 | 61.0 | 99.0 | 65.0 | 19 | 7 | 12 |
7 | f | 38 | 90 | 95 | 92.5 | 77.0 | 43.5 | 79.0 | 21.0 | 78.0 | 36.0 | 28 | 3 | 4 |
8 | f | 39 | 65 | 80 | 72.5 | 91.0 | 90.0 | 93.0 | 83.5 | 93.0 | 90.0 | 9 | 13 | 2 |
9 | m | 17 | 90 | 90 | 90.0 | 80.5 | 87.5 | 76.5 | 27.0 | 78.5 | 67.5 | 29 | 3 | 4 |
10 | f | 25 | 100 | 100 | 100.0 | 90.0 | None | 85.0 | None | 90.0 | None | 17 | 10 | 11 |
11 | f | 22 | 80 | 60 | 70.0 | 70.0 | 70.0 | 70.0 | 65.0 | 70.0 | 70.0 | 22 | 4 | 6 |
12 | m | 41 | 90 | 80 | 85.0 | 76.5 | 55.5 | 67.5 | 52.5 | 74.0 | 55.5 | 28 | 5 | 3 |
13 | m | 53 | 95 | 60 | 77.5 | 40.0 | 33.0 | 56.0 | 49.0 | 47.0 | 44.0 | 31 | 2 | 11 |
14 | m | 43 | 95 | 90 | 92.5 | 52.0 | 29.0 | 49.0 | 36.0 | 52.0 | 29.0 | 31 | 2 | 10 |
15 | f | 23 | 90 | 80 | 85.0 | 88.0 | 40.0 | 70.5 | 66.5 | 84.0 | 54.5 | 32 | 2 | 12 |
16 | m | 42 | 90 | 90 | 90.0 | 75.5 | 55.5 | 70.5 | 50.0 | 75.0 | 50.0 | 4 | 11 | 7 |
17 | m | 19 | 55 | 60 | 57.5 | 62.0 | 50.0 | 66.0 | 50.5 | 63.0 | 50.0 | 25 | 5 | 6 |
18 | f | 31 | 90 | 95 | 92.5 | 89.5 | 90.0 | 86.0 | 81.0 | 89.0 | 88.0 | 10 | 9 | 3 |
19 | f | 45 | 100 | 85 | 92.5 | 68.0 | None | 61.0 | 54.0 | 62.0 | 54.0 | 30 | 2 | 13 |
20 | f | 43 | 65 | 65 | 65.0 | 59.0 | 55.0 | 64.0 | 59.0 | 59.5 | 57.0 | 28 | 3 | 12 |
21 | m | 35 | 90 | 100 | 95.0 | 75.5 | 35.5 | 74.5 | None | 75.5 | 35.5 | 30 | 3 | 5 |
22 | f | 35 | 100 | 75 | 87.5 | 89.5 | None | 71.0 | 80.0 | 88.0 | 80.0 | 13 | 8 | 20 |
23 | m | 24 | 55 | 100 | 77.5 | 68.0 | 67.0 | 80.0 | None | 78.0 | 67.0 | 22 | 4 | 10 |
24 | f | 64 | 75 | 85 | 80.0 | 50.0 | 25.0 | 66.0 | 24.0 | 63.0 | 24.5 | 20 | 9 | 4 |
25 | f | 36 | 100 | 80 | 90.0 | 88.0 | None | 66.0 | 63.5 | 81.0 | 63.5 | 26 | 5 | 4 |
26 | m | 35 | 70 | 90 | 80.0 | 29.5 | 28.5 | 19.0 | 41.5 | 24.0 | 35.0 | 32 | 2 | 6 |
27 | f | 74 | 60 | 65 | 62.5 | 68.5 | 49.0 | 61.0 | 49.0 | 65.0 | 49.0 | 13 | 9 | 12 |
28 | f | 61 | 80 | 20 | 50.0 | 71.0 | 63.0 | 31.0 | 72.5 | 64.5 | 70.5 | 15 | 14 | 2 |
29 | f | 43 | 90 | 85 | 87.5 | 65.5 | 27.0 | 41.0 | 45.0 | 50.0 | 45.0 | 32 | 1 | 3 |
30 | m | 32 | 90 | 75 | 82.5 | 67.0 | 56.5 | 66.0 | 65.0 | 66.0 | 64.0 | 16 | 9 | 3 |
31 | m | 44 | 95 | 90 | 92.5 | 83.0 | 56.0 | 79.0 | 42.5 | 81.0 | 54.0 | 24 | 4 | 0 |
32 | f | 29 | 95 | 55 | 75.0 | 67.0 | 53.0 | 65.0 | 53.0 | 67.0 | 53.0 | 26 | 8 | 12 |
33 | m | 62 | 45 | 90 | 67.5 | 54.0 | 37.0 | 65.0 | 81.5 | 62.0 | 61.0 | 14 | 9 | 9 |
34 | f | 33 | 80 | 100 | 90.0 | 70.5 | 76.5 | 64.5 | None | 68.0 | 76.5 | 14 | 12 | 10 |
35 | f | 53 | 100 | 50 | 75.0 | 74.5 | None | 60.5 | 65.0 | 71.0 | 65.0 | 14 | 8 | 7 |
36 | m | 22 | 70 | 65 | 67.5 | 78.0 | 88.0 | 77.0 | 80.0 | 77.0 | 84.0 | 30 | 2 | 6 |
37 | f | 46 | 80 | 75 | 77.5 | 72.5 | 72.0 | 100.0 | 91.0 | 100.0 | 91.0 | 28 | 2 | 5 |
38 | f | 41 | 70 | 55 | 62.5 | 82.0 | 61.5 | 73.0 | 69.0 | 82.0 | 64.0 | 14 | 5 | 19 |
39 | f | 35 | 50 | 95 | 72.5 | 62.5 | 29.0 | 77.0 | 32.0 | 68.0 | 31.0 | 21 | 6 | 9 |
40 | m | 53 | 65 | 80 | 72.5 | 54.0 | 32.0 | 47.5 | 78.5 | 48.0 | 51.0 | 29 | 3 | 7 |
41 | f | 36 | 90 | 100 | 95.0 | 76.5 | 75.5 | 75.0 | None | 76.0 | 75.5 | 15 | 7 | 0 |
42 | m | 29 | 100 | 70 | 85.0 | 75.0 | None | 64.5 | 43.0 | 74.0 | 43.0 | 32 | 1 | 6 |
43 | f | 31 | 85 | 90 | 87.5 | 82.0 | 49.0 | 81.0 | 36.0 | 82.0 | 49.0 | 26 | 5 | 10 |
44 | f | 21 | 85 | 90 | 87.5 | 66.0 | 29.0 | 70.0 | 29.0 | 67.0 | 29.0 | 26 | 7 | 18 |
45 | f | 42 | 90 | 90 | 90.0 | 83.0 | 83.0 | 80.5 | 36.0 | 82.5 | 76.0 | 23 | 3 | 11 |
46 | f | 40 | 95 | 65 | 80.0 | 80.0 | 89.0 | 79.0 | 58.5 | 79.5 | 63.0 | 10 | 12 | 8 |
48 | f | 23 | 90 | 85 | 87.5 | 67.0 | 47.0 | 69.0 | 40.0 | 67.0 | 40.0 | 18 | 6 | 8 |
49 | f | 24 | 85 | 75 | 80.0 | 58.0 | 50.0 | 49.0 | 68.0 | 55.0 | 59.0 | 14 | 13 | 4 |
50 | m | 54 | 90 | 70 | 80.0 | 90.0 | 83.5 | 77.5 | 69.0 | 88.0 | 79.0 | 22 | 6 | 16 |
51 | f | 24 | 85 | 95 | 90.0 | 97.0 | 41.0 | 74.0 | 73.0 | 83.0 | 55.5 | 29 | 1 | 7 |
52 | f | 21 | 85 | 75 | 80.0 | 65.0 | 73.0 | 56.0 | 68.0 | 63.5 | 68.0 | 20 | 6 | 9 |
53 | f | 21 | 90 | 80 | 85.0 | 84.0 | 55.5 | 73.5 | 70.0 | 80.5 | 65.0 | 27 | 4 | 11 |
54 | f | 43 | 95 | 75 | 85.0 | 74.0 | 89.0 | 68.0 | 65.0 | 71.0 | 68.0 | 19 | 4 | 4 |
55 | f | 32 | 75 | 55 | 65.0 | 85.0 | 81.0 | 85.0 | 86.0 | 85.0 | 83.5 | 5 | 13 | 7 |
56 | m | 50 | 70 | 85 | 77.5 | 92.5 | 72.5 | 95.0 | 65.0 | 95.0 | 65.0 | 29 | 3 | 7 |
57 | f | 53 | 95 | 75 | 85.0 | 84.0 | 55.0 | 68.0 | 61.0 | 78.5 | 58.0 | 24 | 5 | 4 |
58 | f | 16 | 85 | 85 | 85.0 | 55.0 | 30.0 | 50.0 | 40.0 | 52.5 | 35.0 | 29 | 2 | 11 |
59 | f | 67 | 95 | 75 | 85.0 | 70.0 | 7.0 | 69.0 | 60.0 | 69.0 | 59.5 | 17 | 7 | 12 |
60 | m | 36 | 90 | 65 | 77.5 | 67.5 | 28.5 | 55.0 | 52.0 | 61.0 | 50.0 | 26 | 4 | 3 |
61 | f | 34 | 90 | 90 | 90.0 | 58.5 | 43.0 | 73.5 | 53.5 | 66.0 | 47.5 | 30 | 0 | 3 |
62 | f | 42 | 100 | 100 | 100.0 | 74.5 | None | 74.0 | None | 74.0 | None | 17 | 5 | 4 |
63 | f | 46 | 80 | 90 | 85.0 | 92.0 | 75.5 | 92.0 | 63.0 | 92.0 | 73.5 | 25 | 1 | 11 |
64 | f | 69 | 95 | 80 | 87.5 | 80.0 | 65.0 | 78.5 | 70.5 | 80.0 | 70.0 | 31 | 1 | 1 |
65 | f | 31 | 100 | 95 | 97.5 | 98.0 | None | 90.0 | 40.0 | 92.0 | 40.0 | 27 | 4 | 4 |
66 | f | 44 | 90 | 95 | 92.5 | 87.0 | 47.5 | 69.0 | 87.0 | 83.0 | 67.0 | 32 | 1 | 2 |
67 | f | 25 | 100 | 100 | 100.0 | 61.5 | None | 58.5 | None | 60.5 | None | 28 | 8 | 9 |
68 | f | 45 | 70 | 50 | 60.0 | 80.5 | 51.5 | 63.0 | 69.0 | 72.5 | 61.5 | 25 | 4 | 1 |
69 | f | 47 | 90 | 100 | 95.0 | 100.0 | None | 71.5 | 83.0 | 97.5 | 83.0 | 30 | 2 | 2 |
70 | f | 33 | 85 | 70 | 77.5 | 70.0 | 38.0 | 58.5 | 65.0 | 68.0 | 40.0 | 21 | 7 | 12 |
71 | f | 40 | 40 | 100 | 70.0 | 69.0 | 56.0 | 70.0 | None | 70.0 | 56.0 | 0 | 11 | 14 |
72 | f | 18 | 80 | 75 | 77.5 | 67.5 | 51.5 | 66.0 | 57.0 | 67.0 | 53.0 | 29 | 4 | 6 |
73 | f | 74 | 85 | 80 | 82.5 | 66.0 | 55.0 | 63.0 | 50.5 | 65.0 | 55.0 | 20 | 1 | 5 |
74 | m | 21 | 40 | 40 | 40.0 | 90.5 | 80.0 | 74.5 | 83.0 | 82.0 | 81.0 | 22 | 7 | 5 |
75 | f | 45 | 80 | 95 | 87.5 | 74.0 | 67.0 | 76.0 | 17.0 | 75.0 | 64.0 | 23 | 4 | 4 |
76 | f | 61 | 100 | 40 | 70.0 | 69.5 | None | 44.5 | 73.0 | 54.5 | 73.0 | 16 | 4 | 12 |
77 | f | 42 | 70 | 90 | 80.0 | 87.0 | 72.0 | 90.5 | 43.5 | 88.5 | 64.0 | 11 | 10 | 10 |
78 | m | 31 | 100 | 70 | 85.0 | 92.0 | None | 81.0 | 60.0 | 87.5 | 60.0 | 14 | 6 | 11 |
79 | f | 37 | 90 | 80 | 85.0 | 95.5 | 68.0 | 83.5 | 83.0 | 94.0 | 71.0 | 20 | 5 | 9 |
80 | m | 28 | 100 | 50 | 75.0 | 100.0 | None | 100.0 | 100.0 | 100.0 | 100.0 | 12 | 7 | 12 |
81 | m | 41 | 90 | 85 | 87.5 | 80.0 | 59.5 | 70.0 | 41.0 | 77.0 | 59.0 | 17 | 6 | 3 |
82 | f | 41 | 80 | 75 | 77.5 | 94.5 | 61.5 | 86.0 | 74.0 | 92.0 | 67.0 | 27 | 4 | 8 |
83 | f | 34 | 90 | 35 | 62.5 | 81.0 | 52.0 | 71.0 | 58.0 | 81.0 | 58.0 | 27 | 2 | 6 |
84 | f | 39 | 75 | 70 | 72.5 | 57.0 | 57.0 | 59.5 | 50.0 | 58.0 | 50.0 | 22 | 3 | 10 |
85 | f | 18 | 85 | 85 | 85.0 | 93.0 | 92.0 | 91.0 | 89.0 | 91.5 | 91.0 | 25 | 4 | 21 |
86 | f | 31 | 100 | 85 | 92.5 | 100.0 | None | 100.0 | 50.0 | 100.0 | 50.0 | 30 | 3 | 5 |
87 | m | 26 | 95 | 75 | 85.0 | 85.0 | 88.0 | 82.0 | 82.0 | 85.0 | 85.0 | 32 | 1 | 5 |
88 | m | 66 | 60 | 85 | 72.5 | 67.5 | 66.0 | 74.0 | 57.0 | 74.0 | 64.0 | 30 | 5 | 9 |
89 | f | 26 | 60 | 80 | 70.0 | 70.0 | 77.0 | 82.0 | 67.5 | 77.0 | 70.5 | 14 | 8 | 1 |
90 | m | 45 | 100 | 95 | 97.5 | 100.0 | None | 100.0 | 100.0 | 100.0 | 100.0 | 14 | 9 | 6 |
91 | m | 62 | 100 | 80 | 90.0 | 81.0 | None | 74.5 | 82.0 | 79.5 | 82.0 | 32 | 2 | 1 |
92 | m | 22 | 85 | 95 | 90.0 | 66.0 | 56.0 | 72.0 | 63.0 | 70.5 | 59.5 | 28 | 1 | 8 |
93 | f | 28 | 100 | 75 | 87.5 | 89.5 | None | 67.0 | 60.0 | 80.0 | 60.0 | 16 | 7 | 4 |
94 | f | 41 | 35 | 75 | 55.0 | 55.0 | 61.0 | 80.0 | 57.0 | 72.0 | 60.0 | 31 | 1 | 11 |
95 | m | 46 | 95 | 80 | 87.5 | 90.0 | 75.0 | 80.0 | 80.0 | 85.0 | 75.0 | 29 | 3 | 5 |
96 | f | 56 | 70 | 50 | 60.0 | 63.0 | 52.5 | 67.5 | 65.5 | 64.0 | 59.5 | 26 | 6 | 7 |
97 | f | 23 | 70 | 85 | 77.5 | 77.0 | 66.5 | 77.0 | 77.5 | 77.0 | 74.0 | 20 | 8 | 10 |
98 | f | 70 | 90 | 85 | 87.5 | 65.5 | 85.5 | 87.0 | 80.0 | 74.0 | 80.0 | 19 | 8 | 7 |
99 | f | 24 | 70 | 80 | 75.0 | 61.5 | 81.0 | 70.0 | 61.0 | 65.0 | 81.0 | 31 | 2 | 15 |
100 | f | 44 | 65 | 25 | 45.0 | 62.0 | 72.0 | 87.0 | 77.0 | 69.5 | 73.5 | 1 | 15 | 6 |
101 | f | 28 | 100 | 40 | 70.0 | 87.0 | None | 68.0 | 54.0 | 81.0 | 54.0 | 14 | 7 | 2 |
102 | f | 40 | 75 | 65 | 70.0 | 53.0 | 37.0 | 84.0 | 52.0 | 81.0 | 51.0 | 22 | 4 | 7 |
103 | f | 33 | 85 | 40 | 62.5 | 80.0 | 27.0 | 31.0 | 82.5 | 81.0 | 73.0 | 24 | 5 | 7 |
There is much more you can do, and the documentation of Great Tables has plenty of examples and tips. In my experience, though, it is rare that you will need to style a data frame; results are usually shown graphically.
Computing environment
[4]:
%load_ext watermark
%watermark -v -p polars,great_tables,jupyterlab
Python implementation: CPython
Python version : 3.12.3
IPython version : 8.20.0
polars : 1.0.0
great_tables: 0.9.0
jupyterlab : 4.0.13