We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Expression-Based Operations

For an individual operation, Polars' expression-based syntax may not feel all that different from the equivalent in Pandas.

# Polars
df.filter(pl.col("level") > 25)

# Pandas
df[df["level"] > 25]

But if you have a sequence of operations to carry out, the expression-based system can feel much cleaner.

Polars Example

result = (
    df.filter(pl.col("region") == "west")
    .with_columns(
        (pl.col("tickets_closed") / pl.col("hours_worked")).alias("tickets_per_hour")
    )
    .sort("tickets_per_hour", descending=True)
)

Pandas Example

filtered_df = df[df["region"] == "west"].copy()
filtered_df["tickets_per_hour"] = (
    filtered_df["tickets_closed"] / filtered_df["hours_worked"]
)
result = filtered_df.sort_values("tickets_per_hour", ascending=False)

The Pandas version works, but the transformation is split across multiple names and stores intermediate DataFrames. In Polars, each operation returns a DataFrame that can be passed directly into the next method.

Polars prefers expressions because they're readable (the step-by-step transformation is laid out in order), optimizable (Polars can plan the execution), and composable (they're simple to combine and reuse).

Pandas now also has tools for chaining, like .pipe(), but expressions are built into Polars from the ground up.

Assignment

The SnackStack telemetry team needs a compact report of devices with high temperature readings.

Complete the build_temperature_report function. It accepts a Polars DataFrame of device readings and returns a new DataFrame.

Using one chain of expressions: