Skip to content

Polars Essentials

Polars is a DataFrame library written in Rust. It uses Arrow memory structures and expressions that describe column operations. Methods such as filter and select can be chained, and a lazy query plan lets Polars rearrange compatible operations before execution. Polars has no pandas-style row index.

The examples use the same invented gene results as the pandas lesson. They show how expression syntax changes filtering, aggregation, and reshaping.

Use a uv project with Polars installed. Save the Python blocks in page order as polars_essentials.py, then run uv run python polars_essentials.py from that project. The final example creates a TSV file under outputs/.

Start by creating a DataFrame of invented differential expression results. The p-values are raw values, and some rows do not pass the later filters.

from pathlib import Path
import polars as pl
degs = pl.DataFrame({
"gene": ["TP53", "BRCA1", "EGFR", "KRAS", "MYC", "PTEN", "RB1", "APC"],
"log2fc": [2.5, -1.8, 3.2, 0.3, -2.1, 1.5, -0.2, 0.8],
"pvalue": [0.001, 0.003, 0.0001, 0.45, 0.002, 0.01, 0.67, 0.12],
"biotype": ["protein_coding"] * 8,
})
print(degs)
shape: (8, 4)
┌───────┬────────┬────────┬────────────────┐
│ gene ┆ log2fc ┆ pvalue ┆ biotype │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ str │
╞═══════╪════════╪════════╪════════════════╡
│ TP53 ┆ 2.5 ┆ 0.001 ┆ protein_coding │
│ BRCA1 ┆ -1.8 ┆ 0.003 ┆ protein_coding │
│ EGFR ┆ 3.2 ┆ 0.0001 ┆ protein_coding │
│ KRAS ┆ 0.3 ┆ 0.45 ┆ protein_coding │
│ MYC ┆ -2.1 ┆ 0.002 ┆ protein_coding │
│ PTEN ┆ 1.5 ┆ 0.01 ┆ protein_coding │
│ RB1 ┆ -0.2 ┆ 0.67 ┆ protein_coding │
│ APC ┆ 0.8 ┆ 0.12 ┆ protein_coding │
└───────┴────────┴────────┴────────────────┘

Each column has a strict type. Polars infers these types automatically from the data you provide.

Check the shape of the DataFrame. This returns a tuple of rows and columns.

print(degs.shape)
(8, 4)

View the data types for each column.

print(degs.dtypes)
[String, Float64, Float64, String]

Preview the first few rows with head().

print(degs.head(3))
shape: (3, 4)
┌───────┬────────┬────────┬────────────────┐
│ gene ┆ log2fc ┆ pvalue ┆ biotype │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ str │
╞═══════╪════════╪════════╪════════════════╡
│ TP53 ┆ 2.5 ┆ 0.001 ┆ protein_coding │
│ BRCA1 ┆ -1.8 ┆ 0.003 ┆ protein_coding │
│ EGFR ┆ 3.2 ┆ 0.0001 ┆ protein_coding │
└───────┴────────┴────────┴────────────────┘

Use select() to pick specific columns. Pass column names as strings.

print(degs.select("gene", "log2fc"))
shape: (8, 2)
┌───────┬────────┐
│ gene ┆ log2fc │
│ --- ┆ --- │
│ str ┆ f64 │
╞═══════╪════════╡
│ TP53 ┆ 2.5 │
│ BRCA1 ┆ -1.8 │
│ EGFR ┆ 3.2 │
│ KRAS ┆ 0.3 │
│ MYC ┆ -2.1 │
│ PTEN ┆ 1.5 │
│ RB1 ┆ -0.2 │
│ APC ┆ 0.8 │
└───────┴────────┘

Use filter() with pl.col() expressions to retain rows that match a condition. The raw p-value cutoff below teaches selection syntax. A discovery claim across many genes also needs an appropriate multiple-testing correction.

sig = degs.filter(pl.col("pvalue") < 0.05)
print(sig)
shape: (5, 4)
┌───────┬────────┬────────┬────────────────┐
│ gene ┆ log2fc ┆ pvalue ┆ biotype │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ str │
╞═══════╪════════╪════════╪════════════════╡
│ TP53 ┆ 2.5 ┆ 0.001 ┆ protein_coding │
│ BRCA1 ┆ -1.8 ┆ 0.003 ┆ protein_coding │
│ EGFR ┆ 3.2 ┆ 0.0001 ┆ protein_coding │
│ MYC ┆ -2.1 ┆ 0.002 ┆ protein_coding │
│ PTEN ┆ 1.5 ┆ 0.01 ┆ protein_coding │
└───────┴────────┴────────┴────────────────┘

