Read JSON Review Averages by Product

Medium
File I/O
JSON
Aggregation

Read JSON Review Averages by Product

The file datasets/json/product_reviews.json holds one object per review. Which products are rated best - but only if enough people reviewed them?

What to do

  1. Read the JSON file from disk. Its local path is handed to you as paths['product_reviews'].
  2. Group the rows by product_id and collect the number of reviews and the average rating (rounded to two decimals).
  3. Keep only the products with at least five reviews.
  4. Return the ten best rated products.
  5. Sort by avg_rating descending, then by product_id ascending.
  6. Return exactly these columns: product_id, review_count, avg_rating.

Signature

File questions receive the path as a third argument:

from pyspark.sql import functions as F

def solve(spark, dfs, paths):
    reviews = spark.read.json(paths['product_reviews'])
    # TODO: aggregate, filter and sort
    return reviews

The same data is also preloaded as dfs['product_reviews'] 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[product_reviews]
json
800 rows
datasets/json/product_reviews.json
review_id(int)
product_id(int)
user_id(int)
rating(int)
review_text(string)
review_date(date)

Return these columns

product_id(bigint)
review_count(bigint)
avg_rating(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.