Computing with window functions
Window functions are a great feature in Polars. You may have heard of them or use them frequently in SQL, Spark, and so on. What they help with is the aggregations over groups. A window function helps calculate values over specific groups regardless of the granularity of your dataset. If just doing a group by operation, the resulting DataFrame is changed to the length of that group; however, with a window function, you retain the size or height of your original dataset.
In this recipe, we’ll cover how to aggregate and rank rows over groups as well as sorting over groups.
Getting ready
We’ll be using the Contoso dataset in this recipe. Make sure to read it in a DataFrame and create a new column beforehand:
df = pl.read_csv('../data/contoso_sales.csv', try_parse_dates=True) df = df.with_columns( (pl.col('Quantity') * pl.col('Net Price')).round(2).alias('Sales Amount&apos...