How to convert map to dataframe?

冷暖自知 提交于 2021-02-07 06:52:43

问题


m is a map as following:

scala> m
res119: scala.collection.mutable.Map[Any,Any] = Map(A-> 0.11164610291904906, B-> 0.11856755943424617, C -> 0.1023171832681312)

I want to get:

name  score
A  0.11164610291904906
B  0.11856755943424617
C  0.1023171832681312

How to get the final dataframe?


回答1:


First covert it to a Seq, then you can use the toDF() function.

val spark = SparkSession.builder.getOrCreate()
import spark.implicits._

val m = Map("A"-> 0.11164610291904906, "B"-> 0.11856755943424617, "C" -> 0.1023171832681312)
val df = m.toSeq.toDF("name", "score")
df.show

Will give you:

+----+-------------------+
|name|              score|
+----+-------------------+
|   A|0.11164610291904906|
|   B|0.11856755943424617|
|   C| 0.1023171832681312|
+----+-------------------+


来源:https://stackoverflow.com/questions/46317098/how-to-convert-map-to-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!