Skip to content

34. Word Count

Difficulty: Medium · Topics: Arrays, Aggregations, Strings, Filtering & Selection

The classic Spark example. lines has a text column of space-separated words.

Count how often each word appears, ignoring case (lower-case everything). Return word, cnt ordered by cnt descending, then word ascending.

Row order matters for this problem. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

lines

idtext
1Spark is fast
2spark is fun
3PySpark is Spark

Expected output

wordcnt
is3
spark3
fast1
fun1
pyspark1

Hints

Hint 1F.split(F.lower("text"), " ") produces an array column.
Hint 2F.explode(...) turns each array element into its own row.
Hint 3Group by the exploded word and count.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance

Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice