pyspark.sql.functions.repeat#

pyspark.sql.functions.repeat(col, n)[source]#

Repeats a string column n times, and returns it as a new string column.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or column name

target column to work on.

nColumn or column name or int

number of times to repeat value.

Changed in version 4.0.0: n now accepts column and column name.

Returns
Column

string with repeated values.

Examples

Example 1: Repeat with a constant number of times

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([('ab',)], ['s',])
>>> df.select("*", sf.repeat("s", 3)).show()
+---+------------+
|  s|repeat(s, 3)|
+---+------------+
| ab|      ababab|
+---+------------+
>>> df.select("*", sf.repeat(df.s, sf.lit(4))).show()
+---+------------+
|  s|repeat(s, 4)|
+---+------------+
| ab|    abababab|
+---+------------+

Example 2: Repeat with a column containing different number of times

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([('ab', 5,), ('abc', 6,)], ['s', 't'])
>>> df.select("*", sf.repeat("s", "t")).show()
+---+---+------------------+
|  s|  t|      repeat(s, t)|
+---+---+------------------+
| ab|  5|        ababababab|
|abc|  6|abcabcabcabcabcabc|
+---+---+------------------+