

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Polars
incomplete
2: Basic Operations
incomplete
3: Polars vs. Pandas
incomplete
4: Expression-Based Operations
incomplete
5: Lazy vs. Eager Execution
incomplete
6: No Index
incomplete
7: Filtering With No Index
incomplete
8: Index Alternatives
incomplete
9: Sorting
incomplete
10: Sorting Footguns
incomplete
11: Time-Based Operations
incomplete
12: Parquet
incomplete
13: Parquet With Polars
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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
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.
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.