0

I am trying a very basic thing in spark scala, but couldn't figure it out.

I have data like this:

col1 col2
John 1
Jack 2

And, I want to achieve this:

col1 col2
John 1
John 0
John 2
Jack 2
Jack 1
Jack 3

That is, for each row, I want to create two more rows, one is val(col2)-1 and val(col2)+1.

I tried to use explode, but couldn't figure out how to do it properly.

val exploded_df = df.withColumn("col2", explode($"col2" -1, $"col2", $"col2" +1 ))

And, got:

too many arguments for method explode: (e: org.apache.spark.sql.Column)org.apache.spark.sql.Column

1 Answer 1

1

You need to specify an array column to explode:

val exploded_df = df.withColumn("col2", explode(array($"col2" -1, $"col2", $"col2" +1 )))
Sign up to request clarification or add additional context in comments.

Comments

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.