Polars expressions in Python
The expression API that powers every transform.
import polars as pl
df = pl.DataFrame({
"name": ["ada", "grace", "katherine"],
"score": [95, 88, 92],
})
ranked = df.with_columns(
(pl.col("score") / 100).alias("ratio"),
pl.col("name").str.to_uppercase().alias("label"),
(pl.col("score") >= 90).alias("honors"),
)
bumped = df.with_columns(pl.col("score") + 5)
How it works
with_columnsadds computed columns.pl.col('score')refers to a column in an expression.aliasnames the result; string and boolean ops chain.
Keywords and builtins used here
as
The run, in numbers
- Lines
- 14
- Characters to type
- 312
- Tokens
- 130
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 74 seconds.
Step 1 of 4 in Expressions, step 5 of 32 in Polars.