PySpark SQL 函數的 least(~)
將多個列作為輸入,並返回一個 PySpark 列,其中保存輸入列的每一行的最小值。
參數
1.*cols
| string
或 Column
將比較其行值以進行檢查的輸入列。
返回值
PySpark 數據幀。
例子
考慮以下PySpark DataFrame:
df = spark.createDataFrame([[20,10], [30,50], [40,90]], ["A", "B"])
df.show()
+---+---+
| A| B|
+---+---+
| 20| 10|
| 30| 50|
| 40| 90|
+---+---+
獲取PySpark中指定列的每一行的最小值
要獲取 A
和 B
列的每一行的最小值:
import pyspark.sql.functions as F
df.select(F.least("A","B")).show()
+-----------+
|least(A, B)|
+-----------+
| 10|
| 30|
| 40|
+-----------+
我們還可以傳遞 Column
對象而不是列標簽:
df.select(F.least(df.A,df.B)).show()
+-----------+
|least(A, B)|
+-----------+
| 10|
| 30|
| 40|
+-----------+
請注意,我們可以使用 withColumn(~)
附加由 least(~)
返回的 Column
:
df.withColumn("smallest", F.least("A","B")).show()
+---+---+--------+
| A| B|smallest|
+---+---+--------+
| 20| 10| 10|
| 30| 50| 30|
| 40| 90| 40|
+---+---+--------+
相關用法
- Python PySpark SQL Functions length方法用法及代碼示例
- Python PySpark SQL Functions lit方法用法及代碼示例
- Python PySpark SQL Functions last方法用法及代碼示例
- Python PySpark SQL Functions lower方法用法及代碼示例
- Python PySpark SQL Functions split方法用法及代碼示例
- Python PySpark SQL Functions repeat方法用法及代碼示例
- Python PySpark SQL Functions explode方法用法及代碼示例
- Python PySpark SQL Functions concat方法用法及代碼示例
- Python PySpark SQL Functions instr方法用法及代碼示例
- Python PySpark SQL Functions count_distinct方法用法及代碼示例
- Python PySpark SQL Functions dayofmonth方法用法及代碼示例
- Python PySpark SQL Functions date_add方法用法及代碼示例
- Python PySpark SQL Functions array方法用法及代碼示例
- Python PySpark SQL Functions concat_ws方法用法及代碼示例
- Python PySpark SQL Functions col方法用法及代碼示例
- Python PySpark SQL Functions translate方法用法及代碼示例
- Python PySpark SQL Functions dayofweek方法用法及代碼示例
- Python PySpark SQL Functions expr方法用法及代碼示例
- Python PySpark SQL Functions regexp_extract方法用法及代碼示例
- Python PySpark SQL Functions regexp_replace方法用法及代碼示例
- Python PySpark SQL Functions round方法用法及代碼示例
- Python PySpark SQL Functions countDistinct方法用法及代碼示例
- Python PySpark SQL Functions date_format方法用法及代碼示例
- Python PySpark SQL Functions collect_list方法用法及代碼示例
- Python PySpark SQL Functions upper方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 PySpark SQL Functions | least method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。