我有架构的数据框
root
|-- _id: long (nullable = true)
|-- data: array (nullable = true)
|-- element: struct (containsNull = true)
| | |-- k: string (nullable = true)
| | |-- v: string (nullable = true)
|-- c : string (nullable = true)
df.show(5)
---------------------------------------
_id | data |c
1 |[[key1,key2,key3,key4,key5],[value1,value2,value3,value4,value5]] |c1
-----------------------------------------------------------------------------
2 |[ [key1,key3,key2,key6],[value11,value31,value2,value61] ] |c2
-----------------------------------------------------------------------------
3 | [[key7,key1,key3,key8,key9],[value7,value1,value3,value8,value91]]|c3
-----------------------------------------------------------------------------
4 |[key3,key2,key4,key5,key10],[value32,value23,value43,value10]] |c4
------------------------------------------------------------------------------
5 |[[key1 ,key2,key4,key10],[value1,value23,value42,value101]] |c1
.
.
.
.
我想知道是否可以获得此结果以及我必须如何进行
_id|key1 |key2 |key3 |key4 |key5 |key6 |key7 |key8 |key9 |key10 ...
1|value1 |value2 |value3 |value4 |value5 | | | | |
----------------------------------------------------------------------------
2|value11|value2 |value31 | | |value6 | | |
---------------------------------------------------------------------
3|value1 | |value3 | | | |value7 |value8 |value91|
----------------------------------------------------------------------------
4| |value23|value32|value43| | | | |value10
---------------------------------------------------------------------------
5|value1 |value23| |value42| | | | | |value101
.
.
我试图使用爆炸,但我没有得到结果,我也尝试从第一个拖曳列构建一个阵列,但似乎很难。
答案 0 :(得分:0)
您需要将此数据框映射到每行包含数据的数据框,然后您可以使用适当的列名创建新的数据框
这应该指向正确的方向......
column_names = df.select("data").collect()[0][0]
data_df = map(lambda x: x[1],df.select("data").collect())
data_par = sc.parallelize(data_df)
new_df = spark.createDataFrame(data_par, column_names, 0.1)