cast is a method belongs to class pyspark.sql.column
therefore, A C E are wrong. it should be dataframe.column.cast() or col('col_name').cast()
B is correct, with small typo
The correct code block to return a new DataFrame from DataFrame storesDF where column storeId is of the type string is:
storesDF.withColumn("storeId", col("storeId").cast(StringType()))
Option A has an extra quotation mark after "storeId" and is missing a closing parenthesis for the cast() function.
Option B correctly uses the cast() function to change the data type, but has a typo where "storeId" is repeated inside the string argument for the withColumn() function.
Option C is missing the col() function to reference the storeId column, and also has a typo with the closing parentheses for the cast() function.
Option D correctly references the storeId column using col(), but has a typo with the quotation marks and parentheses.
Option E has a syntax error where the cast() function is inside the quotation marks, and is also missing the col() function to reference the storeId column.
Therefore, the correct answer is B.
storesDF.withColumn("storeId", col("storeId").cast(StringType()))
upvoted 2 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
jds0
4 months agoDataEngine
1 year agoZSun
1 year, 5 months agodduque10
1 year, 6 months ago4be8126
1 year, 7 months ago