Read Parquet Products by Category

Medium
File I/O
Parquet
Aggregation

Read Parquet Products by Category

The file datasets/parquet/products.parquet holds one row per product in the columnar Parquet format. What does every category look like?

What to do

  1. Read the Parquet file from disk. Its local path is handed to you as paths['products'].
  2. Group the rows by category and collect the number of products and the average price (rounded to two decimals).
  3. Sort by product_count descending, then by category ascending.
  4. Return exactly these columns: category, product_count, avg_price.

Signature

File questions receive the path as a third argument:

from pyspark.sql import functions as F

def solve(spark, dfs, paths):
    products = spark.read.parquet(paths['products'])
    # TODO: aggregate and sort
    return products

The same data is also preloaded as dfs['products'] 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[products]
parquet
180 rows
datasets/parquet/products.parquet
product_id(int)
product_name(string)
category(string)
sub_category(string)
brand(string)
price(double)
stock_quantity(int)
created_at(timestamp)

Return these columns

category(string)
product_count(bigint)
avg_price(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.