Combine multiple conditions with & for AND and put parentheses around each comparison. This filter also requires a positive log2 fold change above one.

sig_up = degs.filter((pl.col("pvalue") < 0.05) & (pl.col("log2fc") > 1))
print(sig_up)
shape: (3, 4)
┌──────┬────────┬────────┬────────────────┐
│ gene ┆ log2fc ┆ pvalue ┆ biotype │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ str │
╞══════╪════════╪════════╪════════════════╡
│ TP53 ┆ 2.5 ┆ 0.001 ┆ protein_coding │
│ EGFR ┆ 3.2 ┆ 0.0001 ┆ protein_coding │
│ PTEN ┆ 1.5 ┆ 0.01 ┆ protein_coding │
└──────┴────────┴────────┴────────────────┘

Use with_columns() to create new columns from existing ones. The original DataFrame is not modified. Polars returns a new DataFrame with the added columns.

Here we compute the negative log10 p-value and label each gene as “up” or “down”.

result = degs.with_columns(
(-pl.col("pvalue").log(base=10)).alias("neg_log10p"),
pl.when(pl.col("log2fc") > 0).then(pl.lit("up")).otherwise(
pl.lit("down")).alias("direction"),
)
print(result.select("gene", "log2fc", "pvalue", "neg_log10p", "direction"))
shape: (8, 5)
┌───────┬────────┬────────┬────────────┬───────────┐
│ gene ┆ log2fc ┆ pvalue ┆ neg_log10p ┆ direction │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ f64 ┆ str │
╞═══════╪════════╪════════╪════════════╪═══════════╡
│ TP53 ┆ 2.5 ┆ 0.001 ┆ 3.0 ┆ up │
│ BRCA1 ┆ -1.8 ┆ 0.003 ┆ 2.522879 ┆ down │
│ EGFR ┆ 3.2 ┆ 0.0001 ┆ 4.0 ┆ up │
│ KRAS ┆ 0.3 ┆ 0.45 ┆ 0.346787 ┆ up │
│ MYC ┆ -2.1 ┆ 0.002 ┆ 2.69897 ┆ down │
│ PTEN ┆ 1.5 ┆ 0.01 ┆ 2.0 ┆ up │
│ RB1 ┆ -0.2 ┆ 0.67 ┆ 0.173925 ┆ down │
│ APC ┆ 0.8 ┆ 0.12 ┆ 0.920819 ┆ up │
└───────┴────────┴────────┴────────────┴───────────┘

The pl.when().then().otherwise() pattern works like an if/else statement. You can chain multiple .when() calls to handle more conditions.

Sort by p-value to put the smallest raw p-values first.

print(degs.sort("pvalue").select("gene", "pvalue"))
shape: (8, 2)
┌───────┬────────┐
│ gene ┆ pvalue │
│ --- ┆ --- │
│ str ┆ f64 │
╞═══════╪════════╡
│ EGFR ┆ 0.0001 │
│ TP53 ┆ 0.001 │
│ MYC ┆ 0.002 │
│ BRCA1 ┆ 0.003 │
│ PTEN ┆ 0.01 │
│ APC ┆ 0.12 │
│ KRAS ┆ 0.45 │
│ RB1 ┆ 0.67 │
└───────┴────────┘

Use descending=True to sort from highest to lowest. Here the first rows have the largest positive log2 fold changes.

print(degs.sort("log2fc", descending=True).select("gene", "log2fc").head(3))
shape: (3, 2)
┌──────┬────────┐
│ gene ┆ log2fc │
│ --- ┆ --- │
│ str ┆ f64 │
╞══════╪════════╡
│ EGFR ┆ 3.2 │
│ TP53 ┆ 2.5 │
│ PTEN ┆ 1.5 │
└──────┴────────┘

Use group_by() to split data into groups and compute summaries with agg(). Each row below represents one gene measurement in one sample. Repeated sample identifiers therefore do not represent additional biological samples.

