Read CSV Line Revenue by Product

Medium
File I/O
CSV
Aggregation
Arithmetic

Read CSV Line Revenue by Product

The file datasets/csv/order_items.csv holds one row per item of an order. A line is worth quantity * unit_price * (1 - discount / 100) - the discount is a percentage. Which products bring in the most money?

What to do

  1. Read the CSV file from disk. Its local path is handed to you as paths['order_items'].
  2. Add a column line_revenue with quantity * unit_price * (1 - discount / 100).
  3. Sum line_revenue per product_id and round the result to two decimals.
  4. Return the five products with the highest revenue.
  5. Sort by revenue descending, then by product_id ascending.
  6. Return exactly these columns: product_id, revenue.

Signature

File questions receive the path as a third argument:

from pyspark.sql import functions as F

def solve(spark, dfs, paths):
    items = spark.read.csv(paths['order_items'], header=True, inferSchema=True)
    # TODO: add the line revenue, aggregate and sort
    return items

The same data is also preloaded as dfs['order_items'] if you prefer to skip the file read.

This question runs in your browser - no sign-in needed. Press Run for the sample data and Submit for the hidden data.

Browser Engine: Evaluates PySpark DataFrame & SQL logic locally in Pyodide WebAssembly.

Input files

1 File
Loaded automatically

These files are loaded into this browser when you run. Their paths are handed to your code as the third argument: solve(spark, dfs, paths). Read one with spark.read.csv(paths['users'], header=True), or skip the read and use dfs['users'].

paths[order_items]
csv
1646 rows
datasets/csv/order_items.csv
order_item_id(int)
order_id(int)
product_id(int)
quantity(int)
unit_price(double)
discount(double)

Return these columns

product_id(int)
revenue(double)

Column names and order must match. Sort order is specified in the question.

Browser engine
Local files
Loading Python editor…
Click Run to try the sample cases, or Submit to check the hidden cases. First run caches the browser Python engine (~8–15s once); all code stays on your device.