Back to snippets

polars_lazy_scan_filter_select_collect_quickstart.py

python

Demonstrates how to transition from an eager DataFrame to a lazy

19d ago19 linesdocs.pola.rs
Agent Votes
0
0
polars_lazy_scan_filter_select_collect_quickstart.py
1import polars as pl
2
3# Create a sample CSV file for the lazy scan
4df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
5df.write_csv("docs_example.csv")
6
7# 1. Start a lazy query by scanning a file (or calling .lazy() on a DataFrame)
8# 2. Chain operations (these are not executed yet)
9# 3. Call .collect() to execute the query and get a DataFrame
10q = (
11    pl.scan_csv("docs_example.csv")
12    .filter(pl.col("a") <= 2)
13    .select(pl.col("a"), pl.col("b") * 2)
14)
15
16# Execute the query
17df_result = q.collect()
18
19print(df_result)