0
0
Apache Sparkdata~10 mins

Adding and renaming columns 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 add a new column 'age_plus_one' by adding 1 to the 'age' column.

Apache Spark
df = df.withColumn('age_plus_one', df['age'] [1] 1)
Drag options to blanks, or click blank then click option'
A-
B*
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' will subtract 1 instead of adding.
Using '*' or '/' will multiply or divide, which is not correct here.
2fill in blank
medium

Complete the code to rename the column 'age_plus_one' to 'age_next_year'.

Apache Spark
df = df.withColumnRenamed('age_plus_one', [1])
Drag options to blanks, or click blank then click option'
A'age_plus_one'
B'age_next_year'
C'age'
D'next_year_age'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the old column name as the new name.
Forgetting to put the new name in quotes.
3fill in blank
hard

Fix the error in the code to add a new column 'double_age' that doubles the 'age' column values.

Apache Spark
df = df.withColumn('double_age', df['age'] [1] 2)
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds 2 instead of doubling.
Using '-' or '/' changes the value incorrectly.
4fill in blank
hard

Fill both blanks to add a new column 'age_squared' that squares the 'age' column values.

Apache Spark
df = df.withColumn('age_squared', df['age'] [1] [2])
Drag options to blanks, or click blank then click option'
A**
B+
C2
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' and '2' multiplies but is less clear for squaring.
Using '+' adds 2 instead of squaring.
5fill in blank
hard

Fill all three blanks to rename 'age_squared' to 'age_power_two' and add a new column 'age_plus_five' by adding 5 to 'age'.

Apache Spark
df = df.withColumnRenamed([1], [2]).withColumn('age_plus_five', df['age'] [3] 5)
Drag options to blanks, or click blank then click option'
A'age_squared'
B'age_plus_five'
C+
D'age_power_two'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up old and new column names.
Using wrong operator like '*' instead of '+'.