我的问题是如何将一列拆分为多列。
我不知道为什么df.toPandas()
不起作用。
例如,我想将“df_test”更改为“df_test2”。 我看到许多使用pandas模块的例子。还有别的办法吗? 提前谢谢你。
df_test = sqlContext.createDataFrame([
(1, '14-Jul-15'),
(2, '14-Jun-15'),
(3, '11-Oct-15'),
], ('id', 'date'))
测向测试2
id day month year
1 14 Jul 15
2 14 Jun 15
1 11 Oct 15
火花>;=2.2
您可以跳过
unix_timestamp
,转换并使用to_date
或to_timestamp
:然后应用下面显示的其他日期时间函数。
火花<;2.2
不能在一个访问中派生多个顶级列。可以将结构或集合类型与UDF一起使用,如下所示:
但它不仅在PySpark中相当冗长,而且价格昂贵。
对于基于日期的转换,您只需使用内置函数:
类似地,您可以使用
regexp_extract
分割日期字符串。另见Derive multiple columns from a single column in a Spark DataFrame
注意:
如果对SPARK-11724使用未修补的版本,则需要在
unix_timestamp(...)
之后和cast("timestamp")
之前进行更正。相关问题 更多 >
编程相关推荐