Polars (DataFrame Library) Cheat Sheet
Manipulate large tabular datasets fast with Polars' expression API, lazy execution, and multi-threaded query engine in Python and Rust.
Read, Select, and Filter
Load a Parquet file and perform basic column selection and row filtering.
import polars as pldf = pl.read_parquet("orders.parquet")result = df.select(["order_id", "amount", "status"]).filter( (pl.col("amount") > 100) & (pl.col("status") == "completed"))print(result.head())
Lazy Query with the Expression API
Build a query plan with LazyFrame and let the optimizer fuse operations before execution.
lf = ( pl.scan_parquet("orders.parquet") .filter(pl.col("status") == "completed") .group_by("customer_id") .agg([ pl.col("amount").sum().alias("total_spent"), pl.col("order_id").count().alias("n_orders"), ]) .sort("total_spent", descending=True))result = lf.collect() # optimizer runs hereprint(lf.explain()) # inspect the query plan
Joins and Window Functions
Join two frames and compute a per-group rolling calculation.
joined = orders.join(customers, on="customer_id", how="left")with_rank = joined.with_columns( pl.col("amount").rank(descending=True).over("customer_id").alias("spend_rank"), pl.col("amount").rolling_mean(window_size=3).over("customer_id").alias("rolling_avg"),)
Interop with pandas and Arrow
Convert between Polars, pandas, and Arrow without copying more than necessary.
pandas_df = df.to_pandas()polars_df = pl.from_pandas(pandas_df)# zero-copy-ish conversion via Arrowarrow_table = df.to_arrow()df2 = pl.from_arrow(arrow_table)
Common Expressions
Frequently used building blocks inside select/filter/with_columns.
- pl.col("x")- references a column inside an expression
- pl.lit(value)- inserts a literal scalar into an expression
- .over("group_col")- turns an expression into a windowed/group-wise calculation
- .alias("name")- renames the result of an expression
- pl.when(cond).then(a).otherwise(b)- vectorized conditional logic
- df.lazy() / lf.collect()- switch between eager and lazy execution modes
Default to scan_parquet + LazyFrame chains instead of read_parquet + eager DataFrame — the query optimizer can push filters and column projections down into the file scan, often cutting memory use by an order of magnitude on wide files.
Explore Topics
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance