typestar

Choosing columns in Python

select takes expressions, so a column set can be described rather than listed.

import polars as pl

frame = pl.DataFrame({
    "lang": ["python", "rust"],
    "tours": [11, 12],
    "steps": [299, 181],
    "tpm": [104.5, 98.0],
})

print(frame.select(pl.col("lang", "tpm")))
print(frame.select(pl.all().exclude("lang")).columns)
print(frame.select(pl.col(pl.Int64)).columns)
print(frame.select(pl.all().exclude(pl.Utf8)).columns)

How it works

  1. pl.all() is every column; exclude removes from a set.
  2. pl.col accepts several names, or a dtype.
  3. Selectors compose, which keeps wide frames manageable.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
335
Tokens
139
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 79 seconds.

Type this snippet

Step 2 of 4 in Expressions, step 6 of 32 in Polars.

← Previous Next →