Read CSV Revenue by Country

Medium
File I/O
CSV
Joins
Aggregation

Read CSV Revenue by Country

Two files are involved: datasets/csv/orders.csv (one row per order) and datasets/csv/users.csv (one row per customer). Join them and report how much revenue every country produced.

What to do

  1. Read both CSV files. Their paths are handed to you as paths['orders'] and paths['users'].
  2. Keep only the orders with order_status == 'Completed'.
  3. Join the orders with the users on user_id and take the country of the customer.
  4. Sum total_amount per country and round the result to two decimals.
  5. Sort by revenue descending, then by country ascending.
  6. Return exactly these columns: country, revenue.

Signature

File questions receive the paths as a third argument:

from pyspark.sql import functions as F

def solve(spark, dfs, paths):
    orders = spark.read.csv(paths['orders'], header=True, inferSchema=True)
    users = spark.read.csv(paths['users'], header=True, inferSchema=True)
    # TODO: join, aggregate and sort
    return orders

The same data is also preloaded as dfs['orders'] and dfs['users'] if you prefer to skip the file reads.

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

2 Files
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[orders]
csv
600 rows
datasets/csv/orders.csv
order_id(int)
user_id(int)
order_date(timestamp)
total_amount(double)
shipping_fee(double)
payment_method(string)
order_status(string)
paths[users]
csv
250 rows
datasets/csv/users.csv
user_id(int)
first_name(string)
last_name(string)
email(string)
gender(string)
age(int)
country(string)
signup_date(date)

Return these columns

country(string)
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.