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

Index Alternatives

In Pandas, you might set a column as the index of a DataFrame for convenient lookups:

products = pd.DataFrame(
    {
        "sku": ["TSH-BLU-MED", "CL-SAN-LT-8-GRN", "AP-AN001-SM"],
        "price": [19.99, 29.99, 9.99],
    }
)

# Set "sku" as the index
prices = products.set_index("sku")["price"]
print(prices["TSH-BLU-MED"])
# 19.99

In Polars, because you don't have index labels, you just filter by the column directly:

products = pl.DataFrame(
    {
        "sku": ["TSH-BLU-MED", "CL-SAN-LT-8-GRN", "AP-AN001-SM"],
        "price": [19.99, 29.99, 9.99],
    }
)
tshirt_price = products.filter(pl.col("sku") == "TSH-BLU-MED")["price"].item()
print(tshirt_price)
# 19.99

Alignment Without Index

Pandas automatically matches rows by index labels when performing operations across DataFrames:

products = pd.DataFrame(
    {
        "sku": ["TSH-BLU-MED", "CL-SAN-LT-8-GRN", "AP-AN001-SM"],
        "price": [19.99, 29.99, 9.99],
    }
)
products.set_index("sku", inplace=True)

# The order is different, but Pandas will align by index labels!
fees = pd.DataFrame(
    {
        "sku": ["AP-AN001-SM", "CL-SAN-LT-8-GRN", "TSH-BLU-MED"],
        "base_shipping": [3.50, 6.00, 4.25],
        "fuel_surcharge": [1.25, 2.00, 1.50],
    }
)
fees.set_index("sku", inplace=True)

total_delivery_cost = fees["base_shipping"] + fees["fuel_surcharge"]
print(total_delivery_cost)
# sku
# AP-AN001-SM        4.75
# CL-SAN-LT-8-GRN    8.00
# TSH-BLU-MED        5.75

In Polars, you join explicitly by the column that connects the data, like you would in SQL:

products = pl.DataFrame(
    {
        "sku": ["TSH-BLU-MED", "CL-SAN-LT-8-GRN", "AP-AN001-SM"],
        "price": [19.99, 29.99, 9.99],
    }
)

fees = pl.DataFrame(
    {
        "sku": ["AP-AN001-SM", "CL-SAN-LT-8-GRN", "TSH-BLU-MED"],
        "base_shipping": [3.50, 6.00, 4.25],
        "fuel_surcharge": [1.25, 2.00, 1.50],
    }
)

# Join on "sku"; add the two fee columns for total delivery cost
delivery_costs = (
    products.join(fees, on="sku")
    .with_columns(
        (pl.col("base_shipping") + pl.col("fuel_surcharge")).alias(
            "total_delivery_cost"
        )
    )
    .select(pl.col("sku"), pl.col("total_delivery_cost"))
)
print(delivery_costs)
# shape: (3, 2)
# ┌─────────────────┬─────────────────────┐
# │ sku             ┆ total_delivery_cost │
# │ ---             ┆ ---                 │
# │ str             ┆ f64                 │
# ╞═════════════════╪═════════════════════╡
# │ AP-AN001-SM     ┆ 4.75                │
# │ CL-SAN-LT-8-GRN ┆ 8.0                 │
# │ TSH-BLU-MED     ┆ 5.75                │
# └─────────────────┴─────────────────────┘

Sometimes it's a bit more typing, but I prefer Polars' explicit approach.

Assignment

SnackStack tracks appliance models in one table and warehouse stock in another. The rows don't always arrive in the same order, so the analytics team needs to align the tables by device_id.

Complete the build_inventory_value_report function. It accepts two Polars DataFrames: devices and inventory.