Learn  /  Cheat sheets

Polars cheat sheet

Polars expressions for everyday analysis, updated for Polars 2.0, on one printable page.

Cheat sheet5 min readUpdated 18 October 2026
PythonData analysis
All cheat sheets and guides
All guides and cheat sheets

Read and scan

Python
import polars as pl

df = pl.read_csv("sales.csv", try_parse_dates=True)
df = pl.read_excel("sales.xlsx", sheet_name="Data")
lf = pl.scan_parquet("sales/*.parquet")  # lazy: nothing runs yet
lf.collect_schema()                      # names and types, no run

Select and filter

Python
df.select("date", "region", "amount")
df.filter((pl.col("region") == "North") & (pl.col("amount") > 100))
df.filter(pl.col("region").is_in(["North", "South"]))
df.unique(subset="order_id", keep="first")
df.head(10)

New columns

Python
df.with_columns(
    net=pl.col("amount") - pl.col("discount"),
    month=pl.col("date").dt.truncate("1mo"),
    band=pl.when(pl.col("amount") >= 1000).then(pl.lit("Large"))
           .otherwise(pl.lit("Small")),
)
df.rename({"amt": "amount"})

Group and aggregate

Python
(df.group_by("region")
   .agg(total=pl.col("amount").sum(),
        orders=pl.len(),
        customers=pl.col("customer_id").n_unique())
   .sort("total", descending=True))

Joins and stacking

Python
orders.join(customers, on="customer_id", how="left")
orders.join(returns, on="order_id", how="anti")   # not returned
orders.join(prices, left_on="sku", right_on="product_sku")
pl.concat([jan, feb])                             # stack rows

Window expressions

Python
df.with_columns(
    share=pl.col("amount") / pl.col("amount").sum().over("region"),
    rank=pl.col("amount").rank("dense", descending=True)
           .over("region"),
)
# Latest order per customer
df.filter(pl.col("date") == pl.col("date").max().over("customer_id"))

Pivot and unpivot

Python
df.pivot(on="month", index="region", values="amount",
         aggregate_function="sum")
wide.unpivot(index="region", variable_name="month",
             value_name="amount")

Text, dates and nulls

Python
pl.col("name").str.strip_chars().str.to_titlecase()
pl.col("email").str.split("@").list.get(1)
pl.col("text_date").str.to_date("%d/%m/%Y")
pl.col("date").dt.year()
pl.col("amount").fill_null(0)

Lazy queries

Python
result = (
    pl.scan_parquet("sales/*.parquet")
      .filter(pl.col("year") == 2026)
      .group_by("region")
      .agg(pl.col("revenue").sum())
      .sort("region")       # order is not guaranteed otherwise
      .collect()            # streaming engine by default in 2.0
)

SQL

Python
pl.sql("""
    SELECT region, SUM(amount) AS total
    FROM df
    GROUP BY region
""").collect()

Write and convert

Python
df.write_parquet("out/sales.parquet")
df.write_csv("out/sales.csv")
df.write_excel("out/sales.xlsx")   # needs xlsxwriter
df.to_pandas()                     # needs pandas and pyarrow

Good habits

Start lazy with scan_parquet or scan_csv so Polars can skip what you do not need. Use expressions instead of map_elements with Python functions. In Polars 2.0, joins and group_by do not keep row order, so add sort() when order matters.

Alessandro Ecclesie AgazziWritten by Alessandro Ecclesie Agazzi, analytics engineer in London. Updated 18 October 2026. About me

Found this useful?

New guides and "what's new" explainers on data and AI land here regularly. Follow along on LinkedIn, or share this one with a colleague.