How to use pivot to generate a single-row matrix?

后端 未结 1 1878
醉梦人生
醉梦人生 2021-01-15 18:06

I need to pivot the following two-column dataframe to one-row one (long to wide).

+--------+-----+
|   udate|   cc|
+--------+-----+
|20090622|  458|
|20090         


        
相关标签:
1条回答
  • 2021-01-15 18:34

    tl;dr In Spark 2.4.0 it simply boils down to using groupBy alone.

    val solution = d.groupBy().pivot("udate").agg(first("cc"))
    scala> solution.show
    +--------+--------+--------+--------+--------+
    |20090622|20090624|20090626|20090629|20090914|
    +--------+--------+--------+--------+--------+
    |     458|   31068|     151|     148|     453|
    +--------+--------+--------+--------+--------+
    

    If you really need the first column with the names just use withColumn and you're done.

    val betterSolution = solution.select(lit("cc") as "udate", $"*")
    scala> betterSolution.show
    +-----+--------+--------+--------+--------+--------+
    |udate|20090622|20090624|20090626|20090629|20090914|
    +-----+--------+--------+--------+--------+--------+
    |   cc|     458|   31068|     151|     148|     453|
    +-----+--------+--------+--------+--------+--------+
    
    0 讨论(0)
提交回复
热议问题