Read TSV Users by Country

Easy
File I/O
TSV
Aggregation

Read TSV Users by Country

The file datasets/tsv/users.tsv holds one row per customer. It is a tab separated file, so the columns are split by a tab character. Where do the customers live?

What to do

  1. Read the TSV file from disk. Its local path is handed to you as paths['users']. A TSV is a CSV with sep='\t'.
  2. Group the rows by country and count them.
  3. Return the ten countries with the most users.
  4. Sort by user_count descending, then by country ascending.
  5. Return exactly these columns: country, user_count.

Signature

File questions receive the path as a third argument:

from pyspark.sql import functions as F

def solve(spark, dfs, paths):
    users = spark.read.csv(paths['users'], header=True, inferSchema=True, sep='\t')
    # TODO: group by country, count the users and sort the result
    return users

The same data is also preloaded as dfs['users'] 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[users]
tsv
250 rows
datasets/tsv/users.tsv
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)
user_count(bigint)

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.