How to create a new column with random strings in pyspark

Viewed 67

I am trying to add a new column to my dataframe with random strings, how do I conform the output of randomword to input of withColumn


def randomword(length):
   letters = string.ascii_lowercase
   return ''.join(random.choice(letters) for i in range(length))

df_with_new_col = mydf.withColumn('random_string', randomword(10))

1 Answers
''.join(random.choices(string.ascii_lowercase, k=N))

random.choices Python 3.6+

Related