ML Engineer MasterClass (October) | 4 seats left

Spark · Conditional columns
AmazonAmazon Analytics
Amazon · Clean and Shape Data

Conditional columns

Label order lines with explicit boundaries, branch precedence, and fallbacks.

Step 1 of 6 · Learn

Classify each line

when checks each order line; otherwise supplies the fallback. The example labels line_total of at least 100 as High. line_total includes every unit on that line, not the entire order or a single unit. These labels are rules for this exercise.

Lesson reference: PySpark and Scala

Classify each line

when checks each order line; otherwise supplies the fallback. The example labels line_total of at least 100 as High. line_total includes every unit on that line, not the entire order or a single unit. These labels are rules for this exercise.

PySpark example

from pyspark.sql.functions import col, when

result = order_items.withColumn(
    "line_band", when(col("line_total") >= 100, "High").otherwise("Standard")
).select("order_item_id", "line_band").orderBy("order_item_id")
result.show()

Scala example

import org.apache.spark.sql.functions._

val result = order_items.withColumn(
    "line_band", when(col("line_total") >= 100, "High").otherwise("Standard")
).select("order_item_id", "line_band").orderBy("order_item_id")
result.show(false)

Order your conditions

Chained when uses the first matching branch. Quantity at least three is Multi-unit; among other lines, totals at least 120 are Review and the rest Standard. Line 6 matches both conditions, so the first branch determines its label.

PySpark example

from pyspark.sql.functions import col, when

result = order_items.withColumn(
    "packing", when(col("quantity") >= 3, "Multi-unit")
    .when(col("line_total") >= 120, "Review")
    .otherwise("Standard")
).select("order_item_id", "packing").orderBy("order_item_id")
result.show()

Scala example

import org.apache.spark.sql.functions._

val result = order_items.withColumn(
    "packing", when(col("quantity") >= 3, "Multi-unit")
    .when(col("line_total") >= 120, "Review")
    .otherwise("Standard")
).select("order_item_id", "packing").orderBy("order_item_id")
result.show(false)

Choose a fallback

Without otherwise, unmatched rows receive null. The example labels quantity one as Single and everything else Other. All quantities here are positive; in a dataset with nulls, a null condition would also take the fallback.

PySpark example

from pyspark.sql.functions import col, when

result = order_items.withColumn(
    "quantity_label", when(col("quantity") == 1, "Single").otherwise("Other")
).select("order_item_id", "quantity_label").orderBy("order_item_id")
result.show()

Scala example

import org.apache.spark.sql.functions._

val result = order_items.withColumn(
    "quantity_label", when(col("quantity") === 1, "Single").otherwise("Other")
).select("order_item_id", "quantity_label").orderBy("order_item_id")
result.show(false)
example.pyPySpark
1. Read order_itemsStart with 9 records; preserve the original DataFrame.
2. Classify each lineApply the displayed expression to the source records.
3. Inspect the resultLine 4 totals 50; line 8 totals 49.99. The inclusive boundary separates them.
Source order_items9 rows
order_item_idorder_idproduct_idquantityline_total
11001201159.5
21001202130
31002203299
41002206150
51003204135
610042033149.97
71004205170.02
81005203149.99
910062012120
Classify each line
Result9 rows
order_item_idline_band
1Standard
2Standard
3Standard
4Standard
5Standard
6High
7Standard
8Standard
9High
Line 4 totals 50; line 8 totals 49.99. The inclusive boundary separates them.

The example is loaded in the editor. Run it as written, then try a small change.

Runs on the Spark backend. First startup may take a moment.

Run your code to see the result.