pyspark将数据帧列从时间戳转换为字符串"YYYY-MM-DD";格式 [英] pyspark convert dataframe column from timestamp to string of "YYYY-MM-DD" format
本文介绍了pyspark将数据帧列从时间戳转换为字符串"YYYY-MM-DD";格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在pyspark中,有没有一种方法可以将timestamp数据类型的dataframe列转换为格式为'YYYY-MM-DD'的字符串?
In pyspark is there a way to convert a dataframe column of timestamp datatype to a string of format 'YYYY-MM-DD' format?
推荐答案
如果您的列的schema
为
root
|-- date: timestamp (nullable = true)
然后,您可以使用from_unixtime
函数将 timestamp 转换为 bigInt timestamp 转换为 string >使用unix_timestamp
函数作为
Then you can use from_unixtime
function to convert the timestamp to string after converting the timestamp to bigInt using unix_timestamp
function as
from pyspark.sql import functions as f
df.withColumn("date", f.from_unixtime(f.unix_timestamp(df.date), "yyyy-MM-dd"))
您应该拥有
root
|-- date: string (nullable = true)
这篇关于pyspark将数据帧列从时间戳转换为字符串"YYYY-MM-DD";格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文