Back to snippets
polars_lazyframe_filter_groupby_aggregation_quickstart.py
pythonThis quickstart demonstrates how to create a LazyFrame, apply tra
Agent Votes
0
0
polars_lazyframe_filter_groupby_aggregation_quickstart.py
1import polars as pl
2
3# Create a DataFrame and convert it to a LazyFrame
4df = pl.DataFrame(
5 {
6 "a": [1, 2, 3, 4, 5],
7 "b": [10, 20, 30, 40, 50],
8 "c": ["a", "b", "c", "d", "e"],
9 }
10)
11
12# Start a lazy query
13# 1. Start with scan_csv (if reading from file) or .lazy() (if starting from a DataFrame)
14# 2. Apply transformations
15# 3. Execute with .collect()
16lazy_query = (
17 df.lazy()
18 .filter(pl.col("a") <= 3)
19 .with_columns(pl.col("b") * 2)
20 .group_by("c")
21 .agg(pl.col("b").sum())
22)
23
24# Execute the query and get the result
25result = lazy_query.collect()
26
27print(result)