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…
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
- withColumn
- B
- withColumn
- C
- newColumn
- D
- withColumn
- E
- withColumn
How the community answered
(45 responses)- A71% (32)
- B2% (1)
- C16% (7)
- D9% (4)
- E2% (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
Community Discussion
No community discussion yet for this question.