pyspark.sql.functions.split_part#
- pyspark.sql.functions.split_part(src, delimiter, partNum)[source]#
Splits str by delimiter and return requested part of the split (1-based). If any input is null, returns null. if partNum is out of range of split parts, returns empty string. If partNum is 0, throws an error. If partNum is negative, the parts are counted backward from the end of the string. If the delimiter is an empty string, the str is not split.
Added in version 3.5.0.
- Parameters:
- src
Columnor column name A column of string to be split. A column that evaluates to a string.
- delimiter
Columnor column name A column of string, the delimiter used for split. A column that evaluates to a string.
- partNum
Columnor column name The requested part of the split (1-based). A column that evaluates to an integer.
- src
Examples
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([("11.12.13", ".", 3,)], ["a", "b", "c"]) >>> df.select("*", sf.split_part("a", "b", "c")).show() +--------+---+---+-------------------+ | a| b| c|split_part(a, b, c)| +--------+---+---+-------------------+ |11.12.13| .| 3| 13| +--------+---+---+-------------------+
>>> df.select("*", sf.split_part(df.a, df.b, sf.lit(-2))).show() +--------+---+---+--------------------+ | a| b| c|split_part(a, b, -2)| +--------+---+---+--------------------+ |11.12.13| .| 3| 12| +--------+---+---+--------------------+