PySpark SQL 函數的 max(~)
方法返回指定列中的最大值。
參數
1.col
| string
或 Column
要獲取最大值的列。
返回值
PySpark 列 (pyspark.sql.column.Column
)。
例子
考慮以下PySpark DataFrame:
df = spark.createDataFrame([["Alex", 25], ["Bob", 30]], ["name", "age"])
df.show()
+----+---+
|name|age|
+----+---+
|Alex| 25|
| Bob| 30|
+----+---+
獲取 PySpark 列的最大值
要獲得最大age
:
import pyspark.sql.functions as F
df.select(F.max("age")).show()
+--------+
|max(age)|
+--------+
| 30|
+--------+
要獲取整數形式的最大age
:
list_rows = df.select(F.max("age")).collect()
list_rows[0][0]
30
此處, collect()
方法返回 Row
對象的列表,在本例中長度為 1,因為 select(~)
返回的 PySpark DataFrame 隻有一行。 Row
對象的內容可以通過 [0]
訪問。
獲取PySpark中每組的最大值
考慮以下PySpark DataFrame:
df = spark.createDataFrame([["Alex", 20, "A"],\
["Bob", 30, "B"],\
["Cathy", 50, "A"]],
["name", "age", "class"])
df.show()
+-----+---+-----+
| name|age|class|
+-----+---+-----+
| Alex| 20| A|
| Bob| 30| B|
|Cathy| 50| A|
+-----+---+-----+
要獲取每個 class
的最大值 age
:
df.groupby("class").agg(F.max("age").alias("MAX AGE")).show()
+-----+-------+
|class|MAX AGE|
+-----+-------+
| A| 50|
| B| 30|
+-----+-------+
在這裏,我們使用 alias(~)
方法為聚合的 age
列分配標簽。
相關用法
- Python PySpark SQL Functions month方法用法及代碼示例
- Python PySpark SQL Functions mean方法用法及代碼示例
- Python PySpark SQL Functions min方法用法及代碼示例
- 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 lit方法用法及代碼示例
- Python PySpark SQL Functions upper方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 PySpark SQL Functions | max method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。