Repeat the column in Pyspark

In order to repeat the column in pyspark we will be using repeat() Function. We look at an example on how to  repeat the string of the column in pyspark.

  • Repeat the string of the column in pyspark.

Syntax:

 repeat(colname,n)

colname – Column name.
n –  number of times repeat

We will be using the dataframe named df

Repeat the column in Pyspark 1

 

 

Repeat the column in Pyspark

repeat() function takes up column name and number of times as argument.

### Repeat the column in pyspark

from pyspark.sql.functions import repeat, expr

df.withColumn("new_column",(expr("repeat(name, 2)"))).show()

In our example column name “name” is repeated twice.

Repeat the column in Pyspark 2

 

Repeat the column in Pyspark                                                                                     Repeat the column in Pyspark