Unique Cities per Country

Easy
Aggregation

Unique Cities per Country

The customers table contains customer locations.

Return country, city_count with the number of distinct cities in each country. Sort by country ascending.

Browser Engine: Evaluates PySpark DataFrame & SQL logic locally in Pyodide WebAssembly.

Input data

1 DataFrame

These DataFrames are passed to solve(spark, dfs). Access one with dfs['name'].

df.customers10 rows
customer_id (int)country (string)city (string)
1IndiaMumbai
2IndiaDelhi
3IndiaMumbai
4USAAustin
5USAAustin
6USASeattle
7GermanyBerlin
8GermanyBerlin
9GermanyMunich
10CanadaToronto

Return these columns

country(string)
city_count(int)

Column names and order must match. Sort order is specified in the question.

Browser engine
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.