0

this may be a very simple question. I want to transpose all the rows of dataframe to columns. I want to convert this df as shown below output DF. What are the ways in spark to achieve this?

Note : I have single column in input DF

import sparkSession.sqlContext.implicits._ val df = Seq(("row1"), ("row2"), ("row3"), ("row4"), ("row5")).toDF("COLUMN_NAME") df.show(false) Input DF: +-----------+ |COLUMN_NAME| +-----------+ |row1 | |row2 | |row3 | |row4 | |row5 | +-----------+ Output DF +----+----+----+----+----+ |row1|row2|row3|row4|row5| +----+----+----+----+----+ 
3
  • 1
    Does this answer your question? How to pivot Spark DataFrame? Commented Jun 20, 2020 at 12:02
  • Think this answer will help: stackoverflow.com/a/49393080/1125159. It's not clear if you want row1, row2. etc to be column names in the output DataFrame. I'm guessing not, so you should update your question to include the desired column names. Commented Jun 20, 2020 at 13:03
  • Without groupBy, pivot, agg, & first ..check this - stackoverflow.com/questions/61686883/… Commented Jun 20, 2020 at 13:48

1 Answer 1

0

Does this help you ?

df.withColumn("group",monotonicallyIncreasingId ).groupBy("group").pivot("COLUMN_NAME").agg(first("COLUMN_NAME")).show 
Sign up to request clarification or add additional context in comments.

1 Comment

This is creating row entries in the output DF

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.