What Happened
Quick reference guide for transforming, analyzing, and visualizing data with Python Polars
Download PDF Polars is a library for transforming, analyzing, and visualizing data with a fast and expressive DataFrame API. released by Ritchie Vink in 2020.
uv pip install "polars[all]" Import Polars in Python, and confirm which versions of Polars and its dependencies you have installed:
import polars .show_versions() Polars queries typically read data, transform it, result back out. A complete a single chain of method calls:
Why It Matters
fruit = pl.read_csv("fruit.csv") fruit.filter( (pl.col("weight") > 1000) & pl.col("is_round") ).write_parquet("fruit.parquet") Throughout this cheatsheet, df is a DataFrame, lf is a LazyFrame, o is a second DataFrame to combine with df, and e stands for any expression. So e.abs() means “call .abs() on an expression”, .col("x").abs().
Unlike pandas, Polars DataFrames a row index, favors immutability and method chaining over in-place modifications.
series = pl.Series("sales", [150.00, 300.00, 250.00]) Create a DataFrame from a dictionary of columns, a Series or a plain Python sequence. .read_*() functions to create one from a file:
df = pl.DataFrame({ "sales": series, "id": [41, 42, 43] }) Because , add one explicitly as a column :
What Comes Next
df.with_row_index("id") Turn a DataFrame into a LazyFrame. Alternatively, start from a LazyFrame directly .scan_*() functions:
lf = df.lazy() # executes immediately, whereas builds an optimized . The optimizer automatically applies predicate pushdown (filtering possible) and projection pushdown (dropping columns ).
You move between the two representations with .lazy() and .collect(): .lazy() turns a DataFrame into a LazyFrame, and .collect() executes a LazyFrame a DataFrame back.
Turn a DataFrame into a LazyFrame, and execute a LazyFrame to get a DataFrame:
lf = df.lazy() df = lf.collect() Use the streaming engine to process data out-of-core, so that datasets larger than memory handled:
lf.collect(engine="streaming") Print the optimized , or visualize it as a graph, optimizer decided to do:
lf.explain() lf.show_graph() Execute return per-node timings, actually goes:
Explore more: Software & AI Guide