Path: blob/main/docs/source/src/python/user-guide/expressions/window.py
7890 views
# --8<-- [start:pokemon]1import polars as pl23types = (4"Grass Water Fire Normal Ground Electric Psychic Fighting Bug Steel "5"Flying Dragon Dark Ghost Poison Rock Ice Fairy".split()6)7type_enum = pl.Enum(types)8# then let's load some csv data with information about pokemon9pokemon = pl.read_csv(10"https://gist.githubusercontent.com/ritchie46/cac6b337ea52281aa23c049250a4ff03/raw/89a957ff3919d90e6ef2d34235e6bf22304f3366/pokemon.csv",11).cast({"Type 1": type_enum, "Type 2": type_enum})12print(pokemon.head())13# --8<-- [end:pokemon]1415# --8<-- [start:rank]16result = pokemon.select(17pl.col("Name", "Type 1"),18pl.col("Speed").rank("dense", descending=True).over("Type 1").alias("Speed rank"),19)2021print(result)22# --8<-- [end:rank]2324# --8<-- [start:rank-multiple]25result = pokemon.select(26pl.col("Name", "Type 1", "Type 2"),27pl.col("Speed")28.rank("dense", descending=True)29.over("Type 1", "Type 2")30.alias("Speed rank"),31)3233print(result)34# --8<-- [end:rank-multiple]3536# --8<-- [start:rank-explode]37result = (38pokemon.group_by("Type 1")39.agg(40pl.col("Name"),41pl.col("Speed").rank("dense", descending=True).alias("Speed rank"),42)43.select(pl.col("Name"), pl.col("Type 1"), pl.col("Speed rank"))44.explode("Name", "Speed rank")45)4647print(result)48# --8<-- [end:rank-explode]4950# --8<-- [start:athletes]51athletes = pl.DataFrame(52{53"athlete": list("ABCDEF"),54"country": ["PT", "NL", "NL", "PT", "PT", "NL"],55"rank": [6, 1, 5, 4, 2, 3],56}57)58print(athletes)59# --8<-- [end:athletes]6061# --8<-- [start:athletes-sort-over-country]62result = athletes.select(63pl.col("athlete", "rank").sort_by(pl.col("rank")).over(pl.col("country")),64pl.col("country"),65)6667print(result)68# --8<-- [end:athletes-sort-over-country]6970# --8<-- [start:athletes-explode]71result = athletes.select(72pl.all()73.sort_by(pl.col("rank"))74.over(pl.col("country"), mapping_strategy="explode"),75)7677print(result)78# --8<-- [end:athletes-explode]7980# --8<-- [start:athletes-join]81result = athletes.with_columns(82pl.col("rank").sort().over(pl.col("country"), mapping_strategy="join"),83)8485print(result)86# --8<-- [end:athletes-join]8788# --8<-- [start:pokemon-mean]89result = pokemon.select(90pl.col("Name", "Type 1", "Speed"),91pl.col("Speed").mean().over(pl.col("Type 1")).alias("Mean speed in group"),92)9394print(result)95# --8<-- [end:pokemon-mean]969798# --8<-- [start:group_by]99result = pokemon.select(100"Type 1",101"Type 2",102pl.col("Attack").mean().over("Type 1").alias("avg_attack_by_type"),103pl.col("Defense")104.mean()105.over(["Type 1", "Type 2"])106.alias("avg_defense_by_type_combination"),107pl.col("Attack").mean().alias("avg_attack"),108)109print(result)110# --8<-- [end:group_by]111112# --8<-- [start:operations]113filtered = pokemon.filter(pl.col("Type 2") == "Psychic").select(114"Name",115"Type 1",116"Speed",117)118print(filtered)119# --8<-- [end:operations]120121# --8<-- [start:sort]122result = filtered.with_columns(123pl.col("Name", "Speed").sort_by("Speed", descending=True).over("Type 1"),124)125print(result)126# --8<-- [end:sort]127128# --8<-- [start:examples]129result = pokemon.sort("Type 1").select(130pl.col("Type 1").head(3).over("Type 1", mapping_strategy="explode"),131pl.col("Name")132.sort_by(pl.col("Speed"), descending=True)133.head(3)134.over("Type 1", mapping_strategy="explode")135.alias("fastest/group"),136pl.col("Name")137.sort_by(pl.col("Attack"), descending=True)138.head(3)139.over("Type 1", mapping_strategy="explode")140.alias("strongest/group"),141pl.col("Name")142.sort()143.head(3)144.over("Type 1", mapping_strategy="explode")145.alias("sorted_by_alphabet"),146)147print(result)148# --8<-- [end:examples]149150151