nerdexam
Databricks

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #161

The code block shown below should return a DataFrame containing a column openDateString, a string representation of Java's SimpleDateFormat. Choose the response that correctly fills in the numbered…

The correct answer is A. 1. withColumn. The correct method to add or replace a column in a PySpark DataFrame is withColumn(columnName, columnExpression). Blank 1 must be withColumn. To convert a UNIX epoch integer (seconds since 1970-01-01) into a human-readable string matching Java's SimpleDateFormat (e.g., 'Sunday…

Data Transformation and Manipulation

Question

The code block shown below should return a DataFrame containing a column openDateString, a string representation of Java's SimpleDateFormat. Choose the response that correctly fills in the numbered blanks within the code block to complete this task. Note that column openDate is of type integer and represents a date in the UNIX epoch format - the number of seconds since midnight on January 1st, 1970. An example of Java's SimpleDateFormat is "Sunday, Dec 4, 2008 1:05 pm". A sample of storesDF is displayed below:

Code block:

storesDF.1("openDateString", 2(3, 4))

Options

  • A
    1. withColumn
  • B
    1. withColumn
  • C
    1. newColumn
  • D
    1. withColumn
  • E
    1. withColumn

How the community answered

(45 responses)
  • A
    71% (32)
  • B
    2% (1)
  • C
    16% (7)
  • D
    9% (4)
  • E
    2% (1)

Explanation

The correct method to add or replace a column in a PySpark DataFrame is withColumn(columnName, columnExpression). Blank 1 must be withColumn. To convert a UNIX epoch integer (seconds since 1970-01-01) into a human-readable string matching Java's SimpleDateFormat (e.g., 'Sunday, Dec 4, 2008 1:05 pm'), you first convert the integer to a timestamp using from_unixtime(col('openDate')), then format it using date_format(...) with the appropriate pattern string. The full correct call is: storesDF.withColumn('openDateString', date_format(from_unixtime(col('openDate')), 'EEEE, MMM d, yyyy h:mm a')). Options C is wrong because newColumn() is not a valid PySpark DataFrame method.

Topics

#Spark DataFrames#Date Time Operations#Spark SQL Functions#Data Type Conversion

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice