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 24 discussion

The code block shown below should return a new DataFrame from DataFrame storesDF where column modality is the constant string "PHYSICAL", Assume DataFrame storesDF is the only defined language variable. Choose the response that correctly fills in the numbered blanks within the code block to complete this task.
Code block:
storesDF. _1_(_2_,_3_(_4_))

  • A. 1. withColumn
    2. "modality"
    3. col
    4. "PHYSICAL"
  • B. 1. withColumn
    2. "modality"
    3. lit
    4. PHYSICAL
  • C. 1. withColumn
    2. "modality"
    3. lit
    4. "PHYSICAL"
  • D. 1. withColumn
    2. "modality"
    3. SrtringType
    4. "PHYSICAL"
  • E. 1. newColumn
    2. modality
    3. SrtringType
    4. PHYSICAL
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

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: C
Answer is C Code: from pyspark.sql import SparkSession from pyspark.sql.functions import lit spark = SparkSession.builder.appName("MyApp").getOrCreate() data = [ (0, 3, 20, "A"), (1, 1, 50, "A"), (2, 2, 70, "A"), ] storesDF = spark.createDataFrame(data, ["storeID", "numberOfEmployees", "sqft", "division"]) storesDF.withColumn("modality", lit("PHYSICAL"))
upvoted 1 times
...
newusername
1 year, 2 months ago
Selected Answer: C
Correct
upvoted 1 times
...
4be8126
1 year, 7 months ago
lit and col are two functions in PySpark that are used to create or reference columns in a DataFrame. lit: This function is used to create a column with a literal value. It returns a Column expression of literal value. For example, lit(2) creates a Column with a value of 2. It can be useful when you want to add a new column to a DataFrame with a constant value for all rows. col: This function is used to reference an existing column in a DataFrame. It returns a Column expression that represents a column. For example, col("age") returns a Column expression that represents the "age" column in a DataFrame. It can be useful when you want to select, filter or transform an existing column in a DataFrame. In short, lit is used to create a new column with a constant value, while col is used to reference an existing column in a DataFrame.
upvoted 1 times
...
4be8126
1 year, 7 months ago
Selected Answer: C
Option C is the correct answer. Here's why: The withColumn function is used to add a new column to the DataFrame based on an existing column or a constant value. The first blank (_1_) should be replaced with withColumn to indicate that we want to add a new column. The second blank (_2_) should be replaced with the name of the column we want to add. In this case, we want to add a column called modality. The third blank (_3_) should be replaced with a function that will create the values for the new column. In this case, we want to create a column that has the constant value "PHYSICAL". The lit function can be used to create a column with a literal value. Finally, the fourth blank (_4_) should be replaced with the actual value we want to use for the new column. Since we want to use the string "PHYSICAL", it should be wrapped in quotation marks to indicate that it is a string. Therefore, option C correctly fills in the blanks to give us the following code block: storesDF.withColumn("modality", lit("PHYSICAL"))
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 ...