我试图将几个列的最小值放到一个单独的列中。(创建min列)。操作非常直接,但我未能找到正确的功能:
A B分钟
1 2 1
2 1 1
3 1 1
1 4 1
非常感谢你的帮助!
发布于 2018-08-22 20:22:27
您可以使用least函数
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|
#+---+---+---+如果您有一个列名列表:
cols = ['A', 'B']
df.withColumn('min', least(*cols))同样地,在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|
+---+---+---+如果列存储在Seq中:
val cols = Seq("A", "B")
df.withColumn("min", least(cols.head, cols.tail: _*))https://stackoverflow.com/questions/51974475
复制相似问题