【问题标题】:spark scala dataframe groupBy and orderByspark scala 数据框 groupBy 和 orderBy
【发布时间】:2019-10-18 17:56:22
【问题描述】:

我需要计算第一列和第二列中pair的出现次数并按降序排序。如果计数相同,则首先在第二列中列出数字最小的对。

除了决胜局部分之外,以下内容有效。 _c1 中的第一行应该是 1,2,3 bc 2 小于 4 并且它们的计数相同。我如何按 count desc 和 c2 asc 排序?

new_df.groupBy($"_c0",$"_c1").count().orderBy($"count".desc).limit(10).show()
+---+---+-----+
|_c0|_c1|count|
+---+---+-----+
|  1|  4|    3|
|  1|  2|    3|
|  4|  1|    2|
|  3|  1|    2|
|  3|  4|    2|
|  2|  1|    2|
|  2|  4|    1|
|  1|  7|    1|
|  7|  2|    1|
|  2|  7|    1|
+---+---+-----+

【问题讨论】:

    标签: scala apache-spark-sql sql-order-by


    【解决方案1】:

    尝试在 order by 子句中添加 Desc 的 count 和 asc 的 _c2。

    new_df.groupBy($"_c0",$"_c1").count().orderBy($"count".desc, $"c2".asc).limit(10).show()
    

    按照您希望应用规则的顺序执行此操作。在上面的例子中,它会先按count排序,然后按c2排序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多