Use the withColumn method with the new column name and the expression or value for the column. Example: df.withColumn('new_col', df['existing_col'] + 1)
Click to reveal answer
beginner
What does the withColumnRenamed method do in Spark?
It renames an existing column in a DataFrame. Example: df.withColumnRenamed('old_name', 'new_name') changes the column name from old_name to new_name.
Click to reveal answer
intermediate
Can you chain multiple withColumn calls to add several columns?
Yes, you can chain withColumn calls to add or modify multiple columns. Example: df.withColumn('col1', expr1).withColumn('col2', expr2)
Click to reveal answer
intermediate
What happens if you use withColumn with a column name that already exists?
The existing column is replaced with the new values or expression provided. This is useful for modifying columns.
Click to reveal answer
intermediate
Is it possible to rename multiple columns at once in Spark DataFrame?
Spark does not have a built-in method to rename multiple columns at once. You can chain multiple withColumnRenamed calls or use a loop to rename columns.
Click to reveal answer
Which method adds a new column to a Spark DataFrame?
Adrop
BwithColumnRenamed
Cselect
DwithColumn
✗ Incorrect
withColumn adds or replaces a column in a Spark DataFrame.
What does withColumnRenamed('old', 'new') do?
ARenames column 'old' to 'new'
BAdds a new column named 'new'
CDeletes column 'old'
DDuplicates column 'old' as 'new'
✗ Incorrect
It renames the column named 'old' to 'new'.
If you call withColumn on an existing column name, what happens?
AAn error occurs
BThe column is duplicated
CThe existing column is replaced
DNothing changes
✗ Incorrect
The existing column is replaced with the new values or expression.
How can you rename multiple columns in Spark DataFrame?
AUse a single <code>withColumnRenamed</code> with multiple names
A. withColumnRenamed cannot rename columns; use withColumn instead
B. Column 'Name' does not exist; use correct case 'name' in withColumnRenamed
C. Syntax error: missing parentheses in withColumnRenamed
D. No issue; code runs correctly and renames column
Solution
Step 1: Check column names in DataFrame
The DataFrame has columns 'id' and 'name' (lowercase). The code tries to rename 'Name' (uppercase N) which does not exist.
Step 2: Fix the column name case
Change withColumnRenamed("Name", "first_name") to withColumnRenamed("name", "first_name") to match exact column name.
Final Answer:
Column 'Name' does not exist; use correct case 'name' in withColumnRenamed -> Option B
Quick Check:
Column names are case-sensitive in Spark [OK]
Hint: Match exact column name case when renaming [OK]
Common Mistakes:
Ignoring case sensitivity of column names
Using withColumn instead of withColumnRenamed to rename
Assuming code runs without error despite wrong column name
5. You have a DataFrame df with columns ['id', 'score']. You want to add a new column grade based on score: if score ≥ 90 then 'A', else 'B'. Then rename score to exam_score. Which code correctly does this?
hard
A. df2 = df.withColumn('grade', when(df.score >= 90, 'A').otherwise('B')).renameColumn('score', 'exam_score')
B. df2 = df.withColumnRenamed('score', 'exam_score').withColumn('grade', when(df.score >= 90, 'A').otherwise('B'))
C. from pyspark.sql.functions import when
df2 = df.withColumn('grade', when(df.score >= 90, 'A').otherwise('B')).withColumnRenamed('score', 'exam_score')
D. df2 = df.withColumn('grade', when(df.score > 90, 'A').otherwise('B')).withColumnRenamed('score', 'exam_score')
Solution
Step 1: Add 'grade' column with condition on 'score'
Use when to assign 'A' if score ≥ 90, else 'B'. This is done with withColumn('grade', when(df.score >= 90, 'A').otherwise('B')).
Step 2: Rename 'score' column to 'exam_score'
Use withColumnRenamed('score', 'exam_score') to rename the column after adding 'grade'. The order matters to keep references correct.
Step 3: Check other options
df2 = df.withColumnRenamed('score', 'exam_score').withColumn('grade', when(df.score >= 90, 'A').otherwise('B')) uses old column name after renaming, causing error. df2 = df.withColumn('grade', when(df.score >= 90, 'A').otherwise('B')).renameColumn('score', 'exam_score') uses non-existent renameColumn. df2 = df.withColumn('grade', when(df.score > 90, 'A').otherwise('B')).withColumnRenamed('score', 'exam_score') uses > 90 instead of ≥ 90, which changes logic.
Final Answer:
from pyspark.sql.functions import when
df2 = df.withColumn('grade', when(df.score >= 90, 'A').otherwise('B')).withColumnRenamed('score', 'exam_score') -> Option C
Quick Check:
Add column with when, then rename column [OK]
Hint: Add conditional column first, then rename original column [OK]
Common Mistakes:
Renaming column before adding new column using old name
Using wrong comparison operator (> instead of >=)
Using non-existent renameColumn method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.