Bub*_*Gut 2 apache-spark apache-spark-sql
我试图将几列的最小值放入单独的列中。(创建min列)。操作非常简单,但我无法找到正确的函数:
AB min
1 2 1
2 1 1
3 1 1
1 4 1
非常感谢你的帮助!
您可以least在pyspark 中使用该功能:
from pyspark.sql.functions import least
df.withColumn('min', least('A', 'B')).show()
#+---+---+---+
#| A| B|min|
#+---+---+---+
#| 1| 2| 1|
#| 2| 1| 1|
#| 3| 1| 1|
#| 1| 4| 1|
#+---+---+---+
Run Code Online (Sandbox Code Playgroud)
如果您有列名列表:
cols = ['A', 'B']
df.withColumn('min', least(*cols))
Run Code Online (Sandbox Code Playgroud)
同样在Scala 中:
import org.apache.spark.sql.functions.least
df.withColumn("min", least($"A", $"B")).show
+---+---+---+
| A| B|min|
+---+---+---+
| 1| 2| 1|
| 2| 1| 1|
| 3| 1| 1|
| 1| 4| 1|
+---+---+---+
Run Code Online (Sandbox Code Playgroud)
如果列存储在 Seq 中:
val cols = Seq("A", "B")
df.withColumn("min", least(cols.head, cols.tail: _*))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3605 次 |
| 最近记录: |