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/transformations/pivot.py
7890 views
1
# --8<-- [start:setup]
2
import polars as pl
3
4
# --8<-- [end:setup]
5
6
# --8<-- [start:df]
7
df = pl.DataFrame(
8
{
9
"foo": ["A", "A", "B", "B", "C"],
10
"N": [1, 2, 2, 4, 2],
11
"bar": ["k", "l", "m", "n", "o"],
12
}
13
)
14
print(df)
15
# --8<-- [end:df]
16
17
# --8<-- [start:eager]
18
out = df.pivot("bar", index="foo", values="N", aggregate_function="first")
19
print(out)
20
# --8<-- [end:eager]
21
22
# --8<-- [start:lazy]
23
q = (
24
df.lazy()
25
.collect()
26
.pivot(index="foo", on="bar", values="N", aggregate_function="first")
27
.lazy()
28
)
29
out = q.collect()
30
print(out)
31
# --8<-- [end:lazy]
32
33