Count Rows per City
PYSPARK coding challenge · Difficulty: easy · Topic: GroupBy Aggregations · +50 XP
Problem
Return df_result with each city and the number of employees in it, column named count. Order by city ascending.
Example Input - `employees`
| name | city | | --- | --- | | Asha | Pune | | Ravi | Delhi | | Meera | Pune |
Expected Output
| city | count | | --- | --- | | Delhi | 1 | | Pune | 2 |
Notes
- The DataFrame is created for you - do not recreate it
- Build a DataFrame called
df_resultand finish withdf_result.show()
What this PYSPARK challenge teaches you
“Count Rows per City” is a easy-level PYSPARK challenge focused on GroupBy Aggregations. Working through it gives you hands-on practice with beginner — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- beginner
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- groupBy() followed by count() gives one row per group with a count column.
- employees.groupBy('city').count(), then .orderBy('city') so the rows come out in a fixed order.
How to practise it on PySpark.in
Open the challenge, write your PySpark code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related PYSPARK challenges
- Calculate Total Sales and Average Order Value by Customer
- Analyze Sales Data to Find Top Performers by Region
- Fix the Broken Pipeline
- Optimize the 100M-Row Join
- Parse Apache Logs with Regex
- Optimize Small DataFrame Join with Broadcast
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYSPARK environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers GroupBy Aggregations.