0
0
Apache Sparkdata~10 mins

String functions in Spark in Apache Spark - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert the 'name' column to lowercase using Spark string functions.

Apache Spark
from pyspark.sql.functions import [1]
df_lower = df.select([1]("name").alias("name_lower"))
Drag options to blanks, or click blank then click option'
Alower
Bupper
Ctrim
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'upper' instead of 'lower' changes text to uppercase.
Using 'trim' removes spaces but does not change case.
2fill in blank
medium

Complete the code to remove spaces from the start and end of the 'city' column.

Apache Spark
from pyspark.sql.functions import [1]
df_trimmed = df.select([1]("city").alias("city_trimmed"))
Drag options to blanks, or click blank then click option'
Alower
Bconcat
Csubstring
Dtrim
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lower' changes case but does not remove spaces.
Using 'concat' joins strings but does not trim spaces.
3fill in blank
hard

Fix the error in the code to extract the first 3 characters from the 'code' column.

Apache Spark
from pyspark.sql.functions import substring
df_sub = df.select(substring("code", [1], 3).alias("code_sub"))
Drag options to blanks, or click blank then click option'
A3
B4
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting substring at 0 causes an error or empty result.
Starting at 3 or 4 skips the first characters.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 4 characters.

Apache Spark
words = ["apple", "bat", "carrot", "dog"]
lengths = { [1] : len([1]) for [1] in words if len([1]) [2] 4 }
Drag options to blanks, or click blank then click option'
Aword
B>
C<
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' filters words shorter than 4.
Using 'len' as a variable name causes confusion.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words containing the letter 'a'.

Apache Spark
words = ["apple", "bat", "carrot", "dog"]
result = { [1] : [2] for [3] in words if 'a' in [3] }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Cword
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word.lower()' instead of 'word.upper()' changes case incorrectly.
Using 'word' as key does not convert to uppercase.