Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Post Closed as "Duplicate" by philantrovert, eliasah apache-spark
added 32 characters in body
Source Link
Leo C
  • 22.5k
  • 3
  • 28
  • 42

I am finding it hard to transpose columns in DF. Given below is the base dataframe and the expected output

Student Class Subject Grade

Sam 6th Grade Maths A

Sam 6th Grade Science A

Sam 7th Grade Maths A-

Sam 7th Grade Science A

Rob 6th Grade Maths A

Rob 6th Grade Science A-

Rob 7th Grade Maths A-

Rob 7th Grade Science B

Rob 7th Grade AP A

Student Class Subject Grade Sam 6th Grade Maths A Sam 6th Grade Science A Sam 7th Grade Maths A- Sam 7th Grade Science A Rob 6th Grade Maths A Rob 6th Grade Science A- Rob 7th Grade Maths A- Rob 7th Grade Science B Rob 7th Grade AP A 

Expected output:

Student Class Math_Grade Science_Grade AP_Grade

Sam 6th Grade A A

Sam 7th Grade A- A

Rob 6th Grade A A-

Rob 7th Grade A- B A

Student Class Math_Grade Science_Grade AP_Grade Sam 6th Grade A A Sam 7th Grade A- A Rob 6th Grade A A- Rob 7th Grade A- B A 

Please suggest what is the best way to solve this.

I am finding it hard to transpose columns in DF. Given below is the base dataframe and the expected output

Student Class Subject Grade

Sam 6th Grade Maths A

Sam 6th Grade Science A

Sam 7th Grade Maths A-

Sam 7th Grade Science A

Rob 6th Grade Maths A

Rob 6th Grade Science A-

Rob 7th Grade Maths A-

Rob 7th Grade Science B

Rob 7th Grade AP A

Expected output:

Student Class Math_Grade Science_Grade AP_Grade

Sam 6th Grade A A

Sam 7th Grade A- A

Rob 6th Grade A A-

Rob 7th Grade A- B A

Please suggest what is the best way to solve this.

I am finding it hard to transpose columns in DF. Given below is the base dataframe and the expected output

Student Class Subject Grade Sam 6th Grade Maths A Sam 6th Grade Science A Sam 7th Grade Maths A- Sam 7th Grade Science A Rob 6th Grade Maths A Rob 6th Grade Science A- Rob 7th Grade Maths A- Rob 7th Grade Science B Rob 7th Grade AP A 

Expected output:

Student Class Math_Grade Science_Grade AP_Grade Sam 6th Grade A A Sam 7th Grade A- A Rob 6th Grade A A- Rob 7th Grade A- B A 

Please suggest what is the best way to solve this.

Source Link
SriniD
  • 6.8k
  • 5
  • 32
  • 58

Transposing DataFrame columns in Spark Scala

I am finding it hard to transpose columns in DF. Given below is the base dataframe and the expected output

Student Class Subject Grade

Sam 6th Grade Maths A

Sam 6th Grade Science A

Sam 7th Grade Maths A-

Sam 7th Grade Science A

Rob 6th Grade Maths A

Rob 6th Grade Science A-

Rob 7th Grade Maths A-

Rob 7th Grade Science B

Rob 7th Grade AP A

Expected output:

Student Class Math_Grade Science_Grade AP_Grade

Sam 6th Grade A A

Sam 7th Grade A- A

Rob 6th Grade A A-

Rob 7th Grade A- B A

Please suggest what is the best way to solve this.