Read CSV Users by Country

Easy
File I/O
CSV
Filtering

Read CSV Users by Country

The file datasets/csv/users.csv holds one row per customer. Read it with Spark and return the customers from India who are 30 or older.

What to do

  1. Read the CSV file from disk. Its local path is handed to you as paths['users'].
  2. Keep the rows where country == 'India' and age >= 30.
  3. Sort by age descending, then by user_id ascending.
  4. Return exactly these columns: user_id, first_name, last_name, country, age.

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)
    return (users
            .filter((F.col('country') == 'India') & (F.col('age') >= 30))
            .select('user_id', 'first_name', 'last_name', 'country', 'age')
            .orderBy(F.col('age').desc(), F.col('user_id')))

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]
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

user_id(int)
first_name(string)
last_name(string)
country(string)
age(int)

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.