Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/docs/source/src/python/user-guide/misc/styling.py
7890 views
1
# --8<-- [start:setup]
2
import warnings
3
4
# great-tables throws a Deprecation warning in `as_raw_html` under Python 3.13
5
# https://github.com/posit-dev/great-tables/pull/563
6
warnings.filterwarnings(
7
"ignore", "'count' is passed as positional argument", category=DeprecationWarning
8
)
9
# --8<-- [end:setup]
10
11
# --8<-- [start:dataframe]
12
import polars as pl
13
import polars.selectors as cs
14
15
path = "docs/assets/data/iris.csv"
16
17
df = (
18
pl.scan_csv(path)
19
.group_by("species")
20
.agg(cs.starts_with("petal").mean().round(3))
21
.collect()
22
)
23
print(df)
24
# --8<-- [end:dataframe]
25
26
# --8<-- [start:structure-header]
27
df.style.tab_header(title="Iris Data", subtitle="Mean measurement values per species")
28
# --8<-- [end:structure-header]
29
30
# --8<-- [start:structure-header-out]
31
print(
32
df.style.tab_header(
33
title="Iris Data", subtitle="Mean measurement values per species"
34
).as_raw_html()
35
)
36
# --8<-- [end:structure-header-out]
37
38
39
# --8<-- [start:structure-stub]
40
df.style.tab_stub(rowname_col="species")
41
# --8<-- [end:structure-stub]
42
43
# --8<-- [start:structure-stub-out]
44
print(df.style.tab_stub(rowname_col="species").as_raw_html())
45
# --8<-- [end:structure-stub-out]
46
47
# --8<-- [start:structure-spanner]
48
(
49
df.style.tab_spanner("Petal", cs.starts_with("petal")).cols_label(
50
petal_length="Length", petal_width="Width"
51
)
52
)
53
# --8<-- [end:structure-spanner]
54
55
# --8<-- [start:structure-spanner-out]
56
print(
57
df.style.tab_spanner("Petal", cs.starts_with("petal"))
58
.cols_label(petal_length="Length", petal_width="Width")
59
.as_raw_html()
60
)
61
# --8<-- [end:structure-spanner-out]
62
63
# --8<-- [start:format-number]
64
df.style.fmt_number("petal_width", decimals=1)
65
# --8<-- [end:format-number]
66
67
68
# --8<-- [start:format-number-out]
69
print(df.style.fmt_number("petal_width", decimals=1).as_raw_html())
70
# --8<-- [end:format-number-out]
71
72
73
# --8<-- [start:style-simple]
74
from great_tables import loc, style
75
76
df.style.tab_style(
77
style.fill("yellow"),
78
loc.body(
79
rows=pl.col("petal_length") == pl.col("petal_length").max(),
80
),
81
)
82
# --8<-- [end:style-simple]
83
84
# --8<-- [start:style-simple-out]
85
from great_tables import loc, style
86
87
print(
88
df.style.tab_style(
89
style.fill("yellow"),
90
loc.body(
91
rows=pl.col("petal_length") == pl.col("petal_length").max(),
92
),
93
).as_raw_html()
94
)
95
# --8<-- [end:style-simple-out]
96
97
98
# --8<-- [start:style-bold-column]
99
from great_tables import loc, style
100
101
df.style.tab_style(
102
style.text(weight="bold"),
103
loc.body(columns="species"),
104
)
105
# --8<-- [end:style-bold-column]
106
107
# --8<-- [start:style-bold-column-out]
108
from great_tables import loc, style
109
110
print(
111
df.style.tab_style(
112
style.text(weight="bold"),
113
loc.body(columns="species"),
114
).as_raw_html()
115
)
116
# --8<-- [end:style-bold-column-out]
117
118
# --8<-- [start:full-example]
119
from great_tables import loc, style
120
121
(
122
df.style.tab_header(
123
title="Iris Data", subtitle="Mean measurement values per species"
124
)
125
.tab_stub(rowname_col="species")
126
.cols_label(petal_length="Length", petal_width="Width")
127
.tab_spanner("Petal", cs.starts_with("petal"))
128
.fmt_number("petal_width", decimals=2)
129
.tab_style(
130
style.fill("yellow"),
131
loc.body(
132
rows=pl.col("petal_length") == pl.col("petal_length").max(),
133
),
134
)
135
)
136
# --8<-- [end:full-example]
137
138
# --8<-- [start:full-example-out]
139
from great_tables import loc, style
140
141
print(
142
df.style.tab_header(
143
title="Iris Data", subtitle="Mean measurement values per species"
144
)
145
.tab_stub(rowname_col="species")
146
.cols_label(petal_length="Length", petal_width="Width")
147
.tab_spanner("Petal", cs.starts_with("petal"))
148
.fmt_number("petal_width", decimals=2)
149
.tab_style(
150
style.fill("yellow"),
151
loc.body(
152
rows=pl.col("petal_length") == pl.col("petal_length").max(),
153
),
154
)
155
.tab_style(
156
style.text(weight="bold"),
157
loc.body(columns="species"),
158
)
159
.as_raw_html()
160
)
161
# --8<-- [end:full-example-out]
162
163