Fra*_*u P 7 apache-spark apache-spark-sql pyspark
我需要使用 pyspark 从时间戳(这是数据帧中的一列)中提取时间。假设这是时间戳 2019-01-03T18:21:39 ,我只想提取时间“18:21:39”,以便它始终以这种方式显示“01:01:01”
df = spark.createDataFrame(["2020-06-17T00:44:30","2020-06-17T06:06:56","2020-06-17T15:04:34"],StringType()).toDF('datetime')
df=df.select(df['datetime'].cast(TimestampType()))
Run Code Online (Sandbox Code Playgroud)
我尝试了如下但没有得到预期的结果
df1=df.withColumn('time',concat(hour(df['datetime']),lit(":"),minute(df['datetime']),lit(":"),second(df['datetime'])))
display(df1)
Run Code Online (Sandbox Code Playgroud)
+-------------------+-------+
| datetime| time|
+-------------------+-------+
|2020-06-17 00:44:30|0:44:30|
|2020-06-17 06:06:56| 6:6:56|
|2020-06-17 15:04:34|15:4:34|
+-------------------+-------+
Run Code Online (Sandbox Code Playgroud)
我的结果是这样的6:6:56,但我希望它们是06:06:56
Lam*_*nus 13
使用date_format函数。
from pyspark.sql.types import StringType
df = spark \
.createDataFrame(["2020-06-17T00:44:30","2020-06-17T06:06:56","2020-06-17T15:04:34"], StringType()) \
.toDF('datetime')
from pyspark.sql.functions import date_format
q = df.withColumn('time', date_format('datetime', 'HH:mm:ss'))
>>> q.show()
+-------------------+--------+
| datetime| time|
+-------------------+--------+
|2020-06-17T00:44:30|00:44:30|
|2020-06-17T06:06:56|06:06:56|
|2020-06-17T15:04:34|15:04:34|
+-------------------+--------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23278 次 |
| 最近记录: |