简体   繁体   English

如何创建新的字符串列以从Spark中的时间戳提取整数?

[英]How to create new string column extracting integers from a timestamp in Spark?

I have a spark dataframe with a timestamp column, I want a new column which has strings in the format "YYYYMM". 我有一个带时间戳列的spark数据帧,我想要一个新列,其字符串格式为“ YYYYMM”。

I tried with: 我尝试过:

df.withColumn('year_month',year(col("timestamp")).cast("string")+month(col("timestamp")).cast("string"))

But if my timestamp is 2016-10-12, it returns as 2020 as YYYYMM. 但是,如果我的时间戳记是2016-10-12,则它在2020年返回为YYYYMM。

You can use date_format : 您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM