I have tried the built in stack function described in this post Unpivot in spark-sql/pyspark for Scala, and works fine for each of the columns identified with a code that contains a letter but not in those columns where the code is just a number.
I have a dataframe df that looks like this
I applied as mentioned in the linked answer:
val result = df.select($"Id", expr("stack(3, '00C', 00C, '0R5', 0R5, '234', 234)"))
And the result is this one
What I want is that the value of the row 234 was 0 as it should be.