Thursday, 4 April 2019

Pivot a DataFrame

How to pivot the data to create multiple columns out of 1 column with multiple rows.
spark-shell --queue= *;

To adjust logging level use sc.setLogLevel(newLevel).
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 1.6.0
Spark context available as sc 
SQL context available as sqlContext.

scala>  val sqlcontext = new org.apache.spark.sql.SQLContext(sc)
sqlcontext: org.apache.spark.sql.SQLContext = org.apache.spark.sql.SQLContext@4f9a8d71  

scala> val BazarDF = Seq(
     | ("Veg", "tomato", 1.99),
     | ("Veg", "potato", 0.45),
     | ("Fruit", "apple", 0.99),
     | ("Fruit", "pineapple", 2.59),
     | ("Fruit", "apple", 1.99)
     | ).toDF("Type", "Item", "Price")
BazarDF: org.apache.spark.sql.DataFrame = [Type: string, Item: string, Price: double]

scala> BazarDF.show()
+-----+---------+-----+
| Type|     Item|Price|
+-----+---------+-----+
|  Veg|   tomato| 1.99|
|  Veg|   potato| 0.45|
|Fruit|    apple| 0.99|
|Fruit|pineapple| 2.59|
|Fruit|    apple| 1.99|
+-----+---------+-----+

A pivot can be thought of as translating rows into columns while applying one or more aggregations.
Lets see how we can achieve the same using the above dataframe.
We will pivot the data based on "Item" column.
scala> BazarDF.groupBy("Type").pivot("Item").agg(min("Price")).show()
+-----+-----+---------+------+------+
| Type|apple|pineapple|potato|tomato|
+-----+-----+---------+------+------+
|  Veg| null|     null|  0.45|  1.99|
|Fruit| 0.99|     2.59|  null|  null|
+-----+-----+---------+------+------+

You are Done!





25 comments:

  1. very good blog thanks for sharing your valuable blog keep it up! i have some good tips for your blog.
    iPhone Service Center in Chennai Near Me


    ReplyDelete
  2. Excellent information by the author
    Best Play and Pre School for kids in Hyderabad,India. To give your kid a best environment and learning it is the right way to join in play and pre school were kids can build there physically, emotionally and mentally skills developed. We provide programs to kids like Play Group, Nursery, Sanjary Junior, Sanjary Senior and Teacher training Program.
    ­play school in hyderabad

    ReplyDelete
  3. QuickBooks Tech Support Phone Number - If you are looking for a top-grade Accounting and Financial Management software, QuickBooks should be your first choice. It is the most recommended software by Accountants and Bookkeepers all over the world. One of the core reason behind this popularity is the 24×7 available QuickBooks Support services from the toll-free QuickBooks Tech Support Phone Number. QB is highly trustable software and it seldom runs into errors and issues. If in an event, any error pops-up on your screen, simply, dial the toll-free QuickBooks Support Number. Your call will be picked up by an Intuit Certified QuickBooks ProAdvisor who is certified to provide services from the toll-free QuickBooks Support Phone Number.

    Canon Printer Tech Support Phone Number | HP Printer Tech Support Phone Number | Norton Tech Support Phone Number

    ReplyDelete
  4. Excellent blog posting information

    Sanjary Academy is the best Piping Design institute in Hyderabad, Telangana. It is the best Piping design Course in India and we have offer professional Engineering Courses like Piping design Course, QA/QC Course, document controller course, Pressure Vessel Design Course, Welding Inspector Course, Quality Management Course and Safety Officer Course.
    Piping Design Course

    ReplyDelete
  5. Great information of the blog posts

    Pressure Vessel Design Course is one of the courses offered by Sanjary Academy in Hyderabad. We have offer professional Engineering Course like Piping Design Course,QA / QC Course,document Controller course,pressure Vessel Design Course,Welding Inspector Course, Quality Management Course, #Safety officer course.
    Welding Inspector Course
    Safety officer course
    Quality Management Course
    Quality Management Course in India

    ReplyDelete
  6. Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome
    All University BCOM 1st Year TimeTable 2020

    ReplyDelete
  7. Affordable Wall Painting Servicing Dubai by Expert Painters in dubai for house painting solution in dubai, villa painting dubai, apartment painting dubai.

    ReplyDelete
  8. Dubai's Largest & Most Trusted Handyman Dubai At Doorstep. Our Expert Teams are highly trained for any Home Improvement

    ReplyDelete
  9. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!
    data science certification

    ReplyDelete
  10. Escort service in delhi this is very nice bolg. Thinks for whatching my service blog.

    Escort Service In Mahipalpur

    ReplyDelete
  11. I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.
    business analytics course

    ReplyDelete
  12. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.
    AWS Training in Hyderabad
    AWS Course in Hyderabad

    ReplyDelete
  13. this is a very nice blog, its true with the advancement of technologies, threat of breeching is increased. securing our devices with antivirus software is mandatory visit us at : mcafee.com/activate

    ReplyDelete
  14. Well-written and informative content. Keep sharing more with us. Thanks for sharing with us.
    Best Data Science Training in Hyderabad

    ReplyDelete
  15. Thank you, I have recently been searching for information about this topic for ages and
    yours is the best I have discovered so far.
    wedding photography packages
    leather jacket

    ReplyDelete
  16. I think that you should definitely do it again sometime and hopefully I will be able to Pergola & Proud to serve Cargo Services

    ReplyDelete