Path: blob/main/docs/source/src/rust/user-guide/expressions/window.rs
7889 views
fn main() -> Result<(), Box<dyn std::error::Error>> {1// --8<-- [start:pokemon]2use polars::prelude::*;3use reqwest::blocking::Client;45let data: Vec<u8> = Client::new()6.get("https://gist.githubusercontent.com/ritchie46/cac6b337ea52281aa23c049250a4ff03/raw/89a957ff3919d90e6ef2d34235e6bf22304f3366/pokemon.csv")7.send()?8.text()?9.bytes()10.collect();1112let file = std::io::Cursor::new(data);13let df = CsvReadOptions::default()14.with_has_header(true)15.into_reader_with_file_handle(file)16.finish()?;1718println!("{}", df.head(Some(5)));19// --8<-- [end:pokemon]2021// --8<-- [start:rank]22let result = df23.clone()24.lazy()25.select([26col("Name"),27col("Type 1"),28col("Speed")29.rank(30RankOptions {31method: RankMethod::Dense,32descending: true,33},34None,35)36.over(["Type 1"])37.alias("Speed rank"),38])39.collect()?;4041println!("{result}");42// --8<-- [end:rank]4344// --8<-- [start:rank-multiple]45// Contribute the Rust translation of the Python example by opening a PR.46// --8<-- [end:rank-multiple]4748// --8<-- [start:rank-explode]49// Contribute the Rust translation of the Python example by opening a PR.50// --8<-- [end:rank-explode]5152// --8<-- [start:athletes]53// Contribute the Rust translation of the Python example by opening a PR.54// --8<-- [end:athletes]5556// --8<-- [start:athletes-sort-over-country]57// Contribute the Rust translation of the Python example by opening a PR.58// --8<-- [end:athletes-sort-over-country]5960// --8<-- [start:athletes-explode]61// Contribute the Rust translation of the Python example by opening a PR.62// --8<-- [end:athletes-explode]6364// --8<-- [start:athletes-join]65// Contribute the Rust translation of the Python example by opening a PR.66// --8<-- [end:athletes-join]6768// --8<-- [start:pokemon-mean]69let result = df70.clone()71.lazy()72.select([73col("Name"),74col("Type 1"),75col("Speed"),76col("Speed")77.mean()78.over(["Type 1"])79.alias("Mean speed in group"),80])81.collect()?;8283println!("{result}");84// --8<-- [end:pokemon-mean]8586// --8<-- [start:group_by]87let result = df88.clone()89.lazy()90.select([91col("Type 1"),92col("Type 2"),93col("Attack")94.mean()95.over(["Type 1"])96.alias("avg_attack_by_type"),97col("Defense")98.mean()99.over(["Type 1", "Type 2"])100.alias("avg_defense_by_type_combination"),101col("Attack").mean().alias("avg_attack"),102])103.collect()?;104105println!("{result}");106// --8<-- [end:group_by]107108// --8<-- [start:operations]109let filtered = df110.clone()111.lazy()112.filter(col("Type 2").eq(lit("Psychic")))113.select([col("Name"), col("Type 1"), col("Speed")])114.collect()?;115116println!("{filtered}");117// --8<-- [end:operations]118119// --8<-- [start:sort]120let result = filtered121.lazy()122.with_columns([cols(["Name", "Speed"])123.as_expr()124.sort_by(125["Speed"],126SortMultipleOptions::default().with_order_descending(true),127)128.over(["Type 1"])])129.collect()?;130println!("{result}");131// --8<-- [end:sort]132133// --8<-- [start:examples]134let result = df135.lazy()136.select([137col("Type 1")138.head(Some(3))139.over_with_options(Some(["Type 1"]), None, WindowMapping::Explode)?140.flatten(),141col("Name")142.sort_by(143["Speed"],144SortMultipleOptions::default().with_order_descending(true),145)146.head(Some(3))147.over_with_options(Some(["Type 1"]), None, WindowMapping::Explode)?148.flatten()149.alias("fastest/group"),150col("Name")151.sort_by(152["Attack"],153SortMultipleOptions::default().with_order_descending(true),154)155.head(Some(3))156.over_with_options(Some(["Type 1"]), None, WindowMapping::Explode)?157.flatten()158.alias("strongest/group"),159col("Name")160.sort(Default::default())161.head(Some(3))162.over_with_options(Some(["Type 1"]), None, WindowMapping::Explode)?163.flatten()164.alias("sorted_by_alphabet"),165])166.collect()?;167println!("{result:?}");168// --8<-- [end:examples]169170Ok(())171}172173174