PySpark SQL Functions | instr method
PySpark SQL Functions'
instr(~) method returns a new PySpark Column holding the position of the first occurrence of the specified substring in each value of the specified column.
The position is not index-based, and starts from 1 instead of 0.
The column to perform the operation on.
The substring of which to check the position.
A PySpark DataFrame.
Consider the following PySpark DataFrame:
Getting the position of the first occurrence of a substring in PySpark Column
To get the position of the first occurrence of the substring
"B" in column
x, use the
Here, note the following:
2returned for the column value
"ABA"because the substring
"B"occurs in the 2nd position - remember, this method counts position from
if the substring does not exist in the string, then a value of
0is returned. This is the case for
"Cathy"because this string does not include
if the string is
null, then the result will also be