[英]How to create new string column extracting integers from a timestamp in Spark?
我有一個帶時間戳列的spark數據幀,我想要一個新列,其字符串格式為“ YYYYMM”。
我嘗試過:
df.withColumn('year_month',year(col("timestamp")).cast("string")+month(col("timestamp")).cast("string"))
但是,如果我的時間戳記是2016-10-12,則它在2020年返回為YYYYMM。
您可以使用date_format
:
from pyspark.sql.functions import date_format
df.withColumn('year_month', date_format('timestamp', 'yyyyMM'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.