Polars 2 cheatsheet
Polars 2: Basics
8 common Polars 2 tasks.
Create a DataFrame
Build a DataFrame from a dictionary of columns.
import polars as pl
df = pl.DataFrame({
"name": ["Ada", "Grace", "Linus"],
"age": [36, 45, 28],
"team": ["a", "b", "a"],
})
Look at the data
Preview rows, check the schema and summarize numeric columns.
df.head()
df.schema
df.describe()
Select columns
Choose columns by name or expression.
df.select("name", "age")
df.select(pl.col("age") * 2)
Filter rows
Keep rows that match a condition. Combine conditions with & and |.
df.filter((pl.col("age") > 30) & (pl.col("team") == "a"))
Add or change columns
Create new columns from expressions. Existing columns with the same name are replaced.
df.with_columns(
(pl.col("age") + 1).alias("next_age"),
pl.col("name").str.to_uppercase().alias("shout"),
)