Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.
exam questions

Exam Certified Associate Developer for Apache Spark All Questions

View all questions & answers for the Certified Associate Developer for Apache Spark exam

Exam Certified Associate Developer for Apache Spark topic 1 question 22 discussion

Which of the following code blocks returns a new DataFrame from DataFrame storesDF where column storeId is of the type string?

  • A. storesDF.withColumn("storeId, cast(col("storeId"), StringType()))
  • B. storesDF.withColumn("storeId, col("storeId").cast(StringType()))
  • C. storesDF.withColumn("storeId, cast(storeId).as(StringType)
  • D. storesDF.withColumn("storeId, col(storeId).cast(StringType)
  • E. storesDF.withColumn("storeId, cast("storeId").as(StringType()))
Show Suggested Answer Hide Answer
Suggested Answer: B 🗳️

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
jds0
4 months ago
Selected Answer: B
Answer is B but with a typo: See code below (Spark 3.5.1): from pyspark.sql import SparkSession from pyspark.sql.functions import col, cast from pyspark.sql.types import StringType spark = SparkSession.builder.appName("MyApp").getOrCreate() data = [ (0, 3, 20000, "A"), (1, 1, 50000, "A"), (2, 2, 70000, "A"), ] storesDF = spark.createDataFrame(data, ["storeID", "customerSatisfaction", "sqft", "division"]) storesDF.withColumn("storeId", col("storeId").cast(StringType())).printSchema() # root # |-- storeId: string (nullable = true) # |-- customerSatisfaction: long (nullable = true) # |-- sqft: long (nullable = true) # |-- division: string (nullable = true)
upvoted 1 times
...
DataEngine
1 year ago
Anwer is B but it has a typo
upvoted 2 times
...
ZSun
1 year, 5 months ago
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
upvoted 1 times
...
dduque10
1 year, 6 months ago
Selected Answer: B
All answers are wrong because the first argument does not have the closing quotes :D, apart from that, it is B
upvoted 3 times
...
4be8126
1 year, 7 months ago
Selected Answer: B
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
...
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.

SaveCancel
Loading ...