Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Expressions

Column Selectors

Select columns by type, name pattern or position with polars.selectors (cs), combine them with set operations, and apply one expression to many columns.

Wide tables have dozens or hundreds of columns, and writing every name by hand is tedious and fragile. Selectors let you describe columns by what they are: "all numeric columns", "everything that starts with sales_", "every column except the id". They come from the polars.selectors module, conventionally imported as cs.

Python
import polars as pl
import polars.selectors as cs
Python A wide table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Selecting by type

Python By dtype

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

cs.numeric(), cs.float(), cs.integer(), cs.string(), cs.temporal(), cs.boolean(), cs.categorical() and cs.by_dtype(...) are the type-based selectors. cs.all() selects everything.

Selecting by name

Python By name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All of them return a selector, which can be used anywhere an expression could be: in select, with_columns, group_by, agg, drop and filter.

Set operations: combine, subtract, intersect

Selectors support Python's set operators, which is where they shine.

Operator Meaning
a | b Columns in a or b (union)
a & b Columns in both (intersection)
a - b Columns in a but not b (difference)
a ^ b Columns in exactly one (symmetric difference)
~a Everything except a (complement)
Python Set operations on selectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Applying one expression to many columns

A selector plus one expression is a whole transformation. Combine it with name.suffix so new columns do not overwrite old ones.

Python Transform many columns at once

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

name.map takes a Python function from old name to new name. It runs once per column name, not per row, so it costs nothing at scale.

Selectors in group_by and agg

Python Selectors inside agg

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Expanding a selector into names

cs.expand_selector(df, selector) returns the matching names, which helps debugging, and cs.is_selector(obj) tests whether something is a selector. Use df.select(sel).columns when you only want the names.

Plain pl.col also handles dtypes, regexes and lists of names, but selectors are more readable and support set operations. For example, cs.numeric() - cs.by_name("id") has no short equivalent with pl.col.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which expression selects every numeric column except 'id'?

    Selectors support set difference with the minus operator.
  2. 2. What does ~cs.numeric() select?

    The tilde is the complement: every column the selector does not match.
  3. 3. Which selector matches columns whose names start with 'sales'?

    cs.starts_with matches by name prefix. cs.ends_with, cs.contains and cs.matches (regex) work similarly.
  4. 4. Why add name.suffix('_z') after transforming many columns with with_columns?

    Each output keeps its input column name by default, so it would replace the original. A suffix keeps both.

Summary

  • polars.selectors (cs) selects columns by type, name pattern or position.
  • Combine selectors with |, &, -, ^ and ~.
  • One expression on a selector transforms many columns; use name.prefix, name.suffix and name.map to name outputs.
  • Selectors work in select, with_columns, agg, drop and more.

Next: List and Struct Columns