sample_counts = pl.DataFrame({
"sample": ["S1", "S1", "S2", "S2", "S3", "S3"],
"gene": ["TP53", "BRCA1", "TP53", "BRCA1", "TP53", "BRCA1"],
"condition": [
"control", "control", "treated", "treated", "treated", "treated"
],
"count": [100, 250, 450, 120, 380, 95],
})
grouped = sample_counts.group_by("condition").agg(
pl.col("count").mean().alias("mean_count"),
pl.col("count").count().alias("n_measurements"),
pl.col("sample").n_unique().alias("n_samples"),
)
print(grouped.sort("condition"))
shape: (2, 4)
┌───────────┬────────────┬────────────────┬───────────┐
│ condition ┆ mean_count ┆ n_measurements ┆ n_samples │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ u32 ┆ u32 │
╞═══════════╪════════════╪════════════════╪═══════════╡
│ control ┆ 175.0 ┆ 2 ┆ 1 │
│ treated ┆ 261.25 ┆ 4 ┆ 2 │
└───────────┴────────────┴────────────────┴───────────┘

count counts non-null measurements, while n_unique counts distinct sample identifiers in this complete table. The mean pools both genes within each condition; include gene among the grouping keys for a separate mean per gene.

group_by() does not guarantee row order. Apply sort when the display needs a defined order.

Calling .lazy() on this existing DataFrame creates a lazy query. Subsequent operations describe a plan until collection executes it. Polars can then move compatible filters earlier in the plan and remove unused columns.

Call .collect() at the end to execute the plan and get a DataFrame back.

result = (
degs.lazy()
.filter(pl.col("pvalue") < 0.05)
.with_columns(
pl.when(pl.col("log2fc") > 1)
.then(pl.lit("up"))
.when(pl.col("log2fc") < -1)
.then(pl.lit("down"))
.otherwise(pl.lit("within_threshold"))
.alias("direction")
)
.sort("pvalue")
.select("gene", "log2fc", "pvalue", "direction")
.collect()
)
print(result)
shape: (5, 4)
┌───────┬────────┬────────┬───────────┐
│ gene ┆ log2fc ┆ pvalue ┆ direction │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ str │
╞═══════╪════════╪════════╪═══════════╡
│ EGFR ┆ 3.2 ┆ 0.0001 ┆ up │
│ TP53 ┆ 2.5 ┆ 0.001 ┆ up │
│ MYC ┆ -2.1 ┆ 0.002 ┆ down │
│ BRCA1 ┆ -1.8 ┆ 0.003 ┆ down │
│ PTEN ┆ 1.5 ┆ 0.01 ┆ up │
└───────┴────────┴────────┴───────────┘

The DataFrame already occupies memory before this query begins. Calling lazy does not undo that allocation. File scans can defer the read itself; performance depends on the query and input, rather than row count alone.

Gene expression data often needs to be converted between wide and long formats. Wide format has one column per sample. Long format has one row per measurement.

The unpivot() method converts wide data to long format. This is equivalent to melt() in pandas.

expression_wide = pl.DataFrame({
"gene": ["TP53", "BRCA1", "EGFR"],
"Sample1": [100, 250, 50],
"Sample2": [120, 300, 75],
"Sample3": [90, 280, 60],
})
print(expression_wide)
shape: (3, 4)
┌───────┬─────────┬─────────┬─────────┐
│ gene ┆ Sample1 ┆ Sample2 ┆ Sample3 │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 │
╞═══════╪═════════╪═════════╪═════════╡
│ TP53 ┆ 100 ┆ 120 ┆ 90 │
│ BRCA1 ┆ 250 ┆ 300 ┆ 280 │
│ EGFR ┆ 50 ┆ 75 ┆ 60 │
└───────┴─────────┴─────────┴─────────┘
expression_long = expression_wide.unpivot(
on=["Sample1", "Sample2", "Sample3"],
index="gene",
variable_name="sample",
value_name="count",
)
print(expression_long)
shape: (9, 3)
┌───────┬─────────┬───────┐
│ gene ┆ sample ┆ count │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ i64 │
╞═══════╪═════════╪═══════╡
│ TP53 ┆ Sample1 ┆ 100 │
│ BRCA1 ┆ Sample1 ┆ 250 │
│ EGFR ┆ Sample1 ┆ 50 │
│ TP53 ┆ Sample2 ┆ 120 │
│ BRCA1 ┆ Sample2 ┆ 300 │
│ EGFR ┆ Sample2 ┆ 75 │
│ TP53 ┆ Sample3 ┆ 90 │
│ BRCA1 ┆ Sample3 ┆ 280 │
│ EGFR ┆ Sample3 ┆ 60 │
└───────┴─────────┴───────┘

