PySpark 的 SQL 函數 first(~)
方法返回 PySpark DataFrame 的指定列的第一個值。
參數
1.col
| string
或 Column
對象
感興趣的列標簽或 Column
對象。
2. ignorenulls
| boolean
| optional
是否忽略空值。默認情況下,ignorenulls=False
。
返回值
PySpark SQL 列對象 (pyspark.sql.column.Column
)。
例子
考慮以下PySpark DataFrame:
columns = ["name", "age"]
data = [("Alex", 15), ("Bob", 20), ("Cathy", 25)]
df = spark.createDataFrame(data, columns)
df.show()
+-----+---+
| name|age|
+-----+---+
| Alex| 15|
| Bob| 20|
|Cathy| 25|
+-----+---+
獲取 PySpark DataFrame 中列的第一個值
要獲取 name
列的第一個值:
import pyspark.sql.functions as F
df.select(F.first(df.name)).show()
+-----------+
|first(name)|
+-----------+
| Alex|
+-----------+
獲取 PySpark DataFrame 中列的第一個非空值
考慮以下帶有空值的 PySpark DataFrame:
columns = ["name", "age"]
data = [("Alex", None), ("Bob", 20), ("Cathy", 25)]
df = spark.createDataFrame(data, columns)
df.show()
+-----+----+
| name| age|
+-----+----+
| Alex|null|
| Bob| 20|
|Cathy| 25|
+-----+----+
默認為ignorenulls=False
,這意味著無論是否為null
,都返回第一個值:
df.select(F.first(df.age)).show()
+----------+
|first(age)|
+----------+
| null|
+----------+
要返回第一個非空值:
df.select(F.first(df.age, ignorenulls=True)).show()
+----------+
|first(age)|
+----------+
| 20|
+----------+
獲取PySpark中每組的第一個值
first(~)
方法在聚合中也很有用。考慮以下PySpark DataFrame:
columns = ["name", "class"]
data = [("Alex", "A"), ("Alex", "B"), ("Bob", None), ("Bob", "A"), ("Cathy", "C")]
df = spark.createDataFrame(data, columns)
df.show()
+-----+-----+
| name|class|
+-----+-----+
| Alex| A|
| Alex| B|
| Bob| null|
| Bob| A|
|Cathy| C|
+-----+-----+
要獲取每個聚合的第一個值:
df.groupby("name").agg(F.first("class")).show()
+-----+------------+
| name|first(class)|
+-----+------------+
| Alex| A|
| Bob| null|
|Cathy| C|
+-----+------------+
在這裏,我們按 name
進行分組,然後對於每個組,我們獲取 class
列中出現的第一個值。
相關用法
- 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方法用法及代碼示例
- Python PySpark SQL Functions length方法用法及代碼示例
- Python PySpark SQL Functions dayofyear方法用法及代碼示例
- Python PySpark SQL Functions trim方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 PySpark SQL Functions | first method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。