Spark SQL:将聚合函数应用于列列表 [英] Spark SQL: apply aggregate functions to a list of columns

查看:40
本文介绍了Spark SQL:将聚合函数应用于列列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行 groupBy 时,有没有办法将聚合函数应用于数据帧的所有(或一系列)列?换句话说,有没有办法避免对每一列都这样做:

Is there a way to apply an aggregate function to all (or a list of) columns of a dataframe, when doing a groupBy? In other words, is there a way to avoid doing this for every column:

df.groupBy("col1")
  .agg(sum("col2").alias("col2"), sum("col3").alias("col3"), ...)

推荐答案

有多种方法可以将聚合函数应用于多列.

There are multiple ways of applying aggregate functions to multiple columns.

GroupedData 类为最常用的函数提供了许多方法,包括countmaxminmeansum,可以直接使用如下:

GroupedData class provides a number of methods for the most common functions, including count, max, min, mean and sum, which can be used directly as follows:

  • Python:

  • Python:

df = sqlContext.createDataFrame(
    [(1.0, 0.3, 1.0), (1.0, 0.5, 0.0), (-1.0, 0.6, 0.5), (-1.0, 5.6, 0.2)],
    ("col1", "col2", "col3"))

df.groupBy("col1").sum()

## +----+---------+-----------------+---------+
## |col1|sum(col1)|        sum(col2)|sum(col3)|
## +----+---------+-----------------+---------+
## | 1.0|      2.0|              0.8|      1.0|
## |-1.0|     -2.0|6.199999999999999|      0.7|
## +----+---------+-----------------+---------+

  • 斯卡拉

  • Scala

    val df = sc.parallelize(Seq(
      (1.0, 0.3, 1.0), (1.0, 0.5, 0.0),
      (-1.0, 0.6, 0.5), (-1.0, 5.6, 0.2))
    ).toDF("col1", "col2", "col3")
    
    df.groupBy($"col1").min().show
    
    // +----+---------+---------+---------+
    // |col1|min(col1)|min(col2)|min(col3)|
    // +----+---------+---------+---------+
    // | 1.0|      1.0|      0.3|      0.0|
    // |-1.0|     -1.0|      0.6|      0.2|
    // +----+---------+---------+---------+
    

  • 可选地,您可以传递应聚合的列列表

    Optionally you can pass a list of columns which should be aggregated

    df.groupBy("col1").sum("col2", "col3")
    

    您还可以将带有列 a 的键和函数作为值传递给字典/映射:

    You can also pass dictionary / map with columns a the keys and functions as the values:

    • Python

    • Python

    exprs = {x: "sum" for x in df.columns}
    df.groupBy("col1").agg(exprs).show()
    
    ## +----+---------+
    ## |col1|avg(col3)|
    ## +----+---------+
    ## | 1.0|      0.5|
    ## |-1.0|     0.35|
    ## +----+---------+
    

  • 斯卡拉

  • Scala

    val exprs = df.columns.map((_ -> "mean")).toMap
    df.groupBy($"col1").agg(exprs).show()
    
    // +----+---------+------------------+---------+
    // |col1|avg(col1)|         avg(col2)|avg(col3)|
    // +----+---------+------------------+---------+
    // | 1.0|      1.0|               0.4|      0.5|
    // |-1.0|     -1.0|3.0999999999999996|     0.35|
    // +----+---------+------------------+---------+
    

  • 终于可以使用可变参数了:

    Finally you can use varargs:

    • Python

    • Python

    from pyspark.sql.functions import min
    
    exprs = [min(x) for x in df.columns]
    df.groupBy("col1").agg(*exprs).show()
    

  • 斯卡拉

  • Scala

    import org.apache.spark.sql.functions.sum
    
    val exprs = df.columns.map(sum(_))
    df.groupBy($"col1").agg(exprs.head, exprs.tail: _*)
    

  • 还有其他一些方法可以达到类似的效果,但大多数情况下这些方法应该绰绰有余.

    There are some other way to achieve a similar effect but these should more than enough most of the time.

    另见:

    这篇关于Spark SQL:将聚合函数应用于列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