我有一個 df 看起來像這樣:
----- ------- -----
|docId|vocabId|count|
----- ------- -----
| 3| 3| 600|
| 2| 3| 702|
| 1| 2| 120|
| 2| 5| 200|
| 2| 2| 500|
| 3| 1| 100|
| 3| 5| 2000|
| 3| 4| 122|
| 1| 3| 1200|
| 1| 1| 1000|
----- ------- -----
我想輸出 vocabId 的最大計數和它所屬的 docId。我這樣做了:
val wordCounts = docwords.groupBy("vocabId").agg(max($"count") as ("count"))
得到了這個:
------- ----------
|vocabId| count |
------- ----------
| 1| 1000|
| 3| 1200|
| 5| 2000|
| 4| 122|
| 2| 500|
------- ----------
如何在前面添加docId???它應該看起來像這樣(順序不重要):
----- ------- -----
|docId|vocabId|count|
----- ------- -----
| 2| 2| 500|
| 3| 5| 2000|
| 3| 4| 122|
| 1| 3| 1200|
| 1| 1| 1000|
----- ------- -----
uj5u.com熱心網友回復:
您可以使用 docwords over count 和 vocabId 進行自我加入,如下所示
val wordCounts = docwords.groupBy("vocabId").agg(max($"count") as ("count")).join(docwords,Seq("vocabId","count"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485126.html
