1

I have a problem with converting one row using three 3 columns into 3 rows
For example:

 <pre>
<b>ID</b> |  <b>String</b>  |  <b>colA</b> | <b>colB</b> | <b>colC</b>
<em>1</em>  | <em>sometext</em> |   <em>1</em>   |  <em>2</em>   |  <em>3</em>
</pre>

I need to convert it into:

<pre>
<b>ID</b> |  <b>String</b>  |  <b>resultColumn</b>
<em>1</em>  | <em>sometext</em> |     <em>1</em>  
<em>1</em>  | <em>sometext</em> |     <em>2</em>   
<em>1</em>  | <em>sometext</em> |     <em>3</em>    
</pre>

I just have dataFrame which is connected with first schema(table).

val df: dataFrame

Note: I can do it using RDD, but do we have other way? Thanks

1 Answer 1

1

Assuming that df has the schema of your first snippet, I would try:

df.select($"ID", $"String", explode(array($"colA", $"colB",$"colC")).as("resultColumn"))

I you further want to keep the column names, you can use a trick that consists in creating a column of arrays that contains the array of the value and the name. First create your expression

val expr = explode(array(array($"colA", lit("colA")), array($"colB", lit("colB")), array($"colC", lit("colC"))))

then use getItem (since you can not use generator on nested expressions, you need 2 select here)

df.select($"ID, $"String", expr.as("tmp")).select($"ID", $"String", $"tmp".getItem(0).as("resultColumn"), $"tmp".getItem(1).as("columnName"))

It is a bit verbose though, there might be more elegant way to do this.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Greg, how would you keep the column name into a separate column ?
@Eldinea, I edited my answer to address your question as well
Actually you can easily do it with posexplode : posexplode(array($"col1",$"col2") And you will have two columns : pos for the number of the column and col for the value spark.apache.org/docs/latest/api/sql/#posexplode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.