Databricks PySpark Explode and Pivot Columns

<p>The&nbsp;<code>explode</code>&nbsp;function in PySpark is used to transform a column with an array of values into multiple rows. Each row of the resulting DataFrame will contain one element of the original array column.</p> <p>Here is an example of how to use the&nbsp;<code>explode</code>&nbsp;function:</p> <pre> from pyspark.sql.functions import explode</pre> <pre> # create a sample DataFrame data = [(&quot;Alice&quot;, [1, 2, 3]), (&quot;Bob&quot;, [4, 5]), (&quot;Charlie&quot;, [6])] df = spark.createDataFrame(data, [&quot;name&quot;, &quot;numbers&quot;])# explode the numbers column df_exploded = df.select(&quot;name&quot;, explode(&quot;numbers&quot;).alias(&quot;number&quot;))# show the result df_exploded.show()</pre> <p><a href="https://medium.com/@shuklaprashant9264/databricks-pyspark-explode-and-pivot-columns-a8d1b4e713f1"><strong>Website</strong></a></p>
Tags: pivot Columns