pyspark - Exploding pipe separated data in spark -


i have spark dataframe(input_dataframe), data in dataframe looks below:

id              value  1                 2                x|y|z  3                t|u 

i want have output_dataframe, having pipe separated fields exploded , should below:

id              value  1                 2                x  2                y  2                z  3                t  3                u 

please me achieving desired solution using pyspark. appreciated

we can first split , explode value column using functions below,

>>> l=[(1,'a'),(2,'x|y|z'),(3,'t|u')] >>> df = spark.createdataframe(l,['id','val']) >>> df.show() +---+-----+ | id|  val| +---+-----+ |  1|    a| |  2|x|y|z| |  3|  t|u| +---+-----+  >>> pyspark.sql import functions f >>> df.select('id',f.explode(f.split(df.val,'[|]')).alias('value')).show() +---+-----+ | id|value| +---+-----+ |  1|    a| |  2|    x| |  2|    y| |  2|    z| |  3|    t| |  3|    u| +---+-----+ 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -