Lau*_*e D 18 scala max dataframe apache-spark
我想从我的数据框中访问特定列的最小值和最大值,但我没有列的标题,只有它的数字,所以我应该使用scala吗?
也许是这样的:
val q = nextInt(ncol) //we pick a random value for a column number
col = df(q)
val minimum = col.min()
Run Code Online (Sandbox Code Playgroud)
对不起,如果这听起来像一个愚蠢的问题,但我找不到关于这个问题的任何信息:/
Jus*_*ony 26
如何从元数据中获取列名称:
val selectedColumnName = df.columns(q) //pull the (q + 1)th column from the columns array
df.agg(min(selectedColumnName), max(selectedColumnName))
Run Code Online (Sandbox Code Playgroud)
Tau*_*das 21
您可以在分配变量时使用模式匹配:
import org.apache.spark.sql.functions.{min, max}
import org.apache.spark.sql.Row
val Row(minValue: Double, maxValue: Double) = df.agg(min(q), max(q)).head
Run Code Online (Sandbox Code Playgroud)
其中q是Column列的一个或一个名称(String).假设您的数据类型是Double.
sta*_*010 10
这是从具有列名的数据框中获取最小值和最大值的直接方法:
val df = Seq((1, 2), (3, 4), (5, 6)).toDF("A", "B")
df.show()
/*
+---+---+
| A| B|
+---+---+
| 1| 2|
| 3| 4|
| 5| 6|
+---+---+
*/
df.agg(min("A"), max("A")).show()
/*
+------+------+
|min(A)|max(A)|
+------+------+
| 1| 5|
+------+------+
*/
Run Code Online (Sandbox Code Playgroud)
如果要将最小值和最大值作为单独的变量获取,则可以将上述结果转换agg()为 aRow并用于Row.getInt(index)获取Row.
val min_max = df.agg(min("A"), max("A")).head()
// min_max: org.apache.spark.sql.Row = [1,5]
val col_min = min_max.getInt(0)
// col_min: Int = 1
val col_max = min_max.getInt(1)
// col_max: Int = 5
Run Code Online (Sandbox Code Playgroud)
您可以使用列号首先提取列名(通过索引df.columns),然后使用列名称聚合:
val df = Seq((2.0, 2.1), (1.2, 1.4)).toDF("A", "B")
// df: org.apache.spark.sql.DataFrame = [A: double, B: double]
df.agg(max(df(df.columns(1))), min(df(df.columns(1)))).show
+------+------+
|max(B)|min(B)|
+------+------+
| 2.1| 1.4|
+------+------+
Run Code Online (Sandbox Code Playgroud)
小智 5
使用火花函数 min 和 max,您可以找到数据框中任何列的最小值或最大值。
import org.apache.spark.sql.functions.{min, max}
val df = Seq((5, 2), (10, 1)).toDF("A", "B")
df.agg(max($"A"), min($"B")).show()
/*
+------+------+
|max(A)|min(B)|
+------+------+
| 10| 1|
+------+------+
*/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63582 次 |
| 最近记录: |