Long format is useful for plotting and for group_by operations across samples.

The pivot() method converts long data back to wide format.

Each gene and sample pair identifies one measurement here. Repeated pairs need an explicit aggregation rule rather than an assumed value for the output cell.

back_to_wide = expression_long.pivot(
on="sample",
index="gene",
values="count",
)
print(back_to_wide)
shape: (3, 4)
┌───────┬─────────┬─────────┬─────────┐
│ gene ┆ Sample1 ┆ Sample2 ┆ Sample3 │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 │
╞═══════╪═════════╪═════════╪═════════╡
│ TP53 ┆ 100 ┆ 120 ┆ 90 │
│ BRCA1 ┆ 250 ┆ 300 ┆ 280 │
│ EGFR ┆ 50 ┆ 75 ┆ 60 │
└───────┴─────────┴─────────┴─────────┘

Polars can read and write CSV, TSV, Parquet, and other formats. Here we write a TSV and read it back.

output_dir = Path("outputs")
output_dir.mkdir(exist_ok=True)
output_path = output_dir / "degs_polars.tsv"
degs.select("gene", "log2fc", "pvalue").write_csv(
output_path, separator="\t"
)
read_back = pl.read_csv(output_path, separator="\t")
print(read_back)
shape: (8, 3)
┌───────┬────────┬────────┐
│ gene ┆ log2fc ┆ pvalue │
│ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 │
╞═══════╪════════╪════════╡
│ TP53 ┆ 2.5 ┆ 0.001 │
│ BRCA1 ┆ -1.8 ┆ 0.003 │
│ EGFR ┆ 3.2 ┆ 0.0001 │
│ KRAS ┆ 0.3 ┆ 0.45 │
│ MYC ┆ -2.1 ┆ 0.002 │
│ PTEN ┆ 1.5 ┆ 0.01 │
│ RB1 ┆ -0.2 ┆ 0.67 │
│ APC ┆ 0.8 ┆ 0.12 │
└───────┴────────┴────────┘

Parquet preserves column types and supports column selection during a read. Polars provides write_parquet and read_parquet for that format. The SQL and DuckDB lesson demonstrates queries against Parquet measurements.

If you already know pandas, this table maps common operations to their Polars equivalents.

Operation pandas Polars
Select columns df[["gene", "log2fc"]] df.select("gene", "log2fc")
Filter rows df[df["pvalue"] < 0.05] df.filter(pl.col("pvalue") < 0.05)
Add column df["new"] = values df.with_columns(expr.alias("new"))
Sort df.sort_values("col") df.sort("col")
Group + aggregate df.groupby("col").agg(...) df.group_by("col").agg(...)
Wide to long df.melt(...) df.unpivot(...)
Long to wide df.pivot(...) df.pivot(...)
Read CSV pd.read_csv(...) pl.read_csv(...)

Polars expressions describe operations that a context such as select or with_columns applies to a table. The expression can participate in an eager operation or remain part of a lazy query plan.

The join lesson combines gene measurements with annotations and identifies unmatched keys. Its Python tab uses Polars, so you can extend the expression syntax from this page to several related tables.

The missing-value lesson compares detection, removal, and replacement of absent measurements. The reshape lesson converts between long tables and gene-by-sample matrices while preserving the measured totals.

Continue to OOP with Classes to learn how to organize your bioinformatics code into reusable classes.

The Data Wrangling section extends Polars operations to joins, missing values, and duplicate keys.

The Polars project gives its Zenodo software record as the citation for the library.

The Polars guide explains select, with_columns, filter, and group_by contexts. See Polars expressions and contexts. The pivot reference defines the reshape arguments. The read_csv reference and write_csv reference define the separator used for the TSV example.

The optimization guide explains which operations Polars can move or remove. The execution guide explains how collect executes the resulting plan.

The migration guide compares Polars expressions, Arrow memory structures, and row indexing with pandas. Use it when translating an existing pandas operation.