2016-07-16 81 views
0

我的數據是這樣的:如何兩個傳遞一維數據兩個二維

2014 a 1 
2015 b 2 
2014 a 2 
2015 c 4 
2014 b 2 

如何將其傳送到:在星火

 a b c 
2014 3 2 0 
2015 0 2 4 

。 謝謝。

+0

@Ares我的類型和它們fullOuterJoin濾波器的protopyical應用。 – mqshen

回答

2

這是一個透視表

df.show() 
// 
//+------+------+----+ 
//|letter|number|year| 
//+------+------+----+ 
//|  a|  1|2014| 
//|  b|  2|2015| 
//|  a|  2|2014| 
//|  c|  4|2015| 
//|  b|  2|2014| 
//+------+------+----+ 
val pivot = df.groupBy("year") 
.pivot("letter") 
.sum("number") 
.na.fill(0,Seq("a")) 
.na.fill(0,Seq("c")) 

pivot.show() 
//+----+---+---+---+ 
//|year| a| b| c| 
//+----+---+---+---+ 
//|2014| 3| 2| 0| 
//|2015| 0| 2| 4| 
//+----+---+---+---+