我必須計算特徵。我一一計算:spark mergr公共列上的大量數據幀
dataframe1 = (id,feature1)
dataframe2 = (id,feature2)
dataframe3 = (id,feature3)
....
「id」是主鍵。我想得到如下結果:
dataframe = (id,feature1,feature2,...)
我不想多次使用「連接」。功能的數量約爲五十。 有沒有簡單的方法來實現它?
我必須計算特徵。我一一計算:spark mergr公共列上的大量數據幀
dataframe1 = (id,feature1)
dataframe2 = (id,feature2)
dataframe3 = (id,feature3)
....
「id」是主鍵。我想得到如下結果:
dataframe = (id,feature1,feature2,...)
我不想多次使用「連接」。功能的數量約爲五十。 有沒有簡單的方法來實現它?
如果您存儲dataframes在List
您可以慣用表達這種reduce
:
val dfs = List(dataframe1,dataframe2,dataframe3)
dfs.reduce {(x,y) => x.join(y, "id")}
//org.apache.spark.sql.DataFrame = [id: int, feature1: int, feature2: int, feature3: int]
這個東西可以做所有的dataframes的工會來完成,然後做重點GROUPBY如下:
val dataframe = dataframe1.selectExpr("id", "concat('1###',feature1)")
.unionAll(dataframe2.selectExpr("id", "concat('2###',feature2)"))
.unionAll(dataframe3.selectExpr("id", "concat('3###',feature3)"))
.rdd.groupBy(_.get(0))
.map(data => {
val tempData = data._2.map(x =>{
val temp = x.getString(1).split("###")
(temp(0),temp(1))
}).toMap
(data._1.toString,tempData.get("1"),tempData.get("2"),tempData.get("3"))
}).toDF("id","feature1","feature2","feature3")
聯合操作只會附加另一個數據幀,因此它會更快,只是一個邏輯上的改變。
我不認爲這是正確的......你的最終數據框將只有2列,你只是「堆疊」他們,你沒有做過一個聚合後,你的groupBy – maxymoo
這是很明顯,我們需要有總結,我已經給了一個提示,bdw我已經添加了完整的代碼。 –
如果(feature1,feature2,feature3,...)的順序被破壞?我的意思是「groupBy」會產生一些行,如(id,(feature2,feature1,feature3,...)) –
他不想多次使用連接,但是您的代碼將在兩個數據幀的每次交互中連接。 –