我有一个Spark DataFrame,我希望将其行值转换为单列。它是时间数据(列=小时)。 (示例见下文)。
到目前为止,DataFrame看起来像这样:
>>> newdf.show(2)
+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+------+------+------+------+------+------+------+------+------+-------+------+------+------+
|Date |temp0|temp1|temp2|temp3|temp4|temp5|temp6|temp7|temp8|temp9|temp10|temp11|temp12|temp13|temp14|temp15|temp16|temp17|temp18|temp19|temp20 |temp21|temp22|temp23|
+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+------+------+------+------+------+------+------+------+------+-------+------+------+------+
|2012-01-07|25 |29 |15 |null |null |null |4 |39 |128 |65 |3 |3 |7 |1 |4 |1 |4 |3 |4 |6 |1 |3 |1 |2 |
|2012-01-08|16 |15 |8 |null |null |null |4 |39 |128 |65 |3 |3 |7 |1 |4 |1 |4 |3 |4 |6 |1 |3 |1 |2 |
+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+------+------+------+------+------+------+------+------+------+-------+------+------+------+
目标DataFrame应该如下所示:
+---------------------+-------------+
| Date | temperature |
+---------------------+-------------+
| 2012-01-07 00:00:00 | 25 |
| 2012-01-07 01:00:00 | 29 |
| 2012-01-07 02:00:00 | 15 |
| 2012-01-07 03:00:00 | null |
| .... | .... |
| 2012-01-08 00:00:00 | 16 |
| 2012-01-08 01:00:00 | 15 |
+---------------------+-------------+
PySpark是否可能?我已经测试过数据透视功能,但是无法给我想要的结构。每行应对应一个小时。
还有其他移调的可能性吗?
答案 0 :(得分:0)
步骤1:创建数据框,
import pyspark.sql.functions as F
df = sql.createDataFrame([
('2012-01-07',25 ,29 ,15 ,7 ,7 ,7 ,4 ,39 ,128 ,65 ,3 ,3 ,7 ,1 ,4 ,1 ,4 ,3 ,4 ,6 ,1 ,3 ,1 ,2 ),\
('2012-01-08',16 ,15 ,8 ,7 ,7 ,7 ,4 ,39 ,128 ,65 ,3 ,3 ,7 ,1 ,4 ,1 ,4 ,3 ,4 ,6 ,1 ,3 ,1 ,2 ),\
],[
'Date','temp0','temp1','temp2','temp3','temp4','temp5','temp6','temp7','temp8','temp9','temp10','temp11','temp12','temp13','temp14','temp15','temp16','temp17','temp18','temp19','temp20' ,'temp21','temp22','temp23'
])
第2步:展开各列并合并以创建时间戳记
def _combine(x,y):
d = str(x) + ' {}:00:00'.format(y)
return d
combine = F.udf(lambda x,y: _combine(x,y))
cols, dtypes = zip(*((c, t) for (c, t) in df.dtypes if c not in ['Date']))
kvs = F.explode(F.array([
F.struct(F.lit(c).alias("key"), F.col(c).alias("val")) for c in cols])).alias("kvs")
df = df.select(['Date'] + [kvs])\
.select(['Date'] + ["kvs.key", F.col("kvs.val").alias('temperature')])\
.withColumn('key', F.regexp_replace('key', 'temp', ''))\
.withColumn('Date', combine('Date','key').cast('timestamp'))\
.drop('key')
df.show()
这给出了输出,
+-------------------+-----------+
| Date|temperature|
+-------------------+-----------+
|2012-01-07 00:00:00| 25|
|2012-01-07 01:00:00| 29|
|2012-01-07 02:00:00| 15|
|2012-01-07 03:00:00| 7|
|2012-01-07 04:00:00| 7|
|2012-01-07 05:00:00| 7|
|2012-01-07 06:00:00| 4|
|2012-01-07 07:00:00| 39|
|2012-01-07 08:00:00| 128|
|2012-01-07 09:00:00| 65|
|2012-01-07 10:00:00| 3|
|2012-01-07 11:00:00| 3|
|2012-01-07 12:00:00| 7|
|2012-01-07 13:00:00| 1|
|2012-01-07 14:00:00| 4|
|2012-01-07 15:00:00| 1|
|2012-01-07 16:00:00| 4|
|2012-01-07 17:00:00| 3|
|2012-01-07 18:00:00| 4|
|2012-01-07 19:00:00| 6|
|2012-01-07 20:00:00| 1|
|2012-01-07 21:00:00| 3|
|2012-01-07 22:00:00| 1|
|2012-01-07 23:00:00| 2|
|2012-01-08 00:00:00| 16|
|2012-01-08 01:00:00| 15|
|2012-01-08 02:00:00| 8|
|2012-01-08 03:00:00| 7|
|2012-01-08 04:00:00| 7|
|2012-01-08 05:00:00| 7|
|2012-01-08 06:00:00| 4|
|2012-01-08 07:00:00| 39|
|2012-01-08 08:00:00| 128|
|2012-01-08 09:00:00| 65|
|2012-01-08 10:00:00| 3|
|2012-01-08 11:00:00| 3|
|2012-01-08 12:00:00| 7|
|2012-01-08 13:00:00| 1|
|2012-01-08 14:00:00| 4|
|2012-01-08 15:00:00| 1|
|2012-01-08 16:00:00| 4|
|2012-01-08 17:00:00| 3|
|2012-01-08 18:00:00| 4|
|2012-01-08 19:00:00| 6|
|2012-01-08 20:00:00| 1|
|2012-01-08 21:00:00| 3|
|2012-01-08 22:00:00| 1|
|2012-01-08 23:00:00| 2|
+-------------------+-----------+