有没有办法将聚合函数应用于数据帧的所有(或列表)列groupBy?换句话说,有没有办法避免为每一列执行此操作:
df.groupBy("col1")
.agg(sum("col2").alias("col2"), sum("col3").alias("col3"), ...)
Run Code Online (Sandbox Code Playgroud) 当我尝试groupBy并获得max时,拥有这个数据帧我得到Column是不可迭代的:
linesWithSparkDF
+---+-----+
| id|cycle|
+---+-----+
| 31| 26|
| 31| 28|
| 31| 29|
| 31| 97|
| 31| 98|
| 31| 100|
| 31| 101|
| 31| 111|
| 31| 112|
| 31| 113|
+---+-----+
only showing top 10 rows
ipython-input-41-373452512490> in runlgmodel2(model, data)
65 linesWithSparkDF.show(10)
66
---> 67 linesWithSparkGDF = linesWithSparkDF.groupBy(col("id")).agg(max(col("cycle")))
68 print "linesWithSparkGDF"
69
/usr/hdp/current/spark-client/python/pyspark/sql/column.py in __iter__(self)
241
242 def __iter__(self):
--> 243 raise TypeError("Column is not iterable")
244
245 # string methods
TypeError: Column is …Run Code Online (Sandbox Code Playgroud) 我在 Scala 中有一个列名列表,例如
var cols = List("col1", "col2", "col3","col4")
Run Code Online (Sandbox Code Playgroud)
我还有一个包含这些列的数据框,但都是字符串。现在我想通过遍历数据框的列表或列来转换数据框的列,因为我的列列表非常大,我无法使用这么多.withColumn参数
提前致谢