Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/docs/source/src/rust/user-guide/expressions/window.rs
7889 views
1
fn main() -> Result<(), Box<dyn std::error::Error>> {
2
// --8<-- [start:pokemon]
3
use polars::prelude::*;
4
use reqwest::blocking::Client;
5
6
let data: Vec<u8> = Client::new()
7
.get("https://gist.githubusercontent.com/ritchie46/cac6b337ea52281aa23c049250a4ff03/raw/89a957ff3919d90e6ef2d34235e6bf22304f3366/pokemon.csv")
8
.send()?
9
.text()?
10
.bytes()
11
.collect();
12
13
let file = std::io::Cursor::new(data);
14
let df = CsvReadOptions::default()
15
.with_has_header(true)
16
.into_reader_with_file_handle(file)
17
.finish()?;
18
19
println!("{}", df.head(Some(5)));
20
// --8<-- [end:pokemon]
21
22
// --8<-- [start:rank]
23
let result = df
24
.clone()
25
.lazy()
26
.select([
27
col("Name"),
28
col("Type 1"),
29
col("Speed")
30
.rank(
31
RankOptions {
32
method: RankMethod::Dense,
33
descending: true,
34
},
35
None,
36
)
37
.over(["Type 1"])
38
.alias("Speed rank"),
39
])
40
.collect()?;
41
42
println!("{result}");
43
// --8<-- [end:rank]
44
45
// --8<-- [start:rank-multiple]
46
// Contribute the Rust translation of the Python example by opening a PR.
47
// --8<-- [end:rank-multiple]
48
49
// --8<-- [start:rank-explode]
50
// Contribute the Rust translation of the Python example by opening a PR.
51
// --8<-- [end:rank-explode]
52
53
// --8<-- [start:athletes]
54
// Contribute the Rust translation of the Python example by opening a PR.
55
// --8<-- [end:athletes]
56
57
// --8<-- [start:athletes-sort-over-country]
58
// Contribute the Rust translation of the Python example by opening a PR.
59
// --8<-- [end:athletes-sort-over-country]
60
61
// --8<-- [start:athletes-explode]
62
// Contribute the Rust translation of the Python example by opening a PR.
63
// --8<-- [end:athletes-explode]
64
65
// --8<-- [start:athletes-join]
66
// Contribute the Rust translation of the Python example by opening a PR.
67
// --8<-- [end:athletes-join]
68
69
// --8<-- [start:pokemon-mean]
70
let result = df
71
.clone()
72
.lazy()
73
.select([
74
col("Name"),
75
col("Type 1"),
76
col("Speed"),
77
col("Speed")
78
.mean()
79
.over(["Type 1"])
80
.alias("Mean speed in group"),
81
])
82
.collect()?;
83
84
println!("{result}");
85
// --8<-- [end:pokemon-mean]
86
87
// --8<-- [start:group_by]
88
let result = df
89
.clone()
90
.lazy()
91
.select([
92
col("Type 1"),
93
col("Type 2"),
94
col("Attack")
95
.mean()
96
.over(["Type 1"])
97
.alias("avg_attack_by_type"),
98
col("Defense")
99
.mean()
100
.over(["Type 1", "Type 2"])
101
.alias("avg_defense_by_type_combination"),
102
col("Attack").mean().alias("avg_attack"),
103
])
104
.collect()?;
105
106
println!("{result}");
107
// --8<-- [end:group_by]
108
109
// --8<-- [start:operations]
110
let filtered = df
111
.clone()
112
.lazy()
113
.filter(col("Type 2").eq(lit("Psychic")))
114
.select([col("Name"), col("Type 1"), col("Speed")])
115
.collect()?;
116
117
println!("{filtered}");
118
// --8<-- [end:operations]
119
120
// --8<-- [start:sort]
121
let result = filtered
122
.lazy()
123
.with_columns([cols(["Name", "Speed"])
124
.as_expr()
125
.sort_by(
126
["Speed"],
127
SortMultipleOptions::default().with_order_descending(true),
128
)
129
.over(["Type 1"])])
130
.collect()?;
131
println!("{result}");
132
// --8<-- [end:sort]
133
134
// --8<-- [start:examples]
135
let result = df
136
.lazy()
137
.select([
138
col("Type 1")
139
.head(Some(3))
140
.over_with_options(Some(["Type 1"]), None, WindowMapping::Explode)?
141
.flatten(),
142
col("Name")
143
.sort_by(
144
["Speed"],
145
SortMultipleOptions::default().with_order_descending(true),
146
)
147
.head(Some(3))
148
.over_with_options(Some(["Type 1"]), None, WindowMapping::Explode)?
149
.flatten()
150
.alias("fastest/group"),
151
col("Name")
152
.sort_by(
153
["Attack"],
154
SortMultipleOptions::default().with_order_descending(true),
155
)
156
.head(Some(3))
157
.over_with_options(Some(["Type 1"]), None, WindowMapping::Explode)?
158
.flatten()
159
.alias("strongest/group"),
160
col("Name")
161
.sort(Default::default())
162
.head(Some(3))
163
.over_with_options(Some(["Type 1"]), None, WindowMapping::Explode)?
164
.flatten()
165
.alias("sorted_by_alphabet"),
166
])
167
.collect()?;
168
println!("{result:?}");
169
// --8<-- [end:examples]
170
171
Ok(())
172
}
173
174