PySpark SQL 函數的 instr(~) 方法返回一個新的 PySpark 列,該列保存指定列的每個值中指定子字符串第一次出現的位置。
警告
該位置不基於索引,並且從 1 而不是 0 開始。
參數
1.str | string 或 Column
要執行操作的列。
2. substr | string
要檢查其位置的子字符串。
返回值
PySpark 數據幀。
例子
考慮以下PySpark DataFrame:
df = spark.createDataFrame([("ABA",), ("BBB",), ("CCC",), (None,)], ["x",])
df.show()
+----+
|   x|
+----+
| ABA|
| BBB|
| CCC|
|null|
+----+獲取PySpark列中第一次出現子字符串的位置
要獲取子字符串 "B" 在 x 列中第一次出現的位置,請使用 instr(~) 方法:
df.select(F.instr("x", "B")).show()
+-----------+
|instr(x, B)|
+-----------+
|          2|
|          1|
|          0|
|       null|
+-----------+在此,請注意以下事項:
- 
我們看到 2為列值"ABA"返回,因為子字符串"B"出現在第二個位置 - 請記住,此方法從1而不是0計算位置。
- 
如果字符串中不存在該子字符串,則返回值 0。"Cathy"就是這種情況,因為該字符串不包含"B"。
- 
如果字符串是 null,那麽結果也將是null。
相關用法
- Python PySpark SQL Functions isnan方法用法及代碼示例
- Python PySpark SQL Functions split方法用法及代碼示例
- Python PySpark SQL Functions repeat方法用法及代碼示例
- Python PySpark SQL Functions explode方法用法及代碼示例
- Python PySpark SQL Functions concat方法用法及代碼示例
- 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 | instr method